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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
package main
import (
"fmt"
"gf-lt/models"
"gf-lt/pngmeta"
"image"
"net/url"
"os"
"os/exec"
"path"
"slices"
"strings"
"unicode"
"math/rand/v2"
"github.com/rivo/tview"
)
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
// stripThinkingFromMsg removes thinking blocks from assistant messages.
// Skips user, tool, and system messages as they may contain thinking examples.
func stripThinkingFromMsg(msg *models.RoleMsg) *models.RoleMsg {
if !cfg.StripThinkingFromAPI {
return msg
}
// Skip user, tool, and system messages - they might contain thinking examples
if msg.Role == cfg.UserRole || msg.Role == cfg.ToolRole || msg.Role == "system" {
return msg
}
// Strip thinking from assistant messages
if thinkRE.MatchString(msg.Content) {
msg.Content = thinkRE.ReplaceAllString(msg.Content, "")
// Clean up any double newlines that might result
msg.Content = strings.TrimSpace(msg.Content)
}
return msg
}
// refreshChatDisplay updates the chat display based on current character view
// It filters messages for the character the user is currently "writing as"
// and updates the textView with the filtered conversation
func refreshChatDisplay() {
// Determine which character's view to show
viewingAs := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
viewingAs = cfg.WriteNextMsgAs
}
// Filter messages for this character
filteredMessages := filterMessagesForCharacter(chatBody.Messages, viewingAs)
displayText := chatToText(filteredMessages, cfg.ShowSys)
textView.SetText(displayText)
colorText()
if scrollToEndEnabled {
textView.ScrollToEnd()
}
}
func stopTTSIfNotForUser(msg *models.RoleMsg) {
viewingAs := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
viewingAs = cfg.WriteNextMsgAs
}
// stop tts if msg is not for user
if cfg.CharSpecificContextEnabled &&
!slices.Contains(msg.KnownTo, viewingAs) && cfg.TTS_ENABLED {
TTSDoneChan <- true
}
}
func colorText() {
text := textView.GetText(false)
quoteReplacer := strings.NewReplacer(
`”`, `"`,
`“`, `"`,
`“`, `"`,
`”`, `"`,
`**`, `*`,
)
text = quoteReplacer.Replace(text)
// Step 1: Extract code blocks and replace them with unique placeholders
var codeBlocks []string
placeholder := "__CODE_BLOCK_%d__"
counter := 0
// thinking
var thinkBlocks []string
placeholderThink := "__THINK_BLOCK_%d__"
counterThink := 0
// Replace code blocks with placeholders and store their styled versions
text = codeBlockRE.ReplaceAllStringFunc(text, func(match string) string {
// Style the code block and store it
styled := fmt.Sprintf("[red::i]%s[-:-:-]", match)
codeBlocks = append(codeBlocks, styled)
// Generate a unique placeholder (e.g., "__CODE_BLOCK_0__")
id := fmt.Sprintf(placeholder, counter)
counter++
return id
})
text = thinkRE.ReplaceAllStringFunc(text, func(match string) string {
// Style the code block and store it
styled := fmt.Sprintf("[red::i]%s[-:-:-]", match)
thinkBlocks = append(thinkBlocks, styled)
// Generate a unique placeholder (e.g., "__CODE_BLOCK_0__")
id := fmt.Sprintf(placeholderThink, counterThink)
counterThink++
return id
})
// Step 2: Apply other regex styles to the non-code parts
text = quotesRE.ReplaceAllString(text, `[orange::-]$1[-:-:-]`)
text = starRE.ReplaceAllString(text, `[turquoise::i]$1[-:-:-]`)
text = singleBacktickRE.ReplaceAllString(text, "`[pink::i]$1[-:-:-]`")
// text = thinkRE.ReplaceAllString(text, `[yellow::i]$1[-:-:-]`)
// Step 3: Restore the styled code blocks from placeholders
for i, cb := range codeBlocks {
text = strings.Replace(text, fmt.Sprintf(placeholder, i), cb, 1)
}
for i, tb := range thinkBlocks {
text = strings.Replace(text, fmt.Sprintf(placeholderThink, i), tb, 1)
}
textView.SetText(text)
}
func updateStatusLine() {
statusLineWidget.SetText(makeStatusLine())
helpView.SetText(fmt.Sprintf(helpText, makeStatusLine()))
}
func initSysCards() ([]string, error) {
labels := []string{}
labels = append(labels, sysLabels...)
cards, err := pngmeta.ReadDirCards(cfg.SysDir, cfg.UserRole, logger)
if err != nil {
logger.Error("failed to read sys dir", "error", err)
return nil, err
}
for _, cc := range cards {
if cc.Role == "" {
logger.Warn("empty role", "file", cc.FilePath)
continue
}
sysMap[cc.Role] = cc
labels = append(labels, cc.Role)
}
return labels, nil
}
func startNewChat(keepSysP bool) {
id, err := store.ChatGetMaxID()
if err != nil {
logger.Error("failed to get chat id", "error", err)
}
if ok := charToStart(cfg.AssistantRole, keepSysP); !ok {
logger.Warn("no such sys msg", "name", cfg.AssistantRole)
}
// set chat body
chatBody.Messages = chatBody.Messages[:2]
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
newChat := &models.Chat{
ID: id + 1,
Name: fmt.Sprintf("%d_%s", id+1, cfg.AssistantRole),
// chat is written to db when we get first llm response (or any)
// actual chat history (messages) would be parsed then
Msgs: "",
Agent: cfg.AssistantRole,
}
activeChatName = newChat.Name
chatMap[newChat.Name] = newChat
updateStatusLine()
colorText()
}
func renameUser(oldname, newname string) {
if oldname == "" {
// not provided; deduce who user is
// INFO: if user not yet spoke, it is hard to replace mentions in sysprompt and first message about thme
roles := chatBody.ListRoles()
for _, role := range roles {
if role == cfg.AssistantRole {
continue
}
if role == "tool" {
continue
}
if role == "system" {
continue
}
oldname = role
break
}
if oldname == "" {
// still
logger.Warn("fn: renameUser; failed to find old name", "newname", newname)
return
}
}
viewText := textView.GetText(false)
viewText = strings.ReplaceAll(viewText, oldname, newname)
chatBody.Rename(oldname, newname)
textView.SetText(viewText)
}
func setLogLevel(sl string) {
switch sl {
case "Debug":
logLevel.Set(-4)
case "Info":
logLevel.Set(0)
case "Warn":
logLevel.Set(4)
}
}
func listRolesWithUser() []string {
roles := listChatRoles()
// Remove user role if it exists in the list (to avoid duplicates and ensure it's at position 0)
filteredRoles := make([]string, 0, len(roles))
for _, role := range roles {
if role != cfg.UserRole {
filteredRoles = append(filteredRoles, role)
}
}
// Prepend user role to the beginning of the list
result := append([]string{cfg.UserRole}, filteredRoles...)
slices.Sort(result)
return result
}
func loadImage() {
filepath := defaultImage
cc, ok := sysMap[cfg.AssistantRole]
if ok {
if strings.HasSuffix(cc.FilePath, ".png") {
filepath = cc.FilePath
}
}
file, err := os.Open(filepath)
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
imgView.SetImage(img)
}
func strInSlice(s string, sl []string) bool {
for _, el := range sl {
if strings.EqualFold(s, el) {
return true
}
}
return false
}
// isLocalLlamacpp checks if the current API is a local llama.cpp instance.
func isLocalLlamacpp() bool {
u, err := url.Parse(cfg.CurrentAPI)
if err != nil {
return false
}
host := u.Hostname()
return host == "localhost" || host == "127.0.0.1" || host == "::1"
}
// getModelColor returns the color tag for the model name based on its load status.
// For non-local models, returns orange. For local llama.cpp models, returns green if loaded, red if not.
func getModelColor() string {
if !isLocalLlamacpp() {
return "orange"
}
// Check if model is loaded
loaded, err := isModelLoaded(chatBody.Model)
if err != nil {
// On error, assume not loaded (red)
return "red"
}
if loaded {
return "green"
}
return "red"
}
func makeStatusLine() string {
isRecording := false
if asr != nil {
isRecording = asr.IsRecording()
}
persona := cfg.UserRole
if cfg.WriteNextMsgAs != "" {
persona = cfg.WriteNextMsgAs
}
botPersona := cfg.AssistantRole
if cfg.WriteNextMsgAsCompletionAgent != "" {
botPersona = cfg.WriteNextMsgAsCompletionAgent
}
// Add image attachment info to status line
var imageInfo string
if imageAttachmentPath != "" {
// Get just the filename from the path
imageName := path.Base(imageAttachmentPath)
imageInfo = fmt.Sprintf(" | attached img: [orange:-:b]%s[-:-:-]", imageName)
} else {
imageInfo = ""
}
// Add shell mode status to status line
var shellModeInfo string
if shellMode {
shellModeInfo = " | [green:-:b]SHELL MODE[-:-:-]"
} else {
shellModeInfo = ""
}
// Get model color based on load status for local llama.cpp models
modelColor := getModelColor()
statusLine := fmt.Sprintf(indexLineCompletion, boolColors[botRespMode], botRespMode, activeChatName,
boolColors[cfg.ToolUse], cfg.ToolUse, modelColor, chatBody.Model, boolColors[cfg.SkipLLMResp],
cfg.SkipLLMResp, cfg.CurrentAPI, boolColors[isRecording], isRecording, persona,
botPersona, boolColors[injectRole], injectRole)
return statusLine + imageInfo + shellModeInfo
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.IntN(len(letters))]
}
return string(b)
}
// set of roles within card definition and mention in chat history
func listChatRoles() []string {
currentChat, ok := chatMap[activeChatName]
cbc := chatBody.ListRoles()
if !ok {
return cbc
}
currentCard, ok := sysMap[currentChat.Agent]
if !ok {
// case which won't let to switch roles:
// started new chat (basic_sys or any other), at the start it yet be saved or have chatbody
// if it does not have a card or chars, it'll return an empty slice
// log error
logger.Warn("failed to find current card in sysMap", "agent", currentChat.Agent, "sysMap", sysMap)
return cbc
}
charset := []string{}
for _, name := range currentCard.Characters {
if !strInSlice(name, cbc) {
charset = append(charset, name)
}
}
charset = append(charset, cbc...)
return charset
}
func deepseekModelValidator() error {
if cfg.CurrentAPI == cfg.DeepSeekChatAPI || cfg.CurrentAPI == cfg.DeepSeekCompletionAPI {
if chatBody.Model != "deepseek-chat" && chatBody.Model != "deepseek-reasoner" {
if err := notifyUser("bad request", "wrong deepseek model name"); err != nil {
logger.Warn("failed ot notify user", "error", err)
return err
}
return nil
}
}
return nil
}
// == shellmode ==
func toggleShellMode() {
shellMode = !shellMode
if shellMode {
// Update input placeholder to indicate shell mode
textArea.SetPlaceholder("SHELL MODE: Enter command and press <Esc> to execute")
} else {
// Reset to normal mode
textArea.SetPlaceholder("input is multiline; press <Enter> to start the next line;\npress <Esc> to send the message. Alt+1 to exit shell mode")
}
updateStatusLine()
}
func updateFlexLayout() {
if fullscreenMode {
// flex already contains only focused widget; do nothing
return
}
flex.Clear()
flex.AddItem(textView, 0, 40, false)
flex.AddItem(textArea, 0, 10, false)
if positionVisible {
flex.AddItem(statusLineWidget, 0, 2, false)
}
// Keep focus on currently focused widget
focused := app.GetFocus()
if focused == textView {
app.SetFocus(textView)
} else {
app.SetFocus(textArea)
}
}
func executeCommandAndDisplay(cmdText string) {
// Parse the command (split by spaces, but handle quoted arguments)
cmdParts := parseCommand(cmdText)
if len(cmdParts) == 0 {
fmt.Fprintf(textView, "\n[red]Error: No command provided[-:-:-]\n")
if scrollToEndEnabled {
textView.ScrollToEnd()
}
colorText()
return
}
command := cmdParts[0]
args := []string{}
if len(cmdParts) > 1 {
args = cmdParts[1:]
}
// Create the command execution
cmd := exec.Command(command, args...)
// Execute the command and get output
output, err := cmd.CombinedOutput()
// Add the command being executed to the chat
fmt.Fprintf(textView, "\n[yellow]$ %s[-:-:-]\n", cmdText)
var outputContent string
if err != nil {
// Include both output and error
errorMsg := "Error: " + err.Error()
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", errorMsg)
if len(output) > 0 {
outputStr := string(output)
fmt.Fprintf(textView, "[red]%s[-:-:-]\n", outputStr)
outputContent = errorMsg + "\n" + outputStr
} else {
outputContent = errorMsg
}
} else {
// Only output if successful
if len(output) > 0 {
outputStr := string(output)
fmt.Fprintf(textView, "[green]%s[-:-:-]\n", outputStr)
outputContent = outputStr
} else {
successMsg := "Command executed successfully (no output)"
fmt.Fprintf(textView, "[green]%s[-:-:-]\n", successMsg)
outputContent = successMsg
}
}
// Combine command and output in a single message for chat history
combinedContent := "$ " + cmdText + "\n\n" + outputContent
combinedMsg := models.RoleMsg{
Role: cfg.ToolRole,
Content: combinedContent,
}
chatBody.Messages = append(chatBody.Messages, combinedMsg)
// Scroll to end and update colors
if scrollToEndEnabled {
textView.ScrollToEnd()
}
colorText()
}
// parseCommand splits command string handling quotes properly
func parseCommand(cmd string) []string {
var args []string
var current string
var inQuotes bool
var quoteChar rune
for _, r := range cmd {
switch r {
case '"', '\'':
if inQuotes {
if r == quoteChar {
inQuotes = false
} else {
current += string(r)
}
} else {
inQuotes = true
quoteChar = r
}
case ' ', '\t':
if inQuotes {
current += string(r)
} else if current != "" {
args = append(args, current)
current = ""
}
default:
current += string(r)
}
}
if current != "" {
args = append(args, current)
}
return args
}
// == search ==
// Global variables for search state
var searchResults []int
var searchResultLengths []int // To store the length of each match in the formatted string
var searchIndex int
var searchText string
var originalTextForSearch string
// performSearch searches for the given term in the textView content and highlights matches
func performSearch(term string) {
searchText = term
if searchText == "" {
searchResults = nil
searchResultLengths = nil
originalTextForSearch = ""
// Re-render text without highlights
textView.SetText(chatToText(chatBody.Messages, cfg.ShowSys))
colorText()
return
}
// Get formatted text and search directly in it to avoid mapping issues
formattedText := textView.GetText(true)
originalTextForSearch = formattedText
searchTermLower := strings.ToLower(searchText)
formattedTextLower := strings.ToLower(formattedText)
// Find all occurrences of the search term in the formatted text directly
formattedSearchResults := []int{}
searchStart := 0
for {
pos := strings.Index(formattedTextLower[searchStart:], searchTermLower)
if pos == -1 {
break
}
absolutePos := searchStart + pos
formattedSearchResults = append(formattedSearchResults, absolutePos)
searchStart = absolutePos + len(searchText)
}
if len(formattedSearchResults) == 0 {
// No matches found
searchResults = nil
searchResultLengths = nil
notification := "Pattern not found: " + term
if err := notifyUser("search", notification); err != nil {
logger.Error("failed to send notification", "error", err)
}
return
}
// Store the formatted text positions and lengths for accurate highlighting
searchResults = formattedSearchResults
// Create lengths array - all matches have the same length as the search term
searchResultLengths = make([]int, len(formattedSearchResults))
for i := range searchResultLengths {
searchResultLengths[i] = len(searchText)
}
searchIndex = 0
highlightCurrentMatch()
}
// highlightCurrentMatch highlights the current search match and scrolls to it
func highlightCurrentMatch() {
if len(searchResults) == 0 || searchIndex >= len(searchResults) {
return
}
// Get the stored formatted text
formattedText := originalTextForSearch
// For tview to properly support highlighting and scrolling, we need to work with its region system
// Instead of just applying highlights, we need to add region tags to the text
highlightedText := addRegionTags(formattedText, searchResults, searchResultLengths, searchIndex, searchText)
// Update the text view with the text that includes region tags
textView.SetText(highlightedText)
// Highlight the current region and scroll to it
// Need to identify which position in the results array corresponds to the current match
// The region ID will be search_<position>_<index>
currentRegion := fmt.Sprintf("search_%d_%d", searchResults[searchIndex], searchIndex)
textView.Highlight(currentRegion).ScrollToHighlight()
// Send notification about which match we're at
notification := fmt.Sprintf("Match %d of %d", searchIndex+1, len(searchResults))
if err := notifyUser("search", notification); err != nil {
logger.Error("failed to send notification", "error", err)
}
}
// showSearchBar shows the search input field as an overlay
func showSearchBar() {
// Create a temporary flex to combine search and main content
updatedFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchField, 3, 0, true). // Search field at top
AddItem(flex, 0, 1, false) // Main flex layout below
// Add the search overlay as a page
pages.AddPage(searchPageName, updatedFlex, true, true)
app.SetFocus(searchField)
}
// hideSearchBar hides the search input field
func hideSearchBar() {
pages.RemovePage(searchPageName)
// Return focus to the text view
app.SetFocus(textView)
// Clear the search field
searchField.SetText("")
}
// Global variables for index overlay functionality
var indexPageName = "indexOverlay"
// showIndexBar shows the index input field as an overlay at the top
func showIndexBar() {
// Create a temporary flex to combine index input and main content
updatedFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(indexPickWindow, 3, 0, true). // Index field at top
AddItem(flex, 0, 1, false) // Main flex layout below
// Add the index overlay as a page
pages.AddPage(indexPageName, updatedFlex, true, true)
app.SetFocus(indexPickWindow)
}
// hideIndexBar hides the index input field
func hideIndexBar() {
pages.RemovePage(indexPageName)
// Return focus to the text view
app.SetFocus(textView)
// Clear the index field
indexPickWindow.SetText("")
}
// addRegionTags adds region tags to search matches in the text for tview highlighting
func addRegionTags(text string, positions []int, lengths []int, currentIdx int, searchTerm string) string {
if len(positions) == 0 {
return text
}
var result strings.Builder
lastEnd := 0
for i, pos := range positions {
endPos := pos + lengths[i]
// Add text before this match
if pos > lastEnd {
result.WriteString(text[lastEnd:pos])
}
// The matched text, which may contain its own formatting tags
actualText := text[pos:endPos]
// Add region tag and highlighting for this match
// Use a unique region id that includes the match index to avoid conflicts
regionId := fmt.Sprintf("search_%d_%d", pos, i) // position + index to ensure uniqueness
var highlightStart, highlightEnd string
if i == currentIdx {
// Current match - use different highlighting
highlightStart = fmt.Sprintf(`["%s"][yellow:blue:b]`, regionId) // Current match with region and special highlight
highlightEnd = `[-:-:-][""]` // Reset formatting and close region
} else {
// Other matches - use regular highlighting
highlightStart = fmt.Sprintf(`["%s"][gold:red:u]`, regionId) // Other matches with region and highlight
highlightEnd = `[-:-:-][""]` // Reset formatting and close region
}
result.WriteString(highlightStart)
result.WriteString(actualText)
result.WriteString(highlightEnd)
lastEnd = endPos
}
// Add the rest of the text after the last processed match
if lastEnd < len(text) {
result.WriteString(text[lastEnd:])
}
return result.String()
}
// searchNext finds the next occurrence of the search term
func searchNext() {
if len(searchResults) == 0 {
if err := notifyUser("search", "No search results to navigate"); err != nil {
logger.Error("failed to send notification", "error", err)
}
return
}
searchIndex = (searchIndex + 1) % len(searchResults)
highlightCurrentMatch()
}
// searchPrev finds the previous occurrence of the search term
func searchPrev() {
if len(searchResults) == 0 {
if err := notifyUser("search", "No search results to navigate"); err != nil {
logger.Error("failed to send notification", "error", err)
}
return
}
if searchIndex == 0 {
searchIndex = len(searchResults) - 1
} else {
searchIndex--
}
highlightCurrentMatch()
}
// == tab completion ==
func scanFiles(dir, filter string) []string {
var files []string
entries, err := os.ReadDir(dir)
if err != nil {
return files
}
for _, entry := range entries {
name := entry.Name()
if strings.HasPrefix(name, ".") {
continue
}
if filter == "" || strings.HasPrefix(strings.ToLower(name), strings.ToLower(filter)) {
if entry.IsDir() {
files = append(files, name+"/")
} else {
files = append(files, name)
}
}
}
return files
}
func showFileCompletion(filter string) {
baseDir := cfg.CodingDir
if baseDir == "" {
baseDir = "."
}
complMatches = scanFiles(baseDir, filter)
go func() {
app.QueueUpdateDraw(func() {
if len(complMatches) == 0 {
hideCompletion() // Make sure hideCompletion also uses QueueUpdate if it modifies UI
return
}
// Limit the number of complMatches
if len(complMatches) > 10 {
complMatches = complMatches[:10]
}
// Update the popup's content
complPopup.Clear()
for _, f := range complMatches {
complPopup.AddItem(f, "", 0, nil)
}
// Update state and UI
complPopup.SetCurrentItem(0)
complActive = true
// Show the popup and set focus
pages.AddPage("complPopup", complPopup, true, true)
app.SetFocus(complPopup)
// No need to call app.Draw() here, QueueUpdateDraw does it automatically
})
}()
}
func insertCompletion() {
if complIndex >= 0 && complIndex < len(complMatches) {
match := complMatches[complIndex]
currentText := textArea.GetText()
atIdx := strings.LastIndex(currentText, "@")
if atIdx >= 0 {
before := currentText[:atIdx]
textArea.SetText(before+match, true)
}
}
hideCompletion()
}
func hideCompletion() {
complActive = false
complMatches = nil
pages.RemovePage("complPopup")
app.SetFocus(textArea)
app.Draw()
}
|