diff --git a/pkg/tui/interface.go b/pkg/tui/interface.go index fe0ac4f..c7593ed 100644 --- a/pkg/tui/interface.go +++ b/pkg/tui/interface.go @@ -16,6 +16,11 @@ const ( Total ) +const helpMsg = `Available shortcuts: +t/T: print total size aggregated by server +s/S: get user input for server filtering +?: help` + type Tui struct { analyzer *analyze.Analyzer // displayRecord is used to remember latest accesses time by specific IP @@ -47,64 +52,7 @@ func (t *Tui) Run() { for { select { case k := <-t.inputChan: - switch k { - case 'S', 's': - t.noPrint.Store(true) - servers := a.GetCurrentServers() - if len(servers) == 1 { - serverFmt := "" - if len(servers[0]) > 0 { - serverFmt = " (" + servers[0] + ")" - } - fmt.Printf("Only one server%s is available.\n", serverFmt) - } else if len(servers) != 0 { - fmt.Println("Please give the server name you want to view. Enter to remove filtering.") - // Get all servers available - fmt.Println("Available servers:") - for _, s := range servers { - fmt.Println(s) - } - var input string - n, err := fmt.Scanln(&input) - if err != nil { - if n != 0 { - fmt.Println("Failed to get input:", err) - } else { - t.serverFilter = "" - } - } else { - found := false - for _, str := range servers { - if str == input { - found = true - t.serverFilter = input - break - } - } - if !found { - fmt.Println("Input does not match existing server.") - } - } - } - t.noPrint.Store(false) - case 'T', 't': - if t.mode == TopValues { - t.mode = Total - fmt.Println("Switched to showing total") - } else { - t.mode = TopValues - fmt.Println("Switched to showing top values") - } - case '?': - fmt.Println("Available shortcuts:") - fmt.Println("t/T: print total size aggregated by server") - fmt.Println("s/S: get user input for server filtering") - fmt.Println("?: help") - fmt.Println() - } - // This shall always run after input is handled. - // Don't write "continue" above! - go t.waitForOneByte() + t.handleInput(k) case <-t.refreshChan: if t.mode == TopValues { a.PrintTopValues(t.displayRecord, "size", t.serverFilter) @@ -115,3 +63,64 @@ func (t *Tui) Run() { } } } + +func (t *Tui) handleInput(key byte) { + switch key { + case 'S', 's': + t.handleS() + case 'T', 't': + if t.mode == TopValues { + t.mode = Total + fmt.Println("Switched to showing total") + } else { + t.mode = TopValues + fmt.Println("Switched to showing top values") + } + case '?': + fmt.Println(helpMsg) + fmt.Println() + } + go t.waitForOneByte() +} + +func (t *Tui) handleS() { + t.noPrint.Store(true) + defer t.noPrint.Store(false) + + servers := t.analyzer.GetCurrentServers() + if len(servers) == 1 { + serverFmt := "" + if len(servers[0]) > 0 { + serverFmt = " (" + servers[0] + ")" + } + fmt.Printf("Only one server%s is available.\n", serverFmt) + } else if len(servers) != 0 { + fmt.Println("Please give the server name you want to view. Enter to remove filtering.") + // Get all servers available + fmt.Println("Available servers:") + for _, s := range servers { + fmt.Println(s) + } + var input string + n, err := fmt.Scanln(&input) + if err != nil { + if n != 0 { + fmt.Println("Failed to get input:", err) + } else { + t.serverFilter = "" + } + } else { + found := false + for _, str := range servers { + if str == input { + found = true + t.serverFilter = input + break + } + } + if !found { + fmt.Println("Input does not match existing server.") + } + } + } +}