From f7bb218403055286fed946e3114dff81fd2f5a87 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 19:37:05 +0100 Subject: [PATCH] Use reader and writer to make shell extensible --- main.go | 2 +- shell.go | 32 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 00067d6..6cac199 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { showQueue() } if settings["interactive"] == "true" { - interactiveShell() + interactiveShell(os.Stdin, os.Stdout) } } diff --git a/shell.go b/shell.go index eb8ee33..968caff 100644 --- a/shell.go +++ b/shell.go @@ -3,15 +3,17 @@ package main import ( "bufio" "fmt" + "io" "os" "strings" ) -func interactiveShell() { - fmt.Println("Let's try an interactive shell!") - scanner := bufio.NewScanner(os.Stdin) +func interactiveShell(reader io.Reader, writer io.Writer) { + fmt.Fprintln(writer, "Let's try an interactive shell!") + scanner := bufio.NewScanner(reader) for { - fmt.Print("> ") + var quitShell bool = false + fmt.Fprint(writer, "> ") if !scanner.Scan() { break } @@ -21,14 +23,26 @@ func interactiveShell() { continue } cmd := textFields[0] - fmt.Printf("Read input: %q\n", textFields) + fmt.Fprintf(writer, "DEBUG - Read input: %q\n", textFields) switch cmd { - + case "help", "?": + printHelp(os.Stdout) + case "exit", "quit": + quitShell = true default: - fmt.Printf("Unknown command '%s'\n", cmd) + fmt.Fprintf(writer, "Unknown command '%s'\n", cmd) + fmt.Fprintln(writer, "Use 'help' to display help command") + } + if quitShell == true { + break } - } - fmt.Println("\nGoodbye!\n") + fmt.Fprintln(writer, "\nGoodbye!\n") +} + +func printHelp(writer io.Writer) { + fmt.Fprintln(writer, "Available commands:") + fmt.Fprintln(writer, "?, help: display this output") + fmt.Fprintln(writer, "exit, quit: quit this program") }