Refactor the code by adding State type

This commit is contained in:
Sameer Rahmani 2022-11-25 14:06:05 +00:00
parent 459ab8d739
commit 3981cdfd45
2 changed files with 49 additions and 35 deletions

View File

@ -43,6 +43,7 @@ type State struct {
InSelectionMode bool
UpperBound int
LowerBound int
Screen *tcell.Screen
}
// Draws the given `text` to the screen `s` at the given coordinates. Remember that
@ -64,11 +65,11 @@ func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string
}
// Main render function that gets called during the event loop.
func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, current int) {
func Render(s State, inputs Items) {
y := 2
status := fmt.Sprintf("Navigation: [UP or C-p] [Down or C-n] | Select: SPACE | Cancel: [q ESC] | Done: [Enter] | Line: %d", current+1)
drawText(s, 0, 0, xmax-1, 1, helpStyle, status)
for i, item := range (*inputs)[lowerBound:upperBound] {
status := fmt.Sprintf("Navigation: [UP or C-p] [Down or C-n] | Select: SPACE | Cancel: [q ESC] | Done: [Enter] | Line: %d", s.Current+1)
drawText(*s.Screen, 0, 0, xmax-1, 1, helpStyle, status)
for i, item := range (*inputs)[s.LowerBound:s.UpperBound] {
style := tcell.StyleDefault
var txt string
@ -79,16 +80,16 @@ func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, curren
txt = fmt.Sprintf(" %s", item.Text)
}
if current == lowerBound+i {
if s.Current == s.LowerBound+i {
style = highlight
}
drawText(s, 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.
// This is a hack to NOT use `Clear` on each loop
drawText(s, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-1-len(txt)))
if current == lowerBound+i {
drawText(s, 0, y, 1, y+1, tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true), ">")
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), ">")
}
y += 1
@ -108,7 +109,6 @@ func main() {
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow)
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
currentLine := 0
done := false
// Initialize screen
@ -140,14 +140,19 @@ func main() {
}
defer quit()
lowerBound := 0
upperBound := min(ymax-3, len(*input))
inSelectedMode := false
startOfSelection := 0
state := State{
Current: 0,
Screen: &s,
LowerBound: 0,
UpperBound: min(ymax-3, len(*input)),
InSelectionMode: false,
}
// Event loop
for !done {
Render(s, input, lowerBound, upperBound, currentLine)
Render(state, input)
// Update screen
s.Show()
@ -169,42 +174,44 @@ func main() {
s.Sync()
} else if ev.Rune() == ' ' {
// Mark the current line as selected
item := (*input)[currentLine]
item := (*input)[state.Current]
item.Selected = !item.Selected
} else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP {
if currentLine > 0 {
currentLine -= 1
if state.Current > 0 {
state.Current -= 1
}
if currentLine <= lowerBound && currentLine != 0 {
lowerBound -= 1
upperBound -= 1
if state.Current <= state.LowerBound && state.Current != 0 {
state.LowerBound -= 1
state.UpperBound -= 1
}
} else if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN {
if currentLine < len(*input)-1 {
currentLine += 1
if state.Current < len(*input)-1 {
state.Current += 1
}
if currentLine >= upperBound && currentLine != len(*input)-1 {
lowerBound += 1
upperBound += 1
if state.Current >= state.UpperBound && state.Current != len(*input)-1 {
state.LowerBound += 1
state.UpperBound += 1
}
} else if ev.Key() == tcell.KeyCtrlE || ev.Key() == tcell.KeyEnd {
upperBound = len(*input)
lowerBound = upperBound - ymax + 2
currentLine = len(*input) - 1
state.UpperBound = len(*input)
state.LowerBound = state.UpperBound - ymax + 2
state.Current = len(*input) - 1
} else if ev.Key() == tcell.KeyCtrlA || ev.Key() == tcell.KeyHome {
upperBound = ymax - 2
lowerBound = 0
currentLine = 0
state.UpperBound = ymax - 2
state.LowerBound = 0
state.Current = 0
} else if ev.Key() == tcell.KeyCtrlSpace {
if inSelectedMode {
toggleItems(input, startOfSelection, currentLine)
if state.InSelectionMode {
start := min(startOfSelection, state.Current)
end := max(startOfSelection, state.Current)
toggleItems(input, start, end)
} else {
startOfSelection = currentLine
startOfSelection = state.Current
}
inSelectedMode = !inSelectedMode
state.InSelectionMode = !state.InSelectionMode
}
}
}

View File

@ -50,6 +50,13 @@ func min(a, b int) int {
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// Reads the input from stdin and makes sure that the current
// process is being run in a pipeline and converts the data
// to an array of items.