-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstance.go
199 lines (161 loc) · 5.7 KB
/
instance.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
190
191
192
193
194
195
196
197
198
199
package readline
import (
"context"
"os"
"sync"
)
var ForceCrLf = true
type HintCacheFuncT func(prefix string, items []string) []string
type PreviewFuncT func(ctx context.Context, line []rune, item string, incImages bool, size *PreviewSizeT, callback PreviewFuncCallbackT)
type PreviewFuncCallbackT func(lines []string, pos int, err error)
type TabCompleterReturnT struct {
Prefix string
Suggestions []string
Descriptions map[string]string
DisplayType TabDisplayType
HintCache HintCacheFuncT
Preview PreviewFuncT
}
// Instance is used to encapsulate the parameter group and run time of any given
// readline instance so that you can reuse the readline API for multiple entry
// captures without having to repeatedly unload configuration.
type Instance struct {
fdMutex sync.Mutex
//fdMutex debug.BadMutex
Active bool
closeSigwinch func()
// PasswordMask is what character to hide password entry behind.
// Once enabled, set to 0 (zero) to disable the mask again.
PasswordMask rune
// SyntaxHighlight is a helper function to provide syntax highlighting.
// Once enabled, set to nil to disable again.
SyntaxHighlighter func([]rune) string
// History is an interface for querying the readline history.
// This is exposed as an interface to allow you the flexibility to define how
// you want your history managed (eg file on disk, database, cloud, or even
// no history at all). By default it uses a dummy interface that only stores
// historic items in memory.
History History
// HistoryAutoWrite defines whether items automatically get written to
// history.
// Enabled by default. Set to false to disable.
HistoryAutoWrite bool
// TabCompleter is a function that offers completion suggestions.
TabCompleter func([]rune, int, DelayedTabContext) *TabCompleterReturnT
delayedTabContext DelayedTabContext
tcr *TabCompleterReturnT
MinTabItemLength int
MaxTabItemLength int
// MaxTabCompletionRows is the maximum number of rows to display in the tab
// completion grid.
MaxTabCompleterRows int
// SyntaxCompletion is used to autocomplete code syntax (like braces and
// quotation marks). If you want to complete words or phrases then you might
// be better off using the TabCompletion function.
// SyntaxCompletion takes the line ([]rune), change (string) and cursor
// position, and returns the new line and cursor position.
SyntaxCompleter func([]rune, string, int) ([]rune, int)
// DelayedSyntaxWorker allows for syntax highlighting happen to the line
// after the line has been drawn.
DelayedSyntaxWorker func([]rune) []rune
delayedSyntaxCount int32
// HintText is a helper function which displays hint text the prompt.
// HintText takes the line input from the prompt and the cursor position.
// It returns the hint text to display.
HintText func([]rune, int) []rune
// HintColor any ANSI escape codes you wish to use for hint formatting. By
// default this will just be blue.
HintFormatting string
// AutocompleteHistory is another customization allowing for alternative
// results when [ctrl]+[r]
AutocompleteHistory func(string) ([]string, map[string]string)
// TempDirectory is the path to write temporary files when editing a line in
// $EDITOR. This will default to os.TempDir()
TempDirectory string
// GetMultiLine is a callback to your host program. Since multiline support
// is handled by the application rather than readline itself, this callback
// is required when calling $EDITOR. However if this function is not set
// then readline will just use the current line.
GetMultiLine func([]rune) []rune
MaxCacheSize int
cacheHint cacheSliceRune
cacheSyntax cacheString
//cacheSyntaxHighlight cacheString
//cacheSyntaxDelayed cacheSliceRune
// readline operating parameters
prompt string // = ">>> "
promptLen int //= 4
line *UnicodeT
lineChange string // cache what had changed from previous line
termWidth int
multiline []byte
multiSplit []string
skipStdinRead bool
// history
lineBuf *UnicodeT
histPos int
// hint text
hintY int
hintText []rune
ScreenRefresh func()
PreviewInit func()
previewMode previewModeT
previewRef previewRefT
previewItem string
previewCache *previewCacheT
PreviewImages bool
previewCancel context.CancelFunc
PreviewLine PreviewFuncT
// tab completion
modeTabCompletion bool
tabMutex sync.Mutex
tcPrefix string
tcSuggestions []string
tcDescriptions map[string]string
tcDisplayType TabDisplayType
tcOffset int
tcPosX int
tcPosY int
tcMaxX int
tcMaxY int
tcUsedY int
tcMaxLength int
// tab find
modeTabFind bool
rFindSearch []rune // searching message
rFindCancel []rune // search cancelled message
tfLine []rune
tfSuggestions []string
modeAutoFind bool // for when invoked via ^R or ^F outside of [tab]
// vim
modeViMode viMode //= vimInsert
viIteration string
viUndoHistory []*UnicodeT
viUndoSkipAppend bool
viYankBuffer string
viCommandLine []rune
// event
evtKeyPress map[string]keyPressEventCallbackT
//ForceCrLf bool
EnableGetCursorPos bool
}
// NewInstance is used to create a readline instance and initialise it with sane
// defaults.
func NewInstance() *Instance {
rl := new(Instance)
rl.line = new(UnicodeT)
rl.lineBuf = new(UnicodeT)
rl.History = new(ExampleHistory)
rl.HistoryAutoWrite = true
rl.MaxTabCompleterRows = 4
rl.prompt = ">>> "
rl.promptLen = 4
rl.HintFormatting = seqFgBlue
rl.evtKeyPress = make(map[string]keyPressEventCallbackT)
rl.TempDirectory = os.TempDir()
rl.MaxCacheSize = 256
rl.cacheHint.Init(rl)
rl.cacheSyntax.Init(rl)
//rl.ForceCrLf = true
return rl
}