-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (82 loc) · 2.02 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/jroimartin/gocui"
)
var outputView *gocui.View
var sidebar *gocui.View
var database *gorm.DB
var ui *gocui.Gui
var inputfocus *cmdlinesink
var activeChatID uint
var activeCrewID uint
var activeContactID uint
func main() {
filename := os.Args[1]
database, _ = gorm.Open("sqlite3", filename)
g, _ := gocui.NewGui(gocui.OutputNormal)
ui = g
g.Cursor = true
g.SetManagerFunc(layout)
g.SetKeybinding("commandline", gocui.KeyEnter, gocui.ModNone, operatecommand)
setupDatabase(database)
setupAutomation()
setupTelegram()
SetupAdmin()
setupHTTP()
defer database.Close()
fillCommands()
//before we start, we have to disconnect everyone from after being connected during a crash.
var chats []chat
database.Where(&chat{OpenConnection: true}).Find(&chats)
for _, chat := range chats {
chat.OpenConnection = false
database.Save(&chat)
chat.sendMessage("Die Verbindung wurde unterbrochen")
}
g.MainLoop()
g.Close()
}
type printer func(string)
func output(context func(printer printer)) {
ui.Update(func(g *gocui.Gui) error {
context(func(line string) {
fmt.Fprintln(outputView, line)
})
return nil
})
}
func operatecommand(g *gocui.Gui, v *gocui.View) error {
cmd := v.Buffer()
cmd = strings.TrimSuffix(cmd, "\n")
v.SetOrigin(0, 0)
v.SetCursor(0, 0)
v.Clear()
if inputfocus != nil {
instance := inputfocus
inputfocus = nil
return (*instance).TextEntered(cmd)
}
fmt.Fprintf(outputView, "> %s\n", cmd)
params := strings.Split(cmd, " ")
return run(params)
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
outputView, _ = g.SetView("content", 0, 0, maxX-33, maxY-4)
outputView.Wrap = true
outputView.Autoscroll = true
initialupdate := sidebar == nil
sidebar, _ = g.SetView("sidebar", maxX-32, 0, maxX-1, maxY-4)
v, _ := g.SetView("commandline", 0, maxY-3, maxX-1, maxY-1)
v.Editable = true
g.SetCurrentView("commandline")
if initialupdate {
updateSidebar()
}
return nil
}