@@ -47,10 +47,14 @@ opcode_context() {
4747 fi
4848 }
4949
50+ abort () {
51+ echo " $1 " >&2
52+ exit " ${2:- 1} "
53+ }
54+
5055 need_config () {
5156 if [[ ! -f $CONFIG_FILE ]]; then
52- echo " Cannot find config file ($CONFIG_FILE )"
53- exit 1
57+ abort " Cannot find config file ($CONFIG_FILE )"
5458 fi
5559 }
5660
@@ -63,28 +67,63 @@ opcode_context() {
6367 exit 1
6468 fi
6569
66- exact=" ^${CODE} :[[:space:]]*(.+)$"
67- fuzzy=" ^${CODE} [^\:]*:[[:space:]]*(.+)$"
70+ COMMAND=$( get_command_from_file " exact" )
71+ if [[ -z $COMMAND ]]; then
72+ COMMAND=$( get_command_from_file " fuzzy" )
73+ fi
74+ }
6875
69- # shellcheck disable=SC2162
70- while IFS= read line || [ -n " $line " ]; do
71- if [[ $line =~ $exact ]]; then
72- COMMAND=" ${BASH_REMATCH[1]} "
73- break
74- fi
75- done < " $CONFIG_FILE "
76+ get_command_from_file () {
77+ local pattern
7678
77- if [[ -n " $COMMAND " ]]; then
78- return
79+ if [[ $1 == " fuzzy" ]]; then
80+ pattern=" ^${CODE} [^:]*:[[:space:]]*(.*)$"
81+ else
82+ pattern=" ^${CODE} :[[:space:]]*(.*)$"
7983 fi
8084
81- # shellcheck disable=SC2162
82- while IFS= read line || [ -n " $line " ]; do
83- if [[ $line =~ $fuzzy ]]; then
84- COMMAND=" ${BASH_REMATCH[1]} "
85- break
85+ local command=" "
86+ local collect_command=false
87+ local temp_line=" "
88+
89+ while IFS= read -r line || [[ -n $line ]]; do
90+ if [[ -n " $temp_line " ]]; then
91+ trimmed_line=${line# " ${line%% [![:space:]]* } " }
92+ temp_line=" ${temp_line% \\ } $trimmed_line "
93+ if [[ " $line " =~ \\ $ ]]; then
94+ continue
95+ else
96+ line=" $temp_line "
97+ temp_line=" "
98+ fi
99+ fi
100+
101+ if [[ " $line " =~ \\ $ ]]; then
102+ trimmed_line=${line# " ${line%% [![:space:]]* } " }
103+ temp_line=" ${temp_line% \\ } $trimmed_line "
104+ continue
105+ fi
106+
107+ if $collect_command ; then
108+ if [[ -z " ${line} " || ! " ${line} " =~ ^[[:space:]]+ ]]; then
109+ break
110+ fi
111+
112+ trimmed_line=" ${line# " ${line%% [![:space:]]* } " } "
113+ command=" ${command: +$command && } $trimmed_line "
114+ else
115+ if [[ $line =~ $pattern ]]; then
116+ command=" ${BASH_REMATCH[1]} "
117+ if [[ -n $command ]]; then
118+ break
119+ fi
120+
121+ collect_command=true
122+ fi
86123 fi
87124 done < " $CONFIG_FILE "
125+
126+ echo " $command "
88127 }
89128
90129 run_command () {
@@ -97,8 +136,7 @@ opcode_context() {
97136 eval " $COMMAND " " $@ "
98137 fi
99138 else
100- echo " Code not found: $CODE "
101- exit 1
139+ abort " Code not found: $CODE "
102140 fi
103141 }
104142
@@ -135,7 +173,7 @@ opcode_context() {
135173
136174 list_codes () {
137175 need_config
138- regex=" ^([^#: ]+):"
176+ regex=" ^([a-zA-Z0-9_- ]+):"
139177 break_regex=" ^[[:space:]]*private[[:space:]]*$"
140178
141179 # shellcheck disable=SC2162
@@ -199,8 +237,7 @@ opcode_context() {
199237 # shellcheck disable=SC2086
200238 echo $COMMAND
201239 else
202- echo " Code not found: $CODE "
203- exit 1
240+ abort " Code not found: $CODE "
204241 fi
205242
206243 fi
@@ -270,7 +307,7 @@ opcode_context() {
270307 bold () { printf " \e[1m%b\e[0m\n" " $* " ; }
271308
272309 opcode_initialize () {
273- VERSION=" 0.6.5 "
310+ VERSION=" 0.7.0 "
274311 LONG_USAGE=false
275312 set_config_file
276313 set -e
0 commit comments