Fix the selection style
This commit is contained in:
parent
3981cdfd45
commit
a9d729e894
63
filter.go
63
filter.go
|
@ -34,16 +34,20 @@ var ymax int
|
||||||
|
|
||||||
// TODO: Make the highlight style configuratble.
|
// TODO: Make the highlight style configuratble.
|
||||||
// The style to be used with the current line
|
// The style to be used with the current line
|
||||||
var highlight tcell.Style
|
var highlight,
|
||||||
var selectedStyle tcell.Style
|
selectedStyle,
|
||||||
var helpStyle tcell.Style
|
helpStyle,
|
||||||
|
selectionStyle,
|
||||||
|
selectionArrow,
|
||||||
|
normalArrow tcell.Style
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
Current int
|
Current int
|
||||||
InSelectionMode bool
|
InSelectionMode bool
|
||||||
UpperBound int
|
SelectionStartLine int
|
||||||
LowerBound int
|
UpperBound int
|
||||||
Screen *tcell.Screen
|
LowerBound int
|
||||||
|
Screen *tcell.Screen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draws the given `text` to the screen `s` at the given coordinates. Remember that
|
// 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)
|
drawText(*s.Screen, 0, 0, xmax-1, 1, helpStyle, status)
|
||||||
for i, item := range (*inputs)[s.LowerBound:s.UpperBound] {
|
for i, item := range (*inputs)[s.LowerBound:s.UpperBound] {
|
||||||
style := tcell.StyleDefault
|
style := tcell.StyleDefault
|
||||||
|
arrowStyle := normalArrow
|
||||||
|
|
||||||
var txt string
|
var txt string
|
||||||
|
|
||||||
if item.Selected {
|
if item.Selected {
|
||||||
txt = fmt.Sprintf(" X %s", item.Text)
|
txt = fmt.Sprintf(" ✔ %s", item.Text)
|
||||||
style = selectedStyle
|
style = selectedStyle
|
||||||
} else {
|
} else {
|
||||||
txt = fmt.Sprintf(" %s", item.Text)
|
txt = fmt.Sprintf(" %s", item.Text)
|
||||||
|
@ -84,12 +90,22 @@ func Render(s State, inputs Items) {
|
||||||
style = highlight
|
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)
|
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.
|
// 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
|
// 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)))
|
drawText(*s.Screen, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-1-len(txt)))
|
||||||
if s.Current == s.LowerBound+i {
|
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
|
y += 1
|
||||||
|
@ -105,9 +121,12 @@ func main() {
|
||||||
|
|
||||||
// TODO: Make the default style configurable
|
// TODO: Make the default style configurable
|
||||||
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
|
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
|
||||||
highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite).Bold(true)
|
highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault).Bold(true)
|
||||||
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow)
|
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorPlum)
|
||||||
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
|
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
|
done := false
|
||||||
|
|
||||||
|
@ -140,14 +159,13 @@ func main() {
|
||||||
}
|
}
|
||||||
defer quit()
|
defer quit()
|
||||||
|
|
||||||
startOfSelection := 0
|
|
||||||
|
|
||||||
state := State{
|
state := State{
|
||||||
Current: 0,
|
Current: 0,
|
||||||
Screen: &s,
|
SelectionStartLine: 0,
|
||||||
LowerBound: 0,
|
Screen: &s,
|
||||||
UpperBound: min(ymax-3, len(*input)),
|
LowerBound: 0,
|
||||||
InSelectionMode: false,
|
UpperBound: min(ymax-3, len(*input)),
|
||||||
|
InSelectionMode: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event loop
|
// Event loop
|
||||||
|
@ -205,12 +223,13 @@ func main() {
|
||||||
state.Current = 0
|
state.Current = 0
|
||||||
} else if ev.Key() == tcell.KeyCtrlSpace {
|
} else if ev.Key() == tcell.KeyCtrlSpace {
|
||||||
if state.InSelectionMode {
|
if state.InSelectionMode {
|
||||||
start := min(startOfSelection, state.Current)
|
start := min(state.SelectionStartLine, state.Current)
|
||||||
end := max(startOfSelection, state.Current)
|
end := max(state.SelectionStartLine, state.Current)
|
||||||
toggleItems(input, start, end)
|
toggleItems(input, start, end)
|
||||||
} else {
|
} else {
|
||||||
startOfSelection = state.Current
|
state.SelectionStartLine = state.Current
|
||||||
}
|
}
|
||||||
|
|
||||||
state.InSelectionMode = !state.InSelectionMode
|
state.InSelectionMode = !state.InSelectionMode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue