Skip to content
This repository was archived by the owner on Feb 28, 2026. It is now read-only.

Commit 8f31ca1

Browse files
committed
improved console history logic
1 parent 0e6f15a commit 8f31ca1

4 files changed

Lines changed: 58 additions & 25 deletions

File tree

frontend/desktop/package.json.md5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a24ece3a3d86e13594977247e3352385
1+
371e648509f99cc686955a6284995fb5

frontend/desktop/src/components/dbfragments/console.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ const DBConsoleFragment = ({ }: DBConsolePropType) => {
2121
const output = useAppSelector(selectBlocks)
2222
const [input, setInput] = useState("")
2323
const [nfocus, setFocus] = useState<number>(0)
24-
const commands = output.filter( e => e.cmd === true)
25-
const [pointer, setPointer] = useState<number>(commands.length-1)
24+
const history = output.filter(e => e.cmd).filter(e => e.text !== "").map(e => e.text)
2625
useEffect(() => {
2726
dispatch(initConsole(dbConnection!.id))
2827
}, [dbConnection])
@@ -50,7 +49,7 @@ const DBConsoleFragment = ({ }: DBConsolePropType) => {
5049
{output.map((block, idx) => {
5150
return <OutputBlock block={block} key={idx} />
5251
})}
53-
<PromptInputWithRef onChange={setInput} isActive={currentTab.isActive} nfocus={nfocus} confirmInput={confirmInput} commands={commands} pointer={pointer} setPointer={setPointer} />
52+
<PromptInputWithRef onChange={setInput} isActive={currentTab.isActive} nfocus={nfocus} confirmInput={confirmInput} history={history} />
5453
<span ref={consoleEndRef}></span>
5554
</div>
5655
}
@@ -66,6 +65,7 @@ const OutputBlock = ({ block }: any) => {
6665
const PromptInputWithRef = (props: any) => {
6766
const defaultValue = useRef("")
6867
const inputRef = useRef<HTMLParagraphElement>(null)
68+
const [pointer, setPointer] = useState<number>(-1)
6969
useEffect(() => {
7070
if (props.isActive) {
7171
inputRef.current?.focus()
@@ -78,8 +78,8 @@ const PromptInputWithRef = (props: any) => {
7878
}
7979
}
8080

81-
const setInputRef = ( cmd : string) => {
82-
if(inputRef.current !== null){
81+
const setInputRef = (cmd: string) => {
82+
if (inputRef.current !== null) {
8383
inputRef.current.textContent = cmd;
8484
}
8585
}
@@ -89,14 +89,30 @@ const PromptInputWithRef = (props: any) => {
8989
if (inputRef.current) {
9090
inputRef.current.innerText = ""
9191
}
92+
setPointer(-1)
9293
}
93-
if ( event.key.toLocaleLowerCase() === 'arrowup') {
94-
props.setPointer( () => ((props.pointer + props.commands.length -1 ) % props.commands.length))
95-
setInputRef(props.commands.at(props.pointer)?.text)
94+
const updateInputFromPointer = (newPointer: number) => {
95+
let text = props.history.at(props.history.length - 1 - newPointer)
96+
if (!text) {
97+
text = ""
98+
}
99+
setInputRef(text)
100+
}
101+
if (event.key.toLocaleLowerCase() === 'arrowup') {
102+
if (pointer !== props.history.length - 1) {
103+
setPointer(() => (pointer + 1))
104+
updateInputFromPointer(pointer + 1)
105+
}
96106
}
97-
if ( event.key.toLocaleLowerCase() === 'arrowdown'){
98-
props.setPointer( () => ((props.pointer + 1 ) % props.commands.length))
99-
setInputRef(props.commands.at(props.pointer)?.text)
107+
if (event.key.toLocaleLowerCase() === 'arrowdown') {
108+
let newPointer
109+
if (pointer < 0) {
110+
newPointer = -1
111+
} else {
112+
newPointer = pointer - 1
113+
}
114+
setPointer(newPointer)
115+
updateInputFromPointer(newPointer)
100116
}
101117
}
102118

frontend/desktop/src/redux/consoleSlice.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export const runConsoleCmd = createAsyncThunk(
2222
async (payload: { dbConnId: string, cmdString: string }, { rejectWithValue, getState }: any) => {
2323
const dbConnectionId = payload.dbConnId
2424
const cmdString = payload.cmdString
25+
if (cmdString === "") {
26+
return rejectWithValue("empty command")
27+
}
2528
const result = await eventService.runConsoleCommand(dbConnectionId, cmdString)
2629
if (result.success) {
2730
return {

frontend/server/src/components/dbfragments/console.tsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ const DBConsoleFragment = ({ }: DBConsolePropType) => {
2121
const output = useAppSelector(selectBlocks)
2222
const [input, setInput] = useState("")
2323
const [nfocus, setFocus] = useState<number>(0)
24-
const commands = output.filter( e => e.cmd === true)
25-
const [pointer, setPointer] = useState<number>(commands.length-1)
26-
24+
const history = output.filter(e => e.cmd).filter(e => e.text !== "").map(e => e.text)
2725
useEffect(() => {
2826
dispatch(initConsole(dbConnection!.id))
2927
}, [dbConnection])
@@ -51,7 +49,7 @@ const DBConsoleFragment = ({ }: DBConsolePropType) => {
5149
{output.map((block, idx) => {
5250
return <OutputBlock block={block} key={idx} />
5351
})}
54-
<PromptInputWithRef onChange={setInput} isActive={currentTab.isActive} nfocus={nfocus} confirmInput={confirmInput} commands={commands} pointer={pointer} setPointer={setPointer} />
52+
<PromptInputWithRef onChange={setInput} isActive={currentTab.isActive} nfocus={nfocus} confirmInput={confirmInput} history={history} />
5553
<span ref={consoleEndRef}></span>
5654
</div>
5755
}
@@ -68,7 +66,7 @@ const PromptInputWithRef = (props: any) => {
6866

6967
const defaultValue = useRef("")
7068
const inputRef = useRef<HTMLParagraphElement>(null)
71-
69+
const [pointer, setPointer] = useState<number>(-1)
7270
useEffect(() => {
7371
if (props.isActive) {
7472
inputRef.current?.focus()
@@ -81,8 +79,8 @@ const PromptInputWithRef = (props: any) => {
8179
}
8280
}
8381

84-
const setInputRef = ( cmd : string) => {
85-
if(inputRef.current !== null){
82+
const setInputRef = (cmd: string) => {
83+
if (inputRef.current !== null) {
8684
inputRef.current.textContent = cmd;
8785
}
8886
}
@@ -92,14 +90,30 @@ const PromptInputWithRef = (props: any) => {
9290
if (inputRef.current) {
9391
inputRef.current.innerText = ""
9492
}
93+
setPointer(-1)
94+
}
95+
const updateInputFromPointer = (newPointer: number) => {
96+
let text = props.history.at(props.history.length - 1 - newPointer)
97+
if (!text) {
98+
text = ""
99+
}
100+
setInputRef(text)
95101
}
96-
if ( event.key.toLocaleLowerCase() === 'arrowup') {
97-
props.setPointer( () => ((props.pointer + props.commands.length -1 ) % props.commands.length))
98-
setInputRef(props.commands.at(props.pointer)?.text)
102+
if (event.key.toLocaleLowerCase() === 'arrowup') {
103+
if (pointer !== props.history.length - 1) {
104+
setPointer(() => (pointer + 1))
105+
updateInputFromPointer(pointer + 1)
106+
}
99107
}
100-
if ( event.key.toLocaleLowerCase() === 'arrowdown'){
101-
props.setPointer( () => ((props.pointer + 1 ) % props.commands.length))
102-
setInputRef(props.commands.at(props.pointer)?.text)
108+
if (event.key.toLocaleLowerCase() === 'arrowdown') {
109+
let newPointer
110+
if (pointer < 0) {
111+
newPointer = -1
112+
} else {
113+
newPointer = pointer - 1
114+
}
115+
setPointer(newPointer)
116+
updateInputFromPointer(newPointer)
103117
}
104118
}
105119

0 commit comments

Comments
 (0)