]> Sergey Matveev's repositories - vors.git/blob - cmd/client/gui.go
34f1370688108dbb38d677cfda96a3f7e9383bde7ba5be472656d4e02989dd74
[vors.git] / cmd / client / gui.go
1 // VoRS -- Vo(IP) Really Simple
2 // Copyright (C) 2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU Affero General Public License for more details.
12 //
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "fmt"
20         "log"
21         "sort"
22
23         "github.com/jroimartin/gocui"
24         vors "go.stargrave.org/vors/v3/internal"
25 )
26
27 var (
28         GUI         *gocui.Gui
29         GUIReady    bool
30         GUIReadyC   = make(chan struct{})
31         GUIMaxY     int
32         CurrentView = 0
33 )
34
35 func tabHandle(gui *gocui.Gui, v *gocui.View) error {
36         sids := make([]int, 0, len(Streams)+1)
37         sids = append(sids, -1)
38         for sid := range Streams {
39                 sids = append(sids, int(sid))
40         }
41         sort.Ints(sids)
42         if CurrentView+1 >= len(sids) {
43                 CurrentView = 0
44         } else {
45                 CurrentView++
46         }
47         if CurrentView == 0 {
48                 gui.SetCurrentView("chat")
49         } else {
50                 gui.SetCurrentView(Streams[byte(sids[CurrentView])].name)
51         }
52         return nil
53 }
54
55 func chatEnter(gui *gocui.Gui, v *gocui.View) error {
56         msg := v.Buffer()
57         if len(msg) > 1 {
58                 Ctrl <- vors.ArgsEncode([]byte(vors.CmdChat), []byte(msg[:len(msg)-1]))
59                 log.Println("me:", msg[:len(msg)-1])
60         }
61         v.Clear()
62         v.SetCursor(0, 0)
63         return nil
64 }
65
66 func guiLayout(gui *gocui.Gui) error {
67         var maxX int
68         maxX, GUIMaxY = gui.Size()
69         prevY := 0
70         v, err := gui.SetView("logs", 0, prevY, maxX-1, prevY+7+2)
71         prevY += 7 + 2
72         if err != nil {
73                 if err != gocui.ErrUnknownView {
74                         return err
75                 }
76                 v.Title = fmt.Sprintf("Logs room=%s", *Room)
77                 v.Autoscroll = true
78                 v.Wrap = true
79         }
80         v, err = gui.SetView("chat", 0, prevY, maxX-1, prevY+2)
81         if err != nil {
82                 if err != gocui.ErrUnknownView {
83                         return err
84                 }
85                 v.Title = "chat"
86                 v.Editable = true
87                 if err := GUI.SetKeybinding(
88                         "chat", gocui.KeyEnter, gocui.ModNone, chatEnter,
89                 ); err != nil {
90                         return err
91                 }
92         }
93         prevY += 3
94         sids := make([]int, 0, len(Streams))
95         for sid := range Streams {
96                 sids = append(sids, int(sid))
97         }
98         sort.Ints(sids)
99         for _, sid := range sids {
100                 stream := Streams[byte(sid)]
101                 v, err = gui.SetView(stream.name, 0, prevY, maxX/2-1, prevY+2)
102                 if err != nil {
103                         if err != gocui.ErrUnknownView {
104                                 return err
105                         }
106                         if stream.name == *Name {
107                                 v.Title = ">" + stream.name + "<"
108                         } else {
109                                 v.Title = stream.name
110                                 if err := GUI.SetKeybinding(
111                                         stream.name, gocui.KeyEnter, gocui.ModNone, func(*gocui.Gui, *gocui.View) error {
112                                                 stream.silenced = !stream.silenced
113                                                 return nil
114                                         },
115                                 ); err != nil {
116                                         return err
117                                 }
118                         }
119                 }
120                 _, err = gui.SetView(stream.name+"-vol", maxX/2, prevY, maxX-1, prevY+2)
121                 prevY += 3
122                 if err != nil {
123                         if err != gocui.ErrUnknownView {
124                                 return err
125                         }
126                 }
127         }
128         if !GUIReady {
129                 close(GUIReadyC)
130                 GUIReady = true
131                 gui.SetCurrentView("chat")
132         }
133         return nil
134 }