From 459ab8d73982753f5dede395ffee788f4d3b7b35 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 25 Nov 2022 13:51:30 +0000 Subject: [PATCH] Add a very basic selection mode --- filter.go | 17 ++++++++++++++++- utils.go | 6 ++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/filter.go b/filter.go index 450db9d..fb4e743 100644 --- a/filter.go +++ b/filter.go @@ -38,6 +38,13 @@ var highlight tcell.Style var selectedStyle tcell.Style var helpStyle tcell.Style +type State struct { + Current int + InSelectionMode bool + UpperBound int + LowerBound int +} + // Draws the given `text` to the screen `s` at the given coordinates. Remember that // the text will be wrapped if it overflow. func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) { @@ -135,6 +142,8 @@ func main() { lowerBound := 0 upperBound := min(ymax-3, len(*input)) + inSelectedMode := false + startOfSelection := 0 // Event loop for !done { @@ -189,7 +198,13 @@ func main() { upperBound = ymax - 2 lowerBound = 0 currentLine = 0 - + } else if ev.Key() == tcell.KeyCtrlSpace { + if inSelectedMode { + toggleItems(input, startOfSelection, currentLine) + } else { + startOfSelection = currentLine + } + inSelectedMode = !inSelectedMode } } } diff --git a/utils.go b/utils.go index 0d3ff6c..ca49aeb 100644 --- a/utils.go +++ b/utils.go @@ -36,6 +36,12 @@ func NewItem(text string) *Item { } } +func toggleItems(items Items, start int, end int) { + for _, item := range (*items)[start:end] { + item.Selected = !item.Selected + } +} + // Pffff Golang, right? :D func min(a, b int) int { if a < b {