-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursesApplication.go
75 lines (69 loc) · 1.69 KB
/
cursesApplication.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
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"github.com/marcioAlmada/goremote/upnp"
"github.com/tncardoso/gocurses"
)
type cursesApplication struct {
screen *gocurses.Window
application
}
func newCursesApplication() cursesApplication {
return cursesApplication{}
}
func (cursesApplication) PromptPinCode() (str string) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter provided pin code: ")
str, err := reader.ReadString('\n')
nuke(err, "Could not read input.")
str = strings.TrimRight(str, "\n\r")
return
}
// Run executes the command line curses application
func (app cursesApplication) Run(client upnp.Client, keyMap, altKeyMap keyMap) (e error) {
c := make(chan os.Signal, 1) // handle process termination
signal.Notify(c, os.Interrupt)
go func() {
<-c
gocurses.End()
os.Exit(1)
}()
// add alternative key bidings
keyMap.Merge(altKeyMap)
// env config
os.Setenv("ESCDELAY", "0")
// curses config
gocurses.CursSet(0)
app.screen = gocurses.Initscr()
defer gocurses.End()
app.screen.Keypad(true) // enable keypad support
app.screen.Scrollok(true) // infinite screen
gocurses.Noecho() // avoid char leak of unmapped keys
// run the REPL!
app.screen.Addstr("> Ready!")
for {
ch := app.screen.Getch()
if 4 == ch { // handles CTRL+D
break
}
go func() {
if key, ok := keyMap[ch]; ok {
if _, _, e := client.SendCommand(key.Command); e == nil {
if _, windowx := app.screen.Getmaxyx(); windowx > 30 {
app.screen.Addstr("\n", client.IP, "> ", key.Command)
} else {
app.screen.Addstr("\n> ", key.Command)
}
} else {
app.screen.Addstr("\n", "ERROR: ", e)
}
app.screen.Refresh()
}
}()
}
return
}