]> git.0d.be Git - barnard.git/blob - ui.go
move barnard main package to package root
[barnard.git] / ui.go
1 package main
2
3 import (
4         "fmt"
5         "strings"
6         "time"
7
8         "github.com/kennygrant/sanitize"
9         "layeh.com/barnard/uiterm"
10         "layeh.com/gumble/gumble"
11 )
12
13 const (
14         uiViewLogo        = "logo"
15         uiViewTop         = "top"
16         uiViewStatus      = "status"
17         uiViewInput       = "input"
18         uiViewInputStatus = "inputstatus"
19         uiViewOutput      = "output"
20         uiViewTree        = "tree"
21 )
22
23 func esc(str string) string {
24         return sanitize.HTML(str)
25 }
26
27 func (b *Barnard) UpdateInputStatus(status string) {
28         b.UiInputStatus.Text = status
29         b.UiTree.Rebuild()
30         b.Ui.Refresh()
31 }
32
33 func (b *Barnard) AddOutputLine(line string) {
34         now := time.Now()
35         b.UiOutput.AddLine(fmt.Sprintf("[%02d:%02d:%02d] %s", now.Hour(), now.Minute(), now.Second(), line))
36 }
37
38 func (b *Barnard) AddOutputMessage(sender *gumble.User, message string) {
39         if sender == nil {
40                 b.AddOutputLine(message)
41         } else {
42                 b.AddOutputLine(fmt.Sprintf("%s: %s", sender.Name, strings.TrimSpace(esc(message))))
43         }
44 }
45
46 func (b *Barnard) OnVoiceToggle(ui *uiterm.Ui, key uiterm.Key) {
47         if b.UiStatus.Text == "  Tx  " {
48                 b.UiStatus.Text = " Idle "
49                 b.UiStatus.Fg = uiterm.ColorBlack
50                 b.UiStatus.Bg = uiterm.ColorWhite
51                 b.Stream.StopSource()
52         } else {
53                 b.UiStatus.Fg = uiterm.ColorWhite | uiterm.AttrBold
54                 b.UiStatus.Bg = uiterm.ColorRed
55                 b.UiStatus.Text = "  Tx  "
56                 b.Stream.StartSource()
57         }
58         ui.Refresh()
59 }
60
61 func (b *Barnard) OnQuitPress(ui *uiterm.Ui, key uiterm.Key) {
62         b.Client.Disconnect()
63         b.Ui.Close()
64 }
65
66 func (b *Barnard) OnClearPress(ui *uiterm.Ui, key uiterm.Key) {
67         b.UiOutput.Clear()
68 }
69
70 func (b *Barnard) OnScrollOutputUp(ui *uiterm.Ui, key uiterm.Key) {
71         b.UiOutput.ScrollUp()
72 }
73
74 func (b *Barnard) OnScrollOutputDown(ui *uiterm.Ui, key uiterm.Key) {
75         b.UiOutput.ScrollDown()
76 }
77
78 func (b *Barnard) OnScrollOutputTop(ui *uiterm.Ui, key uiterm.Key) {
79         b.UiOutput.ScrollTop()
80 }
81
82 func (b *Barnard) OnScrollOutputBottom(ui *uiterm.Ui, key uiterm.Key) {
83         b.UiOutput.ScrollBottom()
84 }
85
86 func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) {
87         active := b.Ui.Active()
88         if active == uiViewInput {
89                 b.Ui.SetActive(uiViewTree)
90         } else if active == uiViewTree {
91                 b.Ui.SetActive(uiViewInput)
92         }
93 }
94
95 func (b *Barnard) OnTextInput(ui *uiterm.Ui, textbox *uiterm.Textbox, text string) {
96         if text == "" {
97                 return
98         }
99         if b.Client != nil && b.Client.Self != nil {
100                 b.Client.Self.Channel.Send(text, false)
101                 b.AddOutputMessage(b.Client.Self, text)
102         }
103 }
104
105 func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) {
106         ui.Add(uiViewLogo, &uiterm.Label{
107                 Text: " barnard ",
108                 Fg:   uiterm.ColorWhite | uiterm.AttrBold,
109                 Bg:   uiterm.ColorMagenta,
110         })
111
112         ui.Add(uiViewTop, &uiterm.Label{
113                 Fg: uiterm.ColorWhite,
114                 Bg: uiterm.ColorBlue,
115         })
116
117         b.UiStatus = uiterm.Label{
118                 Text: " Idle ",
119                 Fg:   uiterm.ColorBlack,
120                 Bg:   uiterm.ColorWhite,
121         }
122         ui.Add(uiViewStatus, &b.UiStatus)
123
124         b.UiInput = uiterm.Textbox{
125                 Fg:    uiterm.ColorWhite,
126                 Bg:    uiterm.ColorBlack,
127                 Input: b.OnTextInput,
128         }
129         ui.Add(uiViewInput, &b.UiInput)
130
131         b.UiInputStatus = uiterm.Label{
132                 Fg: uiterm.ColorBlack,
133                 Bg: uiterm.ColorWhite,
134         }
135         ui.Add(uiViewInputStatus, &b.UiInputStatus)
136
137         b.UiOutput = uiterm.Textview{
138                 Fg: uiterm.ColorWhite,
139                 Bg: uiterm.ColorBlack,
140         }
141         ui.Add(uiViewOutput, &b.UiOutput)
142
143         b.UiTree = uiterm.Tree{
144                 Generator: b.TreeItem,
145                 Listener:  b.TreeItemSelect,
146                 Fg:        uiterm.ColorWhite,
147                 Bg:        uiterm.ColorBlack,
148         }
149         ui.Add(uiViewTree, &b.UiTree)
150
151         b.Ui.AddKeyListener(b.OnFocusPress, uiterm.KeyTab)
152         b.Ui.AddKeyListener(b.OnVoiceToggle, uiterm.KeyF1)
153         b.Ui.AddKeyListener(b.OnQuitPress, uiterm.KeyF10)
154         b.Ui.AddKeyListener(b.OnClearPress, uiterm.KeyCtrlL)
155         b.Ui.AddKeyListener(b.OnScrollOutputUp, uiterm.KeyPgup)
156         b.Ui.AddKeyListener(b.OnScrollOutputDown, uiterm.KeyPgdn)
157         b.Ui.AddKeyListener(b.OnScrollOutputTop, uiterm.KeyHome)
158         b.Ui.AddKeyListener(b.OnScrollOutputBottom, uiterm.KeyEnd)
159
160         b.start()
161 }
162
163 func (b *Barnard) OnUiResize(ui *uiterm.Ui, width, height int) {
164         ui.SetBounds(uiViewLogo, 0, 0, 9, 1)
165         ui.SetBounds(uiViewTop, 9, 0, width-6, 1)
166         ui.SetBounds(uiViewStatus, width-6, 0, width, 1)
167         ui.SetBounds(uiViewInput, 0, height-1, width, height)
168         ui.SetBounds(uiViewInputStatus, 0, height-2, width, height-1)
169         ui.SetBounds(uiViewOutput, 0, 1, width-20, height-2)
170         ui.SetBounds(uiViewTree, width-20, 1, width, height-2)
171 }