diff --git a/CodeApp/Managers/TerminalInstance.swift b/CodeApp/Managers/TerminalInstance.swift index 015318310..9cbec1d08 100644 --- a/CodeApp/Managers/TerminalInstance.swift +++ b/CodeApp/Managers/TerminalInstance.swift @@ -319,6 +319,43 @@ class TerminalInstance: NSObject, WKScriptMessageHandler, WKNavigationDelegate { } openSharedFilesApp(urlString: dir) self.readLine() + case let x where x.hasPrefix("history"): + let args = x.components(separatedBy: " ") + if args.count == 1 { + // Display history with index in 2 tabbed columns (like ZSH) + // ZSH only displays the last 25 commands by default + // Format: index\t\tcommand + let script = """ + var historyEntries = localEcho.history.entries; + if (historyEntries.length === 0) { + localEcho.println(''); + } else { + // Display only the last 25 commands (ZSH behavior) + var startIndex = Math.max(0, historyEntries.length - 25); + for (var i = startIndex; i < historyEntries.length; i++) { + localEcho.println((i + 1) + '\\t\\t' + historyEntries[i]); + } + } + """ + executeScript(script) + self.readLine() + } else if args.count == 2 && args[1] == "-p" { + // Clear command history stored in local-echo.js (ZSH behavior) + // This accesses the HistoryController instance (localEcho.history) + // to reset both the entries array and cursor position + executeScript("localEcho.history.entries = []; localEcho.history.cursor = 0;") + self.readLine() + } else if args.count == 2 && (args[1] == "-h" || args[1] == "--help") { + // Display usage information + let usageMessage = "Usage: history [option]\\nOptions:\\n (no option) Display command history with index\\n -p Clear command history\\n -h, --help Display this help message" + executeScript("localEcho.println(`\(usageMessage)`);") + self.readLine() + } else { + // Invalid argument - show usage guidance + let errorMessage = "history: invalid option\\nTry 'history --help' for more information." + executeScript("localEcho.println(`\(errorMessage)`);") + self.readLine() + } default: let command = result["Input"] as! String // guard command.count > 0 else {