-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoreservice.go
More file actions
91 lines (79 loc) · 2.85 KB
/
coreservice.go
File metadata and controls
91 lines (79 loc) · 2.85 KB
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
package xterm
// Ported from xterm.js src/common/services/CoreService.ts.
// defaultModes returns the default ANSI modes.
func defaultModes() Modes {
return Modes{InsertMode: false}
}
// defaultDecPrivateModes returns the default DEC private modes.
func defaultDecPrivateModes() DecPrivateModes {
return DecPrivateModes{
Wraparound: true, // xterm default
}
}
// defaultKittyKeyboardState returns a fresh kitty keyboard state.
func defaultKittyKeyboardState() KittyKeyboardState {
return KittyKeyboardState{}
}
// CoreService manages terminal modes and provides data/binary event plumbing.
type CoreService struct {
IsCursorInitialized bool
IsCursorHidden bool
Modes Modes
DecPrivateModes DecPrivateModes
KittyKeyboard KittyKeyboardState
options *OptionsService
OnDataEmitter EventEmitter[string]
OnUserInputEmitter EventEmitter[struct{}]
OnBinaryEmitter EventEmitter[string]
OnRequestScrollToBottomEmitter EventEmitter[struct{}]
}
// NewCoreService creates a CoreService. The OptionsService is used to check
// disableStdin and scrollOnUserInput. bufferService is not stored; callers
// pass buffer state through TriggerDataEvent parameters.
func NewCoreService(opts *OptionsService) *CoreService {
return &CoreService{
IsCursorInitialized: opts.Options.ShowCursorImmediately,
Modes: defaultModes(),
DecPrivateModes: defaultDecPrivateModes(),
KittyKeyboard: defaultKittyKeyboardState(),
options: opts,
}
}
// Reset restores modes and kitty keyboard state to defaults.
func (cs *CoreService) Reset() {
cs.Modes = defaultModes()
cs.DecPrivateModes = defaultDecPrivateModes()
cs.KittyKeyboard = defaultKittyKeyboardState()
}
// TriggerDataEvent fires the OnData event. If wasUserInput is true and
// scrollOnUserInput is enabled, it also fires OnRequestScrollToBottom
// and OnUserInput. Respects disableStdin.
//
// shouldScroll indicates whether the viewport is not at the bottom (ybase != ydisp).
// The caller is responsible for computing this from the active buffer.
func (cs *CoreService) TriggerDataEvent(data string, wasUserInput bool, shouldScroll bool) {
if cs.options.Options.DisableStdin {
return
}
if wasUserInput && cs.options.Options.ScrollOnUserInput && shouldScroll {
cs.OnRequestScrollToBottomEmitter.Fire(struct{}{})
}
if wasUserInput {
cs.OnUserInputEmitter.Fire(struct{}{})
}
cs.OnDataEmitter.Fire(data)
}
// TriggerBinaryEvent fires the OnBinary event. Respects disableStdin.
func (cs *CoreService) TriggerBinaryEvent(data string) {
if cs.options.Options.DisableStdin {
return
}
cs.OnBinaryEmitter.Fire(data)
}
// Dispose cleans up all event emitters.
func (cs *CoreService) Dispose() {
cs.OnDataEmitter.Dispose()
cs.OnUserInputEmitter.Dispose()
cs.OnBinaryEmitter.Dispose()
cs.OnRequestScrollToBottomEmitter.Dispose()
}