33 <span
44 v-if =" !hidePrompt"
55 class =" vue-command__query__prompt" >
6- {{ prompt }}
6+ {{ local. prompt }}
77 </span >
88
99 <!-- TODO Move autocomplete and search to parent -->
1010 <!-- TODO Make textarea to enforce word break -->
1111 <input
1212 ref =" queryRef"
13- v-model =" query"
13+ v-model =" local. query"
1414 class =" vue-command__query__input"
1515 :disabled =" isOutdated"
1616 :placeholder =" placeholder"
2121 @input =" setQuery($event.target.value)"
2222 @keydown.tab.exact.prevent =" autocompleteQuery"
2323 @keydown.ctrl.r.exact.prevent =" reverseISearch"
24- @keyup.enter.exact =" submit($event.target.value) " />
24+ @keyup.enter.exact =" submit" />
2525 </div >
2626</template >
2727
@@ -33,6 +33,7 @@ import {
3333 inject ,
3434 defineExpose ,
3535 onBeforeMount ,
36+ reactive ,
3637 nextTick
3738} from ' vue'
3839import {
@@ -54,34 +55,41 @@ import size from 'lodash.size'
5455const appendToHistory = inject (' appendToHistory' )
5556const dispatch = inject (' dispatch' )
5657const hidePrompt = inject (' hidePrompt' )
58+ const helpText = inject (' helpText' )
59+ const helpTimeout = inject (' helpTimeout' )
5760const optionsResolver = inject (' optionsResolver' )
5861const parser = inject (' parser' )
5962const programs = inject (' programs' )
6063const setCursorPosition = inject (' setCursorPosition' )
6164const setQuery = inject (' setQuery' )
65+ const showHelp = inject (' showHelp' )
6266const terminal = inject (' terminal' )
6367
6468const isOutdated = ref (false )
6569const placeholder = ref (' ' )
66- const prompt = ref (' ' )
67- const query = ref (' ' )
6870const queryRef = ref (null )
6971
72+ const local = reactive ({
73+ prompt: ' ' ,
74+ query: ' '
75+ })
76+
7077// Autocompletes a command and calls options resolver with found program
7178// and parsed query if there are more than two arguments
7279const autocompleteQuery = async () => {
80+ const query = local .query
7381 // An empty query shall be never autocompleted
74- if (isEmpty (query . value )) {
82+ if (isEmpty (query)) {
7583 return
7684 }
7785
7886 // ['bash', '--help']
79- const parsedQuery = defaultParser (query . value )
87+ const parsedQuery = defaultParser (query)
8088 // bash, make, sh
8189 const command = head (parsedQuery)
8290
8391 const commands = []
84- // TODO Use lodashs filter
92+ // TODO Use lodash
8593 for (const program of programs .value ) {
8694 // If program starts with command, add command to be possibly autocompleted
8795 if (program .startsWith (command)) {
@@ -100,16 +108,16 @@ const autocompleteQuery = async () => {
100108 const program = head (commands)
101109 if (and (
102110 // Check if query expects autocomplete
103- lt (program .length , size (trimStart (query . value ))),
111+ lt (program .length , size (trimStart (query))),
104112 // Check if user provided options resolver
105113 isFunction (optionsResolver), isFunction (parser))
106114 ) {
107- optionsResolver (program, parser (query . value ), setQuery)
115+ optionsResolver (program, parser (query), setQuery)
108116 return
109117 }
110118
111119 // If query has white spaces at the end, ignore
112- if (gt (program .length , size (trimStart (query . value )))) {
120+ if (gt (program .length , size (trimStart (query)))) {
113121 setQuery (program)
114122 }
115123
@@ -122,41 +130,45 @@ const autocompleteQuery = async () => {
122130
123131 // Print a list of commands
124132
125- // Preserve the current query since it gets emptied after "createStdout"
126- // exited
127- const previousQuery = query . value
133+ // Invalidate query since a new one is created and to the current one
134+ isOutdated . value = true
135+
128136 appendToHistory (createStdout (listFormatter (... commands)))
129137
130- // Wait until the query component has been rendered
138+ // We have to wait for the query to be loaded
131139 await nextTick ()
132140
133- // Set the previous query again
134- setQuery (previousQuery)
135- query .value = previousQuery
136- // Invalidate query
137- isOutdated .value = true
141+ // Overwrite new query with old one
142+ setQuery (local .query )
138143 }
139144 }
140145}
141-
142146// Focuses the input
143147const focus = () => {
144148 queryRef .value .focus ()
145149}
146-
147150const reverseISearch = event => {
148151 // TODO
149152 // console.debug(event)
150153}
151-
154+ const showDelayedHelp = () => {
155+ const timeout = setTimeout (() => {
156+ placeholder .value = helpText
157+ }, helpTimeout)
158+
159+ const unwatchIsDisabled = watch (isOutdated, () => {
160+ clearTimeout (timeout)
161+ unwatchIsDisabled ()
162+ })
163+ }
152164// Deactivates this query and dispatches it to execute the command
153165const submit = () => {
154166 isOutdated .value = true
155167 dispatch (query .value )
156168}
157169
158170// Apply given cursor position to actual cursor position
159- const unwatchQuery = watch (query, () => {
171+ const unwatchLocalQuery = watch (() => local . query , () => {
160172 setCursorPosition (queryRef .value .selectionStart )
161173})
162174const unwatchTerminalCursorPosition = watch (
@@ -169,38 +181,27 @@ const unwatchTerminalCursorPosition = watch(
169181// inside a history entry
170182const unwatchTerminalQuery = watch (
171183 () => terminal .value .query ,
172- queryValue => {
173- query . value = queryValue
184+ query => {
185+ local . query = query
174186 }
175187)
176188const unwatchIsDisabled = watch (isOutdated, () => {
177189 unwatchTerminalQuery ()
178- unwatchQuery ()
190+ unwatchLocalQuery ()
179191 unwatchTerminalCursorPosition ()
180192 unwatchIsDisabled ()
181193 placeholder .value = ' '
182194})
183195
184196onBeforeMount (() => {
185- prompt . value = terminal .value .prompt
197+ local . prompt = terminal .value .prompt
186198})
187199onMounted (() => {
188200 focus ()
189201
190- const helpText = inject (' helpText' )
191- const helpTimeout = inject (' helpTimeout' )
192- const showHelp = inject (' showHelp' )
193-
194202 // Show eventually help as placeholder
195203 if (and (showHelp, ! isOutdated .value )) {
196- const timeout = setTimeout (() => {
197- placeholder .value = helpText
198- }, helpTimeout)
199-
200- const unwatchIsDisabled = watch (isOutdated, () => {
201- clearTimeout (timeout)
202- unwatchIsDisabled ()
203- })
204+ showDelayedHelp ()
204205 }
205206})
206207
0 commit comments