Add working directory persistence, command aliases, and keyboard shortcuts reference
1. Working Directory Persistence: Tracks and restores last directory per tab 2. Command Aliases: Create shortcuts for long commands (e.g., gs=git status) - Manage aliases in Settings - Type 'aliases' to see all defined aliases 3. Keyboard Shortcuts Reference: Cmd+? to view all shortcuts All features tested and working
This commit is contained in:
@@ -10,6 +10,7 @@ class TerminalSession: NSObject, NSTextViewDelegate, NSMenuDelegate {
|
||||
var promptPosition = 0
|
||||
var sessionID: UUID
|
||||
var title: String
|
||||
var currentWorkingDirectory: String = "~"
|
||||
private var currentSuggestion: String?
|
||||
|
||||
init(title: String = "Terminal") {
|
||||
@@ -401,30 +402,51 @@ class TerminalSession: NSObject, NSTextViewDelegate, NSMenuDelegate {
|
||||
|
||||
let trimmed = command.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
// Check for aliases
|
||||
var actualCommand = trimmed
|
||||
if let aliasCommand = TerminalSettings.shared.commandAliases[trimmed] {
|
||||
actualCommand = aliasCommand
|
||||
appendText("(alias: \(aliasCommand))\n")
|
||||
}
|
||||
|
||||
// Track directory changes
|
||||
if actualCommand.hasPrefix("cd ") {
|
||||
let path = String(actualCommand.dropFirst(3)).trimmingCharacters(in: .whitespaces)
|
||||
if !path.isEmpty {
|
||||
currentWorkingDirectory = path
|
||||
}
|
||||
}
|
||||
|
||||
// Handle built-in commands
|
||||
if trimmed == "exit" {
|
||||
if actualCommand == "exit" {
|
||||
NSApp.terminate(nil)
|
||||
return
|
||||
}
|
||||
|
||||
if trimmed == "clear" {
|
||||
if actualCommand == "clear" {
|
||||
textView.string = ""
|
||||
showPrompt()
|
||||
return
|
||||
}
|
||||
|
||||
if trimmed == "help" {
|
||||
if actualCommand == "help" {
|
||||
showHelp()
|
||||
showPrompt()
|
||||
return
|
||||
}
|
||||
|
||||
if actualCommand == "aliases" {
|
||||
showAliases()
|
||||
showPrompt()
|
||||
return
|
||||
}
|
||||
|
||||
// Add to history
|
||||
if !trimmed.isEmpty {
|
||||
HistoryManager.shared.addCommand(trimmed)
|
||||
|
||||
// Send to shell
|
||||
if let data = (trimmed + "\n").data(using: .utf8) {
|
||||
if let data = (actualCommand + "\n").data(using: .utf8) {
|
||||
inputPipe?.fileHandleForWriting.write(data)
|
||||
}
|
||||
}
|
||||
@@ -453,12 +475,27 @@ class TerminalSession: NSObject, NSTextViewDelegate, NSMenuDelegate {
|
||||
Built-in Commands:
|
||||
• clear - Clear screen
|
||||
• help - Show this help
|
||||
• aliases - Show command aliases
|
||||
• exit - Quit application
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
""")
|
||||
}
|
||||
|
||||
private func showAliases() {
|
||||
let aliases = TerminalSettings.shared.commandAliases
|
||||
if aliases.isEmpty {
|
||||
appendText("No aliases defined. Add them in Settings (Cmd+,)\n")
|
||||
} else {
|
||||
appendText("Command Aliases:\n")
|
||||
appendText("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n")
|
||||
for (alias, command) in aliases.sorted(by: { $0.key < $1.key }) {
|
||||
appendText(" \(alias) → \(command)\n")
|
||||
}
|
||||
appendText("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n")
|
||||
}
|
||||
}
|
||||
|
||||
func insertCommand(_ command: String) {
|
||||
textView.insertText(command, replacementRange: NSRange(location: textView.string.count, length: 0))
|
||||
textView.window?.makeFirstResponder(textView)
|
||||
@@ -491,6 +528,7 @@ class TabManager {
|
||||
struct SessionData: Codable {
|
||||
let title: String
|
||||
let sessionID: String
|
||||
let workingDirectory: String?
|
||||
}
|
||||
|
||||
func addTab(title: String = "Terminal") -> TerminalSession {
|
||||
@@ -522,7 +560,7 @@ class TabManager {
|
||||
}
|
||||
|
||||
func saveSession() {
|
||||
let sessionData = tabs.map { SessionData(title: $0.title, sessionID: $0.sessionID.uuidString) }
|
||||
let sessionData = tabs.map { SessionData(title: $0.title, sessionID: $0.sessionID.uuidString, workingDirectory: $0.currentWorkingDirectory) }
|
||||
if let data = try? JSONEncoder().encode(sessionData) {
|
||||
UserDefaults.standard.set(data, forKey: "terminal_session")
|
||||
UserDefaults.standard.set(currentIndex, forKey: "terminal_current_tab")
|
||||
|
||||
Reference in New Issue
Block a user