-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
337 lines (290 loc) · 8.48 KB
/
functions.php
File metadata and controls
337 lines (290 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
############ Help func #################
/**
* @param array $lines
* @param string $queryString
* @param bool $withParameter true only show one item
*
* @return array
*/
function getBashCommands(array $lines, string $queryString, $withParameter = false): array
{
$itemsArr = [];
$stop = false;
// put multi lines comments in comment container.
$commentContainer = '';
// retrieve comment string of alias or functions, except custom tag (#alfred;)
$clearCommentString = false;
foreach ($lines as $line) {
$line = str_replace(array("\r", "\n"), '', $line);
$oneCmd = [];
$cmd = '';
$uid = '';
$subTitle = "";
$valid = false;
$arg = "";
$canAddItemToResult = false;
// start with comment tag
if (strpos($line, '#alfred;') === 0) {
// lower case
$bashCommentSegments = explode(';', $line);
// remove first element, which is #alfred;
array_shift($bashCommentSegments);
// Comment tag in bash profile
// #alfred; command:XXX; parameters:XXX, ('none' means no parameter); description: XXX
foreach ($bashCommentSegments as $segment) {
// replace space
$segment = trim(preg_replace('/\s\s+/', ' ', $segment));
[$key, $value] = explode(':', $segment);
$key = trim($key);
$value = trim($value);
if ($key === 'command') {
$bashCmd = $value;
$uid = $bashCmd;
$arg = $bashCmd;
[$inputCmd] = explode(' ', $queryString);
// this is one with argument
if ($withParameter) {
$arg = $queryString;
$valid = true;
if ($inputCmd === $bashCmd) {
$canAddItemToResult = true;
$stop = true;
}
} elseif ( ! alfredQueryMatch($queryString, $bashCmd)) {
// not start with query letter, stop to show
break;
}
$cmd .= 'Cmd: ' . $bashCmd;
}
if ($key === 'parameters' | $key === 'var') {
if ($value === 'none') {
$valid = true;
}
$cmd .= " ($value) ";
}
if ($key === 'description') {
$subTitle .= $value;
}
if ( ! $withParameter || ($withParameter && $canAddItemToResult)) {
$oneCmd = alfredItem($cmd, $uid, $arg, $subTitle, $valid);
}
}
$itemsArr = addAlfredItem($oneCmd, $itemsArr);
$clearCommentString = true;
} else if ($aliasCmdArr = getAliasCmd($line, $queryString)) {
// check if it is alias
[$commentString] = getCommentLines($commentContainer);
[$aliasCmd, $content] = $aliasCmdArr;
$title = 'Alias: ' . $aliasCmd;
$subTitle = trim($commentString, ', ') . $content;
$valid = true;
$arg = $aliasCmd;
$oneCmd = alfredItem($title, $aliasCmd, $arg, $subTitle, $valid);
$itemsArr = addAlfredItem($oneCmd, $itemsArr);
$clearCommentString = true;
} else if ($funcCmdArr = getFunctionCmds($line, $queryString)) {
// check if it is functions
[$commentString, $parameterString] = getCommentLines($commentContainer);
[$funcCmd, $content] = $funcCmdArr;
// check with parameter
if ($withParameter) {
[$inputCmd, $firstPara] = explode(' ', $queryString);
} else {
$inputCmd = $queryString;
}
// check if contains parameter string
if ( ! empty($parameterString)) {
$valid = false;
$parameterString = ' <' . $parameterString . '>';
}else{
// if without parameter string, is valid
$valid = true;
}
$arg = $funcCmd;
if ($inputCmd === $funcCmd) {
$stop = true;
if (preg_match("/ *none */i", $parameterString)) {
$valid = true;
$arg = $queryString;
} elseif (isset($firstPara) && ! empty($firstPara)) {
$valid = true;
$arg = $queryString;
}
}
$title = 'Func: ' . $funcCmd . $parameterString;
$subTitle = trim($commentString, ', ') . $content;
$oneCmd = alfredItem($title, $funcCmd, $arg, $subTitle, $valid);
$itemsArr = addAlfredItem($oneCmd, $itemsArr);
$clearCommentString = true;
} elseif ($comment = getComment($line)) {
$clearCommentString = false;
$commentContainer .= $comment . PHP_EOL;
} elseif (empty(trim($line))) {
$clearCommentString = true;
}
if ($clearCommentString) {
$commentContainer = '';
}
if ($stop) {
// only show one item
break;
}
}
return array_values($itemsArr);
}
/**
* @param string $commentContainer
*
* @return array
*/
function getCommentLines(string $commentContainer): array
{
$parameterString = false;
$commentString = '';
if ( ! empty($commentContainer)) {
$commentLinesArr = explode(PHP_EOL, $commentContainer);
$commentString = '';
foreach ($commentLinesArr as $commentLine) {
$regex = "/^ *(parameter|parameters|var): */i";
preg_match($regex, $commentLine, $matches);
if ( ! empty($matches)) {
$parameterString = str_replace($matches[0], '', $commentLine);
} else {
$commentString .= $commentLine . ', ';
}
}
}
return array($commentString, $parameterString);
}
function getComment($line)
{
$regex = "/^ *#+ */";
preg_match($regex, $line, $matches);
if ( ! empty($matches)) {
return str_replace($matches[0], '', $line);
}
return false;
}
/**
* @param string $queryString
* @param string $bashProfileCmd
*
* @return bool
*/
function alfredQueryMatch(string $queryString, string $bashProfileCmd): bool
{
// allow to show all alfred items at first call
if (empty($queryString)) {
return true;
}
$queryString = strtolower($queryString);
$bashProfileCmd = strtolower(trim($bashProfileCmd));
if ( ! empty($bashProfileCmd)) {
// only check first part of query string, the rest is argument
[$inputCmd] = explode(' ', $queryString);
// check query if same as bash command
if ($inputCmd === $bashProfileCmd) {
return true;
}
// show recommendation of bash command if it contain input cmd
$cmds = array_map('strtolower', splitCamelCaseString($bashProfileCmd));
foreach ($cmds as $cmd) {
if (strpos($cmd, $inputCmd) === 0) {
return true;
}
}
}
return false;
}
/**
* @param string $queryString
*
* @return array
*/
function splitCamelCaseString(string $queryString): array
{
//split string to array according to CamelString, connect-by, connect_by
return preg_split('/(?=[A-Z])|_|-/', $queryString);
}
/**
* @param array $oneCmd
* @param array $itemsArr
*
* @return array
*/
function addAlfredItem(array $oneCmd, array $itemsArr): array
{
if ( ! empty($oneCmd) && ! array_key_exists($oneCmd['uid'], $itemsArr)) {
$itemsArr[$oneCmd['uid']] = $oneCmd;
}
return $itemsArr;
}
/**
* @param string $title
* @param string $uid
* @param string $arg
* @param string $subTitle
* @param bool $valid
*
* @return array
*/
function alfredItem(string $title, string $uid, string $arg, string $subTitle, bool $valid): array
{
$oneCmd = [];
$oneCmd ['title'] = $title;
$oneCmd ['uid'] = $uid;
$oneCmd ['arg'] = $arg;
$oneCmd ['subtitle'] = $subTitle;
$oneCmd ['valid'] = $valid ? "true" : "false";
$oneCmd ['autocomplete'] = $arg;
return $oneCmd;
}
/**
* Get alias cmd
*
* @param $line
*
* @param $queryString
*
* @return bool|mixed
*/
function getAliasCmd($line, $queryString)
{
$matches = [];
// looking for 'alias xx-_xx='
$regex = "/^ *alias +[\w\-_\.\~]+=/";
preg_match($regex, $line, $matches);
if ( ! empty($matches)) {
$matchedAlias = $matches[0];
$alias = str_replace([' ', '='], '', $matchedAlias);
// replace first one alias
$alias = preg_replace('/alias/', '', $alias, 1);
if (alfredQueryMatch($queryString, $alias)) {
$content = str_replace(["'", '"', '\t'], '', substr($line, strlen($matchedAlias)));
$contentArr = explode('#', $content);
$detail = trim($contentArr[0] ?? '');
$comment = trim($contentArr[1] ?? '');
return [$alias, ucfirst($comment) . ' |> ' . $detail];
}
}
return false;
}
function getFunctionCmds($line, $queryString)
{
$matches = [];
// looking for 'func() or function func()' and not start with _
$regex = "/^ *(function? +(?!_)[\w\-_\.\~]+|(?!_)[\w\-_\.\~]+) *\( *\) */";
preg_match($regex, $line, $matches);
if ( ! empty($matches)) {
$matchedFunc = $matches[0];
$func = str_replace([' ', 'function', '(', ')'], '', $matchedFunc);
if (alfredQueryMatch($queryString, $func)) {
$contentArr = explode('#', $line);
$detail = trim($contentArr[0] ?? '');
$comment = trim($contentArr[1] ?? '');
return [$func, ucfirst($comment) . ' |> ' . $detail];
}
}
return false;
}