you can cycle through your command history

This commit is contained in:
Brazly
2026-02-15 19:55:05 +00:00
parent a66f4c95cc
commit a7737b10e0
2 changed files with 29 additions and 0 deletions

View File

@@ -39,6 +39,12 @@ func _input(event: InputEvent) -> void:
KEY_ENTER: KEY_ENTER:
terminal.EnterCommand() terminal.EnterCommand()
KEY_UP:
terminal.NavigateHistory(1)
KEY_DOWN:
terminal.NavigateHistory(-1)
if event.unicode > 31: if event.unicode > 31:
var character = char(event.unicode) var character = char(event.unicode)

View File

@@ -8,6 +8,9 @@ class_name TerminalControls
@onready var scroll: ScrollContainer = $MarginContainer/ScrollContainer @onready var scroll: ScrollContainer = $MarginContainer/ScrollContainer
@export var terminalLine: RichTextLabel @export var terminalLine: RichTextLabel
var terminalHistory: Array[String] = []
var historyIndex: int = -1
var command: String var command: String
var directory: String = "~/" var directory: String = "~/"
var fileSystem: Dictionary = { var fileSystem: Dictionary = {
@@ -63,6 +66,10 @@ func EnterCommand() -> void:
if historyContainer.get_child_count() <24: if historyContainer.get_child_count() <24:
CreateHistoryEntry(fullText) CreateHistoryEntry(fullText)
if userInput != "":
terminalHistory.append(userInput)
historyIndex = 0
match parts[0]: match parts[0]:
"ls", "list": "ls", "list":
@@ -117,6 +124,19 @@ func EnterCommand() -> void:
GetBottomScroll() GetBottomScroll()
# --- History and FileSystem Helpers --- # --- History and FileSystem Helpers ---
func NavigateHistory(direction: int):
if terminalHistory.is_empty():
return
historyIndex += direction
historyIndex = clamp(historyIndex, 0, terminalHistory.size() - 1)
var historyCommand = terminalHistory[historyIndex]
terminalLine.text = "user@work "+ directory + " " + historyCommand
UpdateCaretPos()
func CreateHistoryEntry(content: String) -> void: func CreateHistoryEntry(content: String) -> void:
var rtl = RichTextLabel.new() var rtl = RichTextLabel.new()
@@ -130,6 +150,9 @@ func CreateHistoryEntry(content: String) -> void:
historyContainer.add_child(rtl) historyContainer.add_child(rtl)
historyContainer.move_child(rtl, historyContainer.get_child_count() - 2) historyContainer.move_child(rtl, historyContainer.get_child_count() - 2)
func ResolvePath(current: String, target: String) -> String: func ResolvePath(current: String, target: String) -> String:
if target.begins_with("~/"): if target.begins_with("~/"):
return target if target.ends_with("/") else target + "/" return target if target.ends_with("/") else target + "/"