49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func interactiveShell(reader io.Reader, writer io.Writer) {
|
|
fmt.Fprintln(writer, "Let's try an interactive shell!")
|
|
scanner := bufio.NewScanner(reader)
|
|
for {
|
|
var quitShell bool = false
|
|
fmt.Fprint(writer, "> ")
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
text := strings.Trim(scanner.Text(), " ")
|
|
textFields := strings.Fields(text)
|
|
if len(textFields) == 0 {
|
|
continue
|
|
}
|
|
cmd := textFields[0]
|
|
fmt.Fprintf(writer, "DEBUG - Read input: %q\n", textFields)
|
|
|
|
switch cmd {
|
|
case "help", "?":
|
|
printHelp(os.Stdout)
|
|
case "exit", "quit":
|
|
quitShell = true
|
|
default:
|
|
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")
|
|
}
|
|
|
|
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")
|
|
}
|