diff options
author | Hubert Hirtz <hubert@hirtz.pm> | 2021-10-19 21:13:25 +0200 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtz.pm> | 2021-10-19 21:13:25 +0200 |
commit | ce78dab820c632550c7e767e4601d0d4d2e34de2 (patch) | |
tree | c14d06b0f2d44a6052e0c9d4daa294f7d61a641e | |
parent | Don't forget to close outgoing chan on debug (diff) |
Only show buffer numbers when necessary
Saves space
also removed non-edition related method IsCommand out of editor.go
-rw-r--r-- | app.go | 14 | ||||
-rw-r--r-- | msgstore.go | 12 | ||||
-rw-r--r-- | ui/buffers.go | 16 | ||||
-rw-r--r-- | ui/editor.go | 9 | ||||
-rw-r--r-- | ui/ui.go | 13 | ||||
-rw-r--r-- | window.go | 18 |
6 files changed, 63 insertions, 19 deletions
@@ -24,6 +24,12 @@ const ( ircEvent ) +func isCommand(input []rune) bool { + // Command can't start with two slashes because that's an escape for + // a literal slash in the message + return len(input) >= 1 && input[0] == '/' && !(len(input) >= 2 && input[1] == '/') +} + type bound struct { first time.Time last time.Time @@ -164,6 +170,7 @@ func (app *App) eventLoop() { if !app.pasting { app.setStatus() app.updatePrompt() + app.setBufferNumbers() var currentMembers []irc.Member if app.s != nil { currentMembers = app.s.Names(app.win.CurrentBuffer()) @@ -812,9 +819,10 @@ func (app *App) typing() { if buffer == Home { return } - if app.win.InputLen() == 0 { + input := app.win.InputContent() + if len(input) == 0 { app.s.TypingStop(buffer) - } else if !app.win.InputIsCommand() { + } else if !isCommand(input) { app.s.Typing(app.win.CurrentBuffer()) } } @@ -916,7 +924,7 @@ func (app *App) formatMessage(ev irc.MessageEvent) (buffer string, line ui.Line, // updatePrompt changes the prompt text according to the application context. func (app *App) updatePrompt() { buffer := app.win.CurrentBuffer() - command := app.win.InputIsCommand() + command := isCommand(app.win.InputContent()) var prompt ui.StyledString if buffer == Home || command { prompt = ui.Styled(">", diff --git a/msgstore.go b/msgstore.go new file mode 100644 index 0000000..d42b371 --- /dev/null +++ b/msgstore.go @@ -0,0 +1,12 @@ +package senpai + +type Buffer struct { +} + +type BufferID int + +var nextBufferID BufferID = 0 + +type MsgStore struct { + buffers map[BufferID]*Buffer +} diff --git a/ui/buffers.go b/ui/buffers.go index bf045ca..79d2cf6 100644 --- a/ui/buffers.go +++ b/ui/buffers.go @@ -191,6 +191,8 @@ type BufferList struct { tlInnerWidth int tlHeight int + + showBufferNumbers bool } // NewBufferList returns a new BufferList. @@ -223,6 +225,10 @@ func (bs *BufferList) To(i int) bool { return false } +func (bs *BufferList) ShowBufferNumbers(enabled bool) { + bs.showBufferNumbers = enabled +} + func (bs *BufferList) Next() { bs.current = (bs.current + 1) % len(bs.list) bs.list[bs.current].highlights = 0 @@ -386,11 +392,13 @@ func (bs *BufferList) DrawVerticalBufferList(screen tcell.Screen, x0, y0, width, if i == bs.clicked { st = st.Reverse(true) } - indexText := fmt.Sprintf("%d:", i) - for ; x < x0+indexPadding-len(indexText); x++ { - screen.SetContent(x, y, ' ', nil, tcell.StyleDefault) + if bs.showBufferNumbers { + indexText := fmt.Sprintf("%d:", i) + for ; x < x0+indexPadding-len(indexText); x++ { + screen.SetContent(x, y, ' ', nil, tcell.StyleDefault) + } + printString(screen, &x, y, Styled(indexText, st.Foreground(tcell.ColorGrey))) } - printString(screen, &x, y, Styled(indexText, st.Foreground(tcell.ColorGrey))) title := truncate(b.title, width-(x-x0), "\u2026") printString(screen, &x, y, Styled(title, st)) if 0 < b.highlights { diff --git a/ui/editor.go b/ui/editor.go index 5e3105b..f4bd247 100644 --- a/ui/editor.go +++ b/ui/editor.go @@ -63,12 +63,9 @@ func (e *Editor) Resize(width int) { e.width = width } -func (e *Editor) IsCommand() bool { - line := e.text[e.lineIdx] - - // Command can't start with two slashes because that's an escape for - // a literal slash in the message - return len(line) >= 1 && line[0] == '/' && !(len(line) >= 2 && line[1] == '/') +// Content result must not be modified. +func (e *Editor) Content() []rune { + return e.text[e.lineIdx] } func (e *Editor) TextLen() int { @@ -112,6 +112,10 @@ func (ui *UI) GoToBufferNo(i int) { } } +func (ui *UI) ShowBufferNumbers(enable bool) { + ui.bs.ShowBufferNumbers(enable) +} + func (ui *UI) ScrollUp() { ui.bs.ScrollUp(ui.bs.tlHeight / 2) } @@ -192,12 +196,9 @@ func (ui *UI) SetPrompt(prompt StyledString) { ui.prompt = prompt } -func (ui *UI) InputIsCommand() bool { - return ui.e.IsCommand() -} - -func (ui *UI) InputLen() int { - return ui.e.TextLen() +// InputContent result must not be modified. +func (ui *UI) InputContent() []rune { + return ui.e.Content() } func (ui *UI) InputRune(r rune) { @@ -61,6 +61,24 @@ func (app *App) setStatus() { app.win.SetStatus(status) } +func (app *App) setBufferNumbers() { + input := app.win.InputContent() + if len(input) < 2 || input[0] != '/' { + app.win.ShowBufferNumbers(false) + return + } + commandEnd := len(input) + for i := 0; i < len(input); i++ { + if input[i] == ' ' { + commandEnd = i + break + } + } + command := string(input[:commandEnd]) + showBufferNumbers := strings.HasPrefix("/buffer", command) + app.win.ShowBufferNumbers(showBufferNumbers) +} + func identColor(ident string) tcell.Color { h := fnv.New32() _, _ = h.Write([]byte(ident)) |