feat: save history to file

pull/15/head
TorchedSammy 2021-04-01 22:12:03 -04:00
parent 23f6986123
commit a0bc8e026a
1 changed files with 7 additions and 2 deletions

View File

@ -30,6 +30,7 @@ var commands = map[string]bool{}
// Command aliases // Command aliases
var aliases = map[string]string{} var aliases = map[string]string{}
var bait hooks.Bait var bait hooks.Bait
var homedir string
func main() { func main() {
parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
@ -58,7 +59,7 @@ func main() {
// Set $SHELL if the user wants to // Set $SHELL if the user wants to
if *setshflag { os.Setenv("SHELL", os.Args[0]) } if *setshflag { os.Setenv("SHELL", os.Args[0]) }
homedir, _ := os.UserHomeDir() homedir, _ = os.UserHomeDir()
// If user's config doesn't exixt, // If user's config doesn't exixt,
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) { if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
// Read default from current directory // Read default from current directory
@ -87,6 +88,7 @@ func main() {
LuaInit() LuaInit()
readline.Completer = readline.FilenameCompleter readline.Completer = readline.FilenameCompleter
readline.LoadHistory(homedir + "/.hilbish-history")
for { for {
cmdString, err := readline.String(fmtPrompt()) cmdString, err := readline.String(fmtPrompt())
@ -108,6 +110,7 @@ func main() {
if err == nil { if err == nil {
// If it succeeds, add to history and prompt again // If it succeeds, add to history and prompt again
readline.AddHistory(cmdString) readline.AddHistory(cmdString)
readline.SaveHistory(homedir + "/.hilbish-history")
bait.Em.Emit("command.success", nil) bait.Em.Emit("command.success", nil)
continue continue
} }
@ -140,6 +143,7 @@ func main() {
panic(err) panic(err)
} }
readline.AddHistory(cmdString) readline.AddHistory(cmdString)
readline.SaveHistory(homedir + "/.hilbish-history")
continue continue
} }
@ -223,7 +227,7 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
return false return false
} }
return true return true
} }
func splitInput(input string) ([]string, string) { func splitInput(input string) ([]string, string) {
@ -277,6 +281,7 @@ func splitInput(input string) ([]string, string) {
} }
readline.AddHistory(input) readline.AddHistory(input)
readline.SaveHistory(homedir + "/.hilbish-history")
return cmdArgs, cmdstr.String() return cmdArgs, cmdstr.String()
} }