Use reader and writer to make shell extensible
This commit is contained in:
parent
b471f6a914
commit
f7bb218403
2
main.go
2
main.go
|
@ -21,7 +21,7 @@ func main() {
|
|||
showQueue()
|
||||
}
|
||||
if settings["interactive"] == "true" {
|
||||
interactiveShell()
|
||||
interactiveShell(os.Stdin, os.Stdout)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
32
shell.go
32
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.Fprintln(writer, "\nGoodbye!\n")
|
||||
}
|
||||
|
||||
}
|
||||
fmt.Println("\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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue