Debian Codegraph @

internal/info/infobuffer.go

raw
1 package info
2
3 import (
4 "fmt"
5
6 "github.com/zyedidia/micro/v2/internal/buffer"
7 )
8
9 // The InfoBuf displays messages and other info at the bottom of the screen.
10 // It is represented as a buffer and a message with a style.
11 type InfoBuf struct {
12 *buffer.Buffer
13
14 HasPrompt bool
15 HasMessage bool
16 HasError bool
17 HasYN bool
18
19 PromptType string
20
21 Msg string
22 YNResp bool
23
24 // This map stores the history for all the different kinds of uses Prompt has
25 // It's a map of history type -> history array
26 History map[string][]string
27 HistoryNum int
28 // HistorySearch indicates whether we are searching for history items
29 // beginning with HistorySearchPrefix
30 HistorySearch bool
31 HistorySearchPrefix string
32
33 // Is the current message a message from the gutter
34 HasGutter bool
35
36 PromptCallback func(resp string, canceled bool)
37 EventCallback func(resp string)
38 YNCallback func(yes bool, canceled bool)
39 }
40
41 // NewBuffer returns a new infobuffer
42 func NewBuffer() *InfoBuf {
43 ib := new(InfoBuf)
44 ib.History = make(map[string][]string)
45
46 ib.Buffer = buffer.NewBufferFromString("", "", buffer.BTInfo)
47 ib.LoadHistory()
48
49 return ib
50 }
51
52 // Close performs any cleanup necessary when shutting down the infobuffer
53 func (i *InfoBuf) Close() {
54 i.SaveHistory()
55 }
56
57 // Message sends a message to the user
58 func (i *InfoBuf) Message(msg ...any) {
59 // only display a new message if there isn't an active prompt
60 // this is to prevent overwriting an existing prompt to the user
61 if !i.HasPrompt {
62 displayMessage := fmt.Sprint(msg...)
63 // if there is no active prompt then style and display the message as normal
64 i.Msg = displayMessage
65 i.HasMessage, i.HasError = true, false
66 }
67 }
68
69 // GutterMessage displays a message and marks it as a gutter message
70 func (i *InfoBuf) GutterMessage(msg ...any) {
71 i.Message(msg...)
72 i.HasGutter = true
73 }
74
75 // ClearGutter clears the info bar and unmarks the message
76 func (i *InfoBuf) ClearGutter() {
77 i.HasGutter = false
78 i.Message("")
79 }
80
81 // Error sends an error message to the user
82 func (i *InfoBuf) Error(msg ...any) {
83 // only display a new message if there isn't an active prompt
84 // this is to prevent overwriting an existing prompt to the user
85 if !i.HasPrompt {
86 // if there is no active prompt then style and display the message as normal
87 i.Msg = fmt.Sprint(msg...)
88 i.HasMessage, i.HasError = false, true
89 }
90 // TODO: add to log?
91 }
92
93 // Prompt starts a prompt for the user, it takes a prompt, a possibly partially filled in msg
94 // and callbacks executed when the user executes an event and when the user finishes the prompt
95 // The eventcb passes the current user response as the argument and donecb passes the user's message
96 // and a boolean indicating if the prompt was canceled
97 func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(string), donecb func(string, bool)) {
98 // If we get another prompt mid-prompt we cancel the one getting overwritten
99 if i.HasPrompt {
100 i.DonePrompt(true)
101 }
102
103 if _, ok := i.History[ptype]; !ok {
104 i.History[ptype] = []string{""}
105 } else {
106 i.History[ptype] = append(i.History[ptype], "")
107 }
108 i.HistoryNum = len(i.History[ptype]) - 1
109 i.HistorySearch = false
110
111 i.PromptType = ptype
112 i.Msg = prompt
113 i.HasPrompt = true
114 i.HasMessage, i.HasError, i.HasYN = false, false, false
115 i.HasGutter = false
116 i.PromptCallback = donecb
117 i.EventCallback = eventcb
118 i.Buffer.Insert(i.Buffer.Start(), msg)
119 }
120
121 // YNPrompt creates a yes or no prompt, and the callback returns the yes/no result and whether
122 // the prompt was canceled
123 func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
124 if i.HasPrompt {
125 i.DonePrompt(true)
126 }
127
128 i.Msg = prompt
129 i.HasPrompt = true
130 i.HasYN = true
131 i.HasMessage, i.HasError = false, false
132 i.HasGutter = false
133 i.YNCallback = donecb
134 }
135
136 // DonePrompt finishes the current prompt and indicates whether or not it was canceled
137 func (i *InfoBuf) DonePrompt(canceled bool) {
138 hadYN := i.HasYN
139 i.HasPrompt = false
140 i.HasYN = false
141 i.HasGutter = false
142 if !hadYN {
143 if i.PromptCallback != nil {
144 if canceled {
145 i.Replace(i.Start(), i.End(), "")
146 h := i.History[i.PromptType]
147 i.History[i.PromptType] = h[:len(h)-1]
148 i.PromptCallback("", true)
149 } else {
150 resp := string(i.LineBytes(0))
151 i.Replace(i.Start(), i.End(), "")
152 h := i.History[i.PromptType]
153 h[len(h)-1] = resp
154
155 // avoid duplicates
156 for j := len(h) - 2; j >= 0; j-- {
157 if h[j] == h[len(h)-1] {
158 i.History[i.PromptType] = append(h[:j], h[j+1:]...)
159 break
160 }
161 }
162
163 i.PromptCallback(resp, false)
164 }
165 // i.PromptCallback = nil
166 }
167 }
168 if i.YNCallback != nil && hadYN {
169 i.YNCallback(i.YNResp, canceled)
170 }
171 }
172
173 // Reset resets the infobuffer's msg and info
174 func (i *InfoBuf) Reset() {
175 i.Msg = ""
176 i.HasPrompt, i.HasMessage, i.HasError = false, false, false
177 i.HasGutter = false
178 }