@@ -158,7 +158,8 @@ const showHelp = inject('showHelp')
158
158
const signals = inject (' signals' )
159
159
const terminal = inject (' terminal' )
160
160
161
- // Indicates if the query, including multiline queries, is invalid
161
+ // Indicates if the query, including multiline queries and reverse I search, is
162
+ // invalid
162
163
const isOutdated = ref (false )
163
164
const isReverseISearch = ref (false )
164
165
const multilineQueryRefs = ref (null )
@@ -172,6 +173,7 @@ const local = reactive({
172
173
prompt: terminal .value .prompt ,
173
174
query: ' '
174
175
})
176
+ // Entered with "\"
175
177
const multilineQueries = reactive ([])
176
178
177
179
const isBeforeReverseISearch = computed (() => {
@@ -290,8 +292,13 @@ const autocompleteQuery = async () => {
290
292
const cycleReverseISearch = () => {
291
293
// TODO
292
294
}
293
- // Focuses the last query or multiline query
295
+ // Focuses the last query, multiline query or reverse I search
294
296
const focus = () => {
297
+ if (isReverseISearch .value ) {
298
+ reverseISearchRef .value .focus ()
299
+ return
300
+ }
301
+
295
302
if (isEmpty (multilineQueries)) {
296
303
queryRef .value .focus ()
297
304
return
@@ -312,6 +319,18 @@ const hideReverseISearch = async () => {
312
319
const resizeReverseISearch = () => {
313
320
reverseISearchRef .value .style .width = ` ${ parseInt (reverseISearch .value .length )} ch`
314
321
}
322
+ // Sets the last multiline query
323
+ const setLastMultilineQuery = multilineQuery => {
324
+ set (multilineQueries, size (multilineQueries) - 1 , multilineQuery)
325
+ }
326
+ // Shows and focuses the reverse I search
327
+ const showReverseISearch = async () => {
328
+ isReverseISearch .value = true
329
+
330
+ // Wait for DOM
331
+ await nextTick ()
332
+ reverseISearchRef .value .focus ()
333
+ }
315
334
// Cancels the current query or multiline query and creates a new query
316
335
const sigint = () => {
317
336
if (isEmpty (multilineQueries)) {
@@ -329,18 +348,6 @@ const sigint = () => {
329
348
isOutdated .value = true
330
349
appendToHistory (createQuery ())
331
350
}
332
- // Sets the last multiline query
333
- const setLastMultilineQuery = multilineQuery => {
334
- set (multilineQueries, size (multilineQueries) - 1 , multilineQuery)
335
- }
336
- // Shows and focuses the reverse I search
337
- const showReverseISearch = async () => {
338
- isReverseISearch .value = true
339
-
340
- // Wait for DOM
341
- await nextTick ()
342
- reverseISearchRef .value .focus ()
343
- }
344
351
// Deactivates this query or spawns eventually new multiline queries and finally
345
352
// dispatches the query to execute it
346
353
const submit = async () => {
@@ -373,43 +380,38 @@ const submit = async () => {
373
380
.replaceAll (/ (?<!\\ )\\ (?!\\ )/ g , ' ' )
374
381
.trim ()
375
382
376
- // Dispatch the query to the parent
383
+ // Dispatch the query to the terminal
377
384
dispatch (query)
378
385
}
379
386
380
- const unwatchMultilineQueries = watch (multilineQueries, async () => {
381
- await nextTick ()
387
+ const unwatchIsOutdated = watch (isOutdated, () => {
388
+ // Free resources if query, multiline query or reverse I search are
389
+ // outdated/inactive
382
390
383
- const lastMultilineQueryRef = last (multilineQueryRefs .value )
384
- // Apply given cursor position to actual cursor position
385
- setCursorPosition (lastMultilineQueryRef .selectionStart )
391
+ signals .off (' SIGINT' , sigint)
392
+ unwatchLocalQuery ()
393
+ unwatchTerminalQuery ()
394
+ unwatchTerminalCursorPosition ()
395
+ placeholder .value = ' '
396
+ unwatchMultilineQueries ()
397
+ unwatchIsOutdated ()
398
+ unwatchReverseISearch ()
386
399
})
387
400
const unwatchLocalQuery = watch (() => local .query , async () => {
388
401
await nextTick ()
389
402
390
403
// Apply given cursor position to actual cursor position
391
404
setCursorPosition (queryRef .value .selectionStart )
392
405
})
393
- const unwatchTerminalCursorPosition = watch (
394
- () => terminal .value .cursorPosition ,
395
- async cursorPosition => {
396
- await nextTick ()
397
-
398
- // Apply given cursor position to actual cursor position
399
- queryRef .value .setSelectionRange (cursorPosition, cursorPosition)
400
- }
401
- )
402
- const unwatchTerminalQuery = watch (
403
- () => terminal .value .query ,
404
- async query => {
405
- await nextTick ()
406
+ const unwatchMultilineQueries = watch (multilineQueries, async () => {
407
+ await nextTick ()
406
408
407
- // This allows to mutate the query from outside the component and not only
408
- // inside a history entry
409
- local .query = query
410
- }
411
- )
409
+ const lastMultilineQueryRef = last (multilineQueryRefs .value )
410
+ // Apply given cursor position to actual cursor position
411
+ setCursorPosition (lastMultilineQueryRef .selectionStart )
412
+ })
412
413
const unwatchReverseISearch = watch (reverseISearch, () => {
414
+ // Search in dispatched queries
413
415
for (const dispatchedQuery of terminal .value .dispatchedQueries ) {
414
416
if (dispatchedQuery .startsWith (reverseISearch .value )) {
415
417
if (isEmpty (multilineQueries)) {
@@ -429,25 +431,31 @@ const unwatchReverseISearch = watch(reverseISearch, () => {
429
431
430
432
reverseISearchStatus .value = ' failed reverse-i-search'
431
433
})
432
- const unwatchIsOutdated = watch (isOutdated, () => {
433
- // Free resources if query, multiline query or reverse I search is
434
- // outdated/inactive
434
+ const unwatchTerminalCursorPosition = watch (
435
+ () => terminal .value .cursorPosition ,
436
+ async cursorPosition => {
437
+ await nextTick ()
435
438
436
- signals .off (' SIGINT' , sigint)
437
- unwatchLocalQuery ()
438
- unwatchTerminalQuery ()
439
- unwatchTerminalCursorPosition ()
440
- placeholder .value = ' '
441
- unwatchMultilineQueries ()
442
- unwatchIsOutdated ()
443
- unwatchReverseISearch ()
444
- })
439
+ // Apply given cursor position to actual cursor position
440
+ queryRef .value .setSelectionRange (cursorPosition, cursorPosition)
441
+ }
442
+ )
443
+ const unwatchTerminalQuery = watch (
444
+ () => terminal .value .query ,
445
+ async query => {
446
+ await nextTick ()
447
+
448
+ // This allows to mutate the query from outside the component and not only
449
+ // inside a history entry
450
+ local .query = query
451
+ }
452
+ )
445
453
446
454
onMounted (() => {
447
455
// Bind signals
448
456
signals .on (' SIGINT' , sigint)
449
457
450
- // Set initial query state
458
+ // Set initial query states
451
459
setQuery (' ' )
452
460
setCursorPosition (0 )
453
461
0 commit comments