/
main.go
93 lines (75 loc) · 1.95 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
93
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/Pallinder/go-randomdata"
"github.com/burntcarrot/pairpad/client/editor"
"github.com/burntcarrot/pairpad/commons"
"github.com/burntcarrot/pairpad/crdt"
"github.com/sirupsen/logrus"
)
var (
// Local document containing content.
doc = crdt.New()
// Centralized logger.
logger = logrus.New()
// termbox-based editor.
e = editor.NewEditor(editor.EditorConfig{})
// The name of the file to load from and save to.
fileName string
// Parsed flags.
flags Flags
)
func main() {
// Parse flags.
flags = parseFlags()
s := bufio.NewScanner(os.Stdin)
// Generate a random username.
name := randomdata.SillyName()
// Read username based if login flag is set to true.
if flags.Login {
fmt.Print("Enter your name: ")
s.Scan()
name = s.Text()
}
conn, _, err := createConn(flags)
if err != nil {
fmt.Printf("Connection error, exiting: %s\n", err)
return
}
defer conn.Close()
// Send joining message.
msg := commons.Message{Username: name, Text: "has joined the session.", Type: commons.JoinMessage}
_ = conn.WriteJSON(msg)
logFile, debugLogFile, err := setupLogger(logger)
if err != nil {
fmt.Printf("Failed to setup logger, exiting: %s\n", err)
return
}
defer closeLogFiles(logFile, debugLogFile)
if flags.File != "" {
if doc, err = crdt.Load(flags.File); err != nil {
fmt.Printf("failed to load document: %s\n", err)
return
}
}
uiConfig := UIConfig{
EditorConfig: editor.EditorConfig{
ScrollEnabled: flags.Scroll,
},
}
err = initUI(conn, uiConfig)
if err != nil {
// If error has the prefix "pairpad", then it was triggered by an event that wasn't an error, for example, exiting the editor.
// It's a hacky solution since the UI returns an error only.
if strings.HasPrefix(err.Error(), "pairpad") {
fmt.Println("exiting session.")
return
}
// This is printed when it's an actual error.
fmt.Printf("TUI error, exiting: %s\n", err)
return
}
}