-
Notifications
You must be signed in to change notification settings - Fork 2
/
hotkey.go
148 lines (124 loc) · 4.12 KB
/
hotkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tru
import (
"bufio"
"fmt"
"os"
"github.com/teonet-go/tru/hotkey"
"github.com/teonet-go/tru/teolog"
"github.com/teonet-go/tru/term"
)
// hotkeyMenuState is hotkey menu state
type hotkeyMenuState struct {
logLevel teolog.LogLevel
}
// stopLog stop logger while next key pressed
func (s *hotkeyMenuState) stopLog(h *hotkey.Hotkey) teolog.LogLevel {
state := h.State().(*hotkeyMenuState)
state.logLevel = log.Level() // get current log level
log.SetLevel(0) // stop log
h.NextKeyAction(func(ch []byte) {
log.SetLevel(state.logLevel)
})
return state.logLevel
}
// restoreLog continue stopped by stopLog() logger with saved log level
func (s *hotkeyMenuState) restoreLog(h *hotkey.Hotkey) teolog.LogLevel {
state := h.State().(*hotkeyMenuState)
log.SetLevel(state.logLevel)
return state.logLevel
}
// conitnueLog continue stopped by stopLog() logger with current log level
func (s *hotkeyMenuState) continueLog(h *hotkey.Hotkey) teolog.LogLevel {
h.NextKeyAction(nil)
return log.Level()
}
// newHotkey create and start run tru hokey menu
func (tru *Tru) newHotkey() *hotkey.Hotkey {
quit := func(h *hotkey.Hotkey) {
h.Stop()
fmt.Println("Quit from application...")
tru.Close()
os.Exit(0)
}
state := new(hotkeyMenuState)
h := hotkey.New().SetState(state).Run().
// Add action function which executes when unknown key pressed
AddUnknown(func(h *hotkey.Hotkey, ch []byte) {
fmt.Printf(" unknown key %v pressed, use 'h' key to get help\n", ch)
}).
// When Enter key pressed than print empty line
Add([]byte{13}, "", func(h *hotkey.Hotkey) {
fmt.Println()
}).
// Help print usage test
Add([]string{"h", "?"}, "show this help screen", func(h *hotkey.Hotkey) {
h.State().(*hotkeyMenuState).stopLog(h)
fmt.Print(tru.Logo("", "")) // Logo
fmt.Print(h) // Menu
fmt.Println("\n select menu item hotkey or press any key to continue...") // Footer
}).
// Show or Hide tru statistic
Add("u", "show/hide tru statistic", func(h *hotkey.Hotkey) {
if !tru.StatisticPrintRunning() {
tru.StatisticPrint()
fmt.Println(" print statistic started...")
} else {
tru.StatisticPrintStop()
fmt.Println(" print statistic stopped...")
}
}).
// Show or Hide tru statistic minilog
Add("U", "show/hide tru statistic minilog", func(h *hotkey.Hotkey) {
var s string
if tru.StatisticMinilog() {
s = "show"
} else {
s = "hide"
}
fmt.Printf(" tru statistic minilog %s\n", s)
}).
// Switch log level
Add("l", "switch logger level", func(h *hotkey.Hotkey) {
log.SwitchLevel()
fmt.Println(" logger set to level:", log)
}).
// Print current logger level
Add("L", "print current logger level", func(h *hotkey.Hotkey) {
level := h.State().(*hotkeyMenuState).stopLog(h)
fmt.Println(" current logger level:", level.String())
}).
// Set log level
Add(hotkey.KeyCode{Code: []byte{12}, Name: "Ctrl+L"},
"set new logger level", func(h *hotkey.Hotkey) {
level := h.State().(*hotkeyMenuState).stopLog(h)
fmt.Println(log.Levels())
fmt.Println(" current logger level:", level.String())
reader := bufio.NewReader(os.Stdin)
fmt.Print(" enter new level ~> ")
text, _ := reader.ReadString('\n')
log.SetLevel(text)
h.State().(*hotkeyMenuState).continueLog(h)
}).
// Set log filter
Add("f", "set logger filter", func(h *hotkey.Hotkey) {
level := h.State().(*hotkeyMenuState).stopLog(h)
fmt.Println(" current log level: ", level.String())
fmt.Println(" current log filter:", log.Filter())
reader := bufio.NewReader(os.Stdin)
fmt.Print(" enter new filter ~> ")
text, _ := reader.ReadString('\n')
log.SetFilter(text)
h.State().(*hotkeyMenuState).restoreLog(h)
}).
// Quit form hotkey menu
Add(hotkey.KeyCode{Code: term.Keys.CtrlC(), Name: "Ctrl+C"},
"stop this hotkey menu", func(h *hotkey.Hotkey) {
h.Stop()
fmt.Println("Hotkey menu stopped...")
},
).
// Quit from application
Add("q", "quit from application", quit)
fmt.Println("Hotkey menu startted, press 'h' to help...")
return h
}