]> Sergey Matveev's repositories - vors.git/blob - cmd/server/gui.go
0e488e8977a420a654040eb7d1114c86a180c9fd98dfde0ce0a6e3016b81d743
[vors.git] / cmd / server / 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 General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "flag"
20         "os"
21         "sort"
22         "time"
23
24         "github.com/jroimartin/gocui"
25 )
26
27 var (
28         NoGUI     = flag.Bool("tuiless", false, "No fancy TUI, only logs")
29         GUI       *gocui.Gui
30         GUIReady  bool
31         GUIReadyC = make(chan struct{})
32 )
33
34 func guiQuit(g *gocui.Gui, v *gocui.View) error {
35         go func() {
36                 time.Sleep(100 * time.Millisecond)
37                 os.Exit(0)
38         }()
39         return gocui.ErrQuit
40 }
41
42 func guiLayout(gui *gocui.Gui) error {
43         maxX, _ := gui.Size()
44         prevY := 0
45         v, err := gui.SetView("logs", 0, prevY, maxX-1, prevY+10+2)
46         prevY += 10 + 2
47         if err != nil {
48                 if err != gocui.ErrUnknownView {
49                         return err
50                 }
51                 v.Title = "Logs"
52                 v.Autoscroll = true
53         }
54         sids := make([]int, 0, len(Peers))
55         for sid := range Peers {
56                 sids = append(sids, int(sid))
57         }
58         sort.Ints(sids)
59         for _, sid := range sids {
60                 peer := Peers[byte(sid)]
61                 v, err := gui.SetView(peer.name, 0, prevY, maxX-1, prevY+2)
62                 prevY += 3
63                 if err != nil {
64                         if err != gocui.ErrUnknownView {
65                                 return err
66                         }
67                         v.Title = peer.name
68                 }
69         }
70         if !GUIReady {
71                 close(GUIReadyC)
72                 GUIReady = true
73         }
74         return nil
75 }