-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
189 lines (154 loc) · 5.19 KB
/
state.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"github.com/1egoman/slick/gateway" // The thing to interface with slack
"github.com/1egoman/slick/modal"
"github.com/1egoman/slick/status"
)
// This struct contains the main application state. I have fluxy intentions.
type State struct {
Mode string
Command []rune
CommandCursorPosition int
// Is the current session of the client offline?
Offline bool
// A list of all keys that have been pressed to make up the current command.
KeyStack []rune
// All the connections that are currently made to outside services.
Connections []gateway.Connection
activeConnection int
connectionSynced bool
// Interacting with messages
SelectedMessageIndex int
BottomDisplayedItem int
RenderedMessageNumber int
RenderedAllMessages bool
// Fuzzy picker
SelectionInput SelectionInput
SelectionInputSelectedItem int
SelectionInputBottomDisplayedItem int
// Status message
Status status.Status
// Modal
Modal modal.Modal
// Handlers to bind to specific actions. For example, when the user presses some keys, when we
// switch connections, etc...
EventActions []EventAction
// A map of configuration options for the editor.
Configuration map[string]string
}
func NewInitialState() *State {
return NewInitialStateMode("chat")
}
func NewInitialStateMode(mode string) *State {
return &State{
// The mode the client is in
Mode: mode,
// Starts online
Offline: false,
// The command the user is typing
Command: []rune{},
CommandCursorPosition: 0,
// Connection to the server
Connections: []gateway.Connection{},
// Which connection in the connections object is active
activeConnection: 0,
connectionSynced: false,
// Interacting with messages
SelectedMessageIndex: 0,
BottomDisplayedItem: 0,
RenderedMessageNumber: -1, // A render loop hasn't run yet.
RenderedAllMessages: false,
// Selection Input data
SelectionInput: SelectionInput{},
// Status message
Status: status.Status{},
// Modal
Modal: modal.Modal{},
// Configuration options
Configuration: map[string]string{
// Disable connection caching
"Connection.Cache": "true",
// Should relative line numbers be shown for each message?
// "Message.RelativeLine": "true",
// Disable auto-updates
// "AutoUpdate": "true",
// The format for the tiemstamp in front of each message.
// Reference date: `Mon Jan 2 15:04:05 MST 2006`
"Message.TimestampFormat": " Jan 2 15:04:05",
// How many messages should Ctrl-U / Ctrl-D page by?
"Message.PageAmount": "12",
// User online status settings
"Message.Sender.OnlinePrefix": "*",
"Message.Sender.OnlinePrefixColor": "green::",
"Message.Sender.OfflinePrefix": "*",
"Message.Sender.OfflinePrefixColor": "silver::",
"Message.ReactionColor": "::",
"Message.FileColor": "::",
"Message.SelectedColor": ":teal:",
"Message.Action.Color": "::",
"Message.Action.HighlightColor": "red::",
"Message.Attachment.TitleColor": "green::",
"Message.Attachment.FieldTitleColor": "::B",
"Message.Attachment.FieldValueColor": "::",
"Message.Part.AtMentionUserColor": "red::B",
"Message.Part.AtMentionGroupColor": "yellow::B",
"Message.Part.ChannelColor": "blue::B",
"Message.Part.LinkColor": "cyan::BU",
"Message.LineNumber.Color": "white::",
"Message.LineNumber.ActiveColor": "teal::",
"Message.UnconfirmedColor": "gray::",
"CommandBar.PrefixColor": "::",
"CommandBar.TextColor": "::",
"CommandBar.NewLineColor": "gray::B",
"StatusBar.Color": "::",
"StatusBar.ModeColor": "::",
"StatusBar.ActiveConnectionColor": "white:blue:",
"StatusBar.GatewayConnectedColor": "white::",
"StatusBar.GatewayConnectingColor": ":darkmagenta:",
"StatusBar.GatewayFailedColor": ":red:",
"StatusBar.LogColor": "white::",
"StatusBar.ErrorColor": "darkmagenta::B",
"StatusBar.TopBorderColor": ":gray:",
"FuzzyPicker.TopBorderColor": ":gray:",
"FuzzyPicker.ActiveItemColor": "::B",
},
}
}
func (s *State) ActiveConnection() gateway.Connection {
if len(s.Connections) > 0 {
return s.Connections[s.activeConnection]
} else {
return nil
}
}
// Methods to manage the active connection
// When the user changes the active connection,
func (s *State) SetActiveConnection(index int) {
s.activeConnection = index
s.connectionSynced = false
}
func (s *State) SetNextActiveConnection() {
s.activeConnection += 1
s.connectionSynced = false
// Make sure connectino can never get larger than the amount of conenctions
if s.activeConnection > len(s.Connections)-1 {
s.activeConnection = len(s.Connections) - 1
}
}
func (s *State) SetPrevActiveConnection() {
s.activeConnection -= 1
s.connectionSynced = false
// Make sure connectino can never get below 0
if s.activeConnection < 0 {
s.activeConnection = 0
}
}
func (s *State) ConnectionIsStale() bool {
return !s.connectionSynced
}
func (s *State) SyncActiveConnection() {
s.connectionSynced = true
}
func (s *State) ActiveConnectionIndex() int {
return s.activeConnection
}