From 8522596998d08c317ff8dd6e183f4ed99cd477af Mon Sep 17 00:00:00 2001 From: Joop Kiefte Date: Thu, 28 Sep 2023 02:19:53 +0100 Subject: Improve readline --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 1a5a72b..d7acac1 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os/exec" "strings" "sync" + "bufio" ) var once = sync.Once{} @@ -56,7 +57,7 @@ Running debby with any other arguments will run it as a dolt command.` if query == "" { // Request query fmt.Print("Enter your query: ") - fmt.Scanln(&query) + query = Readline() } // Table name will be empty. Run query @@ -65,7 +66,7 @@ Running debby with any other arguments will run it as a dolt command.` if table == "" { // Request table name fmt.Print("Enter your table name: ") - fmt.Scanln(&table) + table = Readline() } // Run query @@ -74,8 +75,7 @@ Running debby with any other arguments will run it as a dolt command.` if len(args) == 0 { // Request command. Make dolt bold fmt.Print("\033[1mdolt\033[0m ") - argstr := "" - fmt.Scanln(&argstr) + argstr := Readline() args = strings.Split(argstr, " ") } @@ -106,7 +106,8 @@ Running debby with any other arguments will run it as a dolt command.` fmt.Print("- Enter your choice: ") // Read user input - fmt.Scanln(&choice) + choicestr := Readline() + choice = int(choicestr[0] - '0') // Reset all variables except choice table = "" @@ -115,6 +116,13 @@ Running debby with any other arguments will run it as a dolt command.` } } +func Readline() string { + // Read a line from stdin + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + return text +} + func ExecuteDoltCommand(args ...string) { cmd := exec.Command("dolt", args...) cmd.Stdout = os.Stdout -- cgit v1.2.3-70-g09d2