Skip to content

Commit

Permalink
Split long functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Sep 1, 2024
1 parent fcfdf64 commit c79871c
Showing 1 changed file with 67 additions and 58 deletions.
125 changes: 67 additions & 58 deletions pkg/tui/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.")
}
}
}
}

0 comments on commit c79871c

Please sign in to comment.