Skip to content

Commit f14389e

Browse files
authored
feat(Console): improve command history navigation (#1843)
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
1 parent 158bfd6 commit f14389e

1 file changed

Lines changed: 45 additions & 26 deletions

File tree

src/components/widgets/console/ConsoleCommand.vue

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
@input="emitChange"
2020
@keyup.enter.exact="emitSend(newValue)"
2121
@keydown.enter.exact.prevent
22-
@keydown.up.exact.prevent="historyUp()"
23-
@keydown.down.exact.prevent="historyDown()"
22+
@keydown.up.exact="handleKeyUp"
23+
@keydown.down.exact="handleKeyDown"
2424
@keydown.prevent.tab="autoComplete()"
2525
/>
2626
</v-col>
@@ -38,7 +38,7 @@
3838
<script lang="ts">
3939
import { Vue, Component, Prop, Watch, Ref } from 'vue-property-decorator'
4040
import { Globals } from '@/globals'
41-
import type { VTextField } from 'vuetify/lib'
41+
import type { VTextarea } from 'vuetify/lib'
4242
import type { GcodeCommands } from '@/store/printer/types'
4343
4444
@Component({})
@@ -47,7 +47,7 @@ export default class ConsoleCommand extends Vue {
4747
readonly value!: string
4848
4949
@Ref('input')
50-
readonly input!: VTextField
50+
readonly input!: VTextarea
5151
5252
@Prop({ type: Boolean })
5353
readonly disabled?: boolean
@@ -68,14 +68,12 @@ export default class ConsoleCommand extends Vue {
6868
newValue = ''
6969
commandHistoryCount = Globals.CONSOLE_COMMAND_HISTORY
7070
history: string[] = []
71-
originalHistory: string[] = []
72-
isFirst = true
71+
historyIndex = -1
7372
7473
mounted () {
7574
this.newValue = this.value
7675
const savedHistory: string[] = this.$typedState.console.commandHistory
7776
this.history = [...savedHistory]
78-
this.originalHistory = [...savedHistory]
7977
}
8078
8179
emitChange (val: string) {
@@ -86,35 +84,56 @@ export default class ConsoleCommand extends Vue {
8684
emitSend (val: string) {
8785
if (val && val.length > 0) {
8886
if (this.history.length >= this.commandHistoryCount) {
89-
this.originalHistory.pop()
87+
this.history.pop()
9088
}
91-
this.originalHistory.unshift(val)
92-
this.$typedDispatch('console/onUpdateCommandHistory', [...this.originalHistory])
93-
this.history = [...this.originalHistory]
94-
this.isFirst = true
89+
this.history.unshift(val)
90+
this.$typedDispatch('console/onUpdateCommandHistory', [...this.history])
91+
this.historyIndex = -1
9592
this.$emit('send', val)
9693
}
9794
}
9895
99-
historyUp () {
100-
if (this.history.length >= 1) {
101-
if (!this.isFirst) {
102-
const f = this.history.shift()
103-
if (f != null) this.history.push(f)
96+
handleKeyUp (event: KeyboardEvent) {
97+
const el = this.input.$refs.input
98+
99+
if (
100+
el.selectionStart === el.selectionEnd &&
101+
el.value.slice(0, el.selectionStart ?? 0).indexOf('\n') === -1
102+
) {
103+
event.preventDefault()
104+
105+
const historyLength = this.history.length
106+
107+
if (
108+
historyLength >= 1 &&
109+
this.historyIndex !== historyLength
110+
) {
111+
const index = ++this.historyIndex
112+
113+
this.emitChange(this.history[index] ?? '')
104114
}
105-
this.emitChange(this.history[0])
106-
this.isFirst = false
107115
}
108116
}
109117
110-
historyDown () {
111-
if (this.history.length >= 1) {
112-
if (!this.isFirst) {
113-
const f = this.history.pop()
114-
if (f != null) this.history.unshift(f)
118+
handleKeyDown (event: KeyboardEvent) {
119+
const el = this.input.$refs.input
120+
121+
if (
122+
el.selectionStart === el.selectionEnd &&
123+
el.value.slice(el.selectionEnd ?? 0).indexOf('\n') === -1
124+
) {
125+
event.preventDefault()
126+
127+
const historyLength = this.history.length
128+
129+
if (
130+
historyLength >= 1 &&
131+
this.historyIndex !== -1
132+
) {
133+
const index = --this.historyIndex
134+
135+
this.emitChange(this.history[index] ?? '')
115136
}
116-
this.emitChange(this.history[0])
117-
this.isFirst = false
118137
}
119138
}
120139

0 commit comments

Comments
 (0)