Fix the selection style

This commit is contained in:
Sameer Rahmani 2022-11-25 15:18:06 +00:00
parent 3981cdfd45
commit a9d729e894
1 changed files with 41 additions and 22 deletions

View File

@ -34,16 +34,20 @@ var ymax int
// TODO: Make the highlight style configuratble.
// The style to be used with the current line
var highlight tcell.Style
var selectedStyle tcell.Style
var helpStyle tcell.Style
var highlight,
selectedStyle,
helpStyle,
selectionStyle,
selectionArrow,
normalArrow tcell.Style
type State struct {
Current int
InSelectionMode bool
UpperBound int
LowerBound int
Screen *tcell.Screen
Current int
InSelectionMode bool
SelectionStartLine int
UpperBound int
LowerBound int
Screen *tcell.Screen
}
// Draws the given `text` to the screen `s` at the given coordinates. Remember that
@ -71,10 +75,12 @@ func Render(s State, inputs Items) {
drawText(*s.Screen, 0, 0, xmax-1, 1, helpStyle, status)
for i, item := range (*inputs)[s.LowerBound:s.UpperBound] {
style := tcell.StyleDefault
arrowStyle := normalArrow
var txt string
if item.Selected {
txt = fmt.Sprintf(" X %s", item.Text)
txt = fmt.Sprintf(" %s", item.Text)
style = selectedStyle
} else {
txt = fmt.Sprintf(" %s", item.Text)
@ -84,12 +90,22 @@ func Render(s State, inputs Items) {
style = highlight
}
if s.InSelectionMode {
low := min(s.SelectionStartLine, s.Current)
high := max(s.SelectionStartLine, s.Current)
if i >= low && i <= high {
style = selectionStyle
arrowStyle = selectionArrow
}
}
drawText(*s.Screen, 0, y, xmax-1, y+1, style, txt)
// Clean up the rest of the line that might've been filled with a previous render.
// This is a hack to NOT use `Clear` on each loop
drawText(*s.Screen, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-1-len(txt)))
if s.Current == s.LowerBound+i {
drawText(*s.Screen, 0, y, 1, y+1, tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true), ">")
drawText(*s.Screen, 0, y, 1, y+1, arrowStyle, ">")
}
y += 1
@ -105,9 +121,12 @@ func main() {
// TODO: Make the default style configurable
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite).Bold(true)
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow)
highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault).Bold(true)
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorPlum)
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
selectionStyle = tcell.StyleDefault.Background(tcell.ColorRebeccaPurple).Foreground(tcell.ColorDefault)
selectionArrow = tcell.StyleDefault.Background(tcell.ColorRebeccaPurple).Foreground(tcell.ColorGreen).Bold(true)
normalArrow = tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
done := false
@ -140,14 +159,13 @@ func main() {
}
defer quit()
startOfSelection := 0
state := State{
Current: 0,
Screen: &s,
LowerBound: 0,
UpperBound: min(ymax-3, len(*input)),
InSelectionMode: false,
Current: 0,
SelectionStartLine: 0,
Screen: &s,
LowerBound: 0,
UpperBound: min(ymax-3, len(*input)),
InSelectionMode: false,
}
// Event loop
@ -205,12 +223,13 @@ func main() {
state.Current = 0
} else if ev.Key() == tcell.KeyCtrlSpace {
if state.InSelectionMode {
start := min(startOfSelection, state.Current)
end := max(startOfSelection, state.Current)
start := min(state.SelectionStartLine, state.Current)
end := max(state.SelectionStartLine, state.Current)
toggleItems(input, start, end)
} else {
startOfSelection = state.Current
state.SelectionStartLine = state.Current
}
state.InSelectionMode = !state.InSelectionMode
}
}