commit ce78dab
Hubert Hirtz
·
2021-10-19 19:13:25 +0000 UTC
parent e60ad17
Only show buffer numbers when necessary Saves space also removed non-edition related method IsCommand out of editor.go
6 files changed,
+63,
-19
M
app.go
+11,
-3
1@@ -24,6 +24,12 @@ const (
2 ircEvent
3 )
4
5+func isCommand(input []rune) bool {
6+ // Command can't start with two slashes because that's an escape for
7+ // a literal slash in the message
8+ return len(input) >= 1 && input[0] == '/' && !(len(input) >= 2 && input[1] == '/')
9+}
10+
11 type bound struct {
12 first time.Time
13 last time.Time
14@@ -164,6 +170,7 @@ func (app *App) eventLoop() {
15 if !app.pasting {
16 app.setStatus()
17 app.updatePrompt()
18+ app.setBufferNumbers()
19 var currentMembers []irc.Member
20 if app.s != nil {
21 currentMembers = app.s.Names(app.win.CurrentBuffer())
22@@ -812,9 +819,10 @@ func (app *App) typing() {
23 if buffer == Home {
24 return
25 }
26- if app.win.InputLen() == 0 {
27+ input := app.win.InputContent()
28+ if len(input) == 0 {
29 app.s.TypingStop(buffer)
30- } else if !app.win.InputIsCommand() {
31+ } else if !isCommand(input) {
32 app.s.Typing(app.win.CurrentBuffer())
33 }
34 }
35@@ -916,7 +924,7 @@ func (app *App) formatMessage(ev irc.MessageEvent) (buffer string, line ui.Line,
36 // updatePrompt changes the prompt text according to the application context.
37 func (app *App) updatePrompt() {
38 buffer := app.win.CurrentBuffer()
39- command := app.win.InputIsCommand()
40+ command := isCommand(app.win.InputContent())
41 var prompt ui.StyledString
42 if buffer == Home || command {
43 prompt = ui.Styled(">",
+12,
-0
1@@ -0,0 +1,12 @@
2+package senpai
3+
4+type Buffer struct {
5+}
6+
7+type BufferID int
8+
9+var nextBufferID BufferID = 0
10+
11+type MsgStore struct {
12+ buffers map[BufferID]*Buffer
13+}
+12,
-4
1@@ -191,6 +191,8 @@ type BufferList struct {
2
3 tlInnerWidth int
4 tlHeight int
5+
6+ showBufferNumbers bool
7 }
8
9 // NewBufferList returns a new BufferList.
10@@ -223,6 +225,10 @@ func (bs *BufferList) To(i int) bool {
11 return false
12 }
13
14+func (bs *BufferList) ShowBufferNumbers(enabled bool) {
15+ bs.showBufferNumbers = enabled
16+}
17+
18 func (bs *BufferList) Next() {
19 bs.current = (bs.current + 1) % len(bs.list)
20 bs.list[bs.current].highlights = 0
21@@ -386,11 +392,13 @@ func (bs *BufferList) DrawVerticalBufferList(screen tcell.Screen, x0, y0, width,
22 if i == bs.clicked {
23 st = st.Reverse(true)
24 }
25- indexText := fmt.Sprintf("%d:", i)
26- for ; x < x0+indexPadding-len(indexText); x++ {
27- screen.SetContent(x, y, ' ', nil, tcell.StyleDefault)
28+ if bs.showBufferNumbers {
29+ indexText := fmt.Sprintf("%d:", i)
30+ for ; x < x0+indexPadding-len(indexText); x++ {
31+ screen.SetContent(x, y, ' ', nil, tcell.StyleDefault)
32+ }
33+ printString(screen, &x, y, Styled(indexText, st.Foreground(tcell.ColorGrey)))
34 }
35- printString(screen, &x, y, Styled(indexText, st.Foreground(tcell.ColorGrey)))
36 title := truncate(b.title, width-(x-x0), "\u2026")
37 printString(screen, &x, y, Styled(title, st))
38 if 0 < b.highlights {
+3,
-6
1@@ -63,12 +63,9 @@ func (e *Editor) Resize(width int) {
2 e.width = width
3 }
4
5-func (e *Editor) IsCommand() bool {
6- line := e.text[e.lineIdx]
7-
8- // Command can't start with two slashes because that's an escape for
9- // a literal slash in the message
10- return len(line) >= 1 && line[0] == '/' && !(len(line) >= 2 && line[1] == '/')
11+// Content result must not be modified.
12+func (e *Editor) Content() []rune {
13+ return e.text[e.lineIdx]
14 }
15
16 func (e *Editor) TextLen() int {
M
ui/ui.go
+7,
-6
1@@ -112,6 +112,10 @@ func (ui *UI) GoToBufferNo(i int) {
2 }
3 }
4
5+func (ui *UI) ShowBufferNumbers(enable bool) {
6+ ui.bs.ShowBufferNumbers(enable)
7+}
8+
9 func (ui *UI) ScrollUp() {
10 ui.bs.ScrollUp(ui.bs.tlHeight / 2)
11 }
12@@ -192,12 +196,9 @@ func (ui *UI) SetPrompt(prompt StyledString) {
13 ui.prompt = prompt
14 }
15
16-func (ui *UI) InputIsCommand() bool {
17- return ui.e.IsCommand()
18-}
19-
20-func (ui *UI) InputLen() int {
21- return ui.e.TextLen()
22+// InputContent result must not be modified.
23+func (ui *UI) InputContent() []rune {
24+ return ui.e.Content()
25 }
26
27 func (ui *UI) InputRune(r rune) {
+18,
-0
1@@ -61,6 +61,24 @@ func (app *App) setStatus() {
2 app.win.SetStatus(status)
3 }
4
5+func (app *App) setBufferNumbers() {
6+ input := app.win.InputContent()
7+ if len(input) < 2 || input[0] != '/' {
8+ app.win.ShowBufferNumbers(false)
9+ return
10+ }
11+ commandEnd := len(input)
12+ for i := 0; i < len(input); i++ {
13+ if input[i] == ' ' {
14+ commandEnd = i
15+ break
16+ }
17+ }
18+ command := string(input[:commandEnd])
19+ showBufferNumbers := strings.HasPrefix("/buffer", command)
20+ app.win.ShowBufferNumbers(showBufferNumbers)
21+}
22+
23 func identColor(ident string) tcell.Color {
24 h := fnv.New32()
25 _, _ = h.Write([]byte(ident))