Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add picker component #621

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
62a6672
chore: scaffold empty picker component
Broderick-Westrope Sep 19, 2024
e6b33cb
feat: define keymap
Broderick-Westrope Sep 19, 2024
02cf2c4
feat: add ListState; implement Model
Broderick-Westrope Sep 19, 2024
5c4ad9d
feat: add Model opt to hide indicators
Broderick-Westrope Sep 19, 2024
533274c
feat: add IntState; allow cycles; allow custom DisplayFunc
Broderick-Westrope Sep 20, 2024
d565b95
feat: add ignore max & min to IntState
Broderick-Westrope Sep 20, 2024
725f3f5
fix: clamp IntState selection between min & max
Broderick-Westrope Sep 20, 2024
9c1984b
feat: add methods for getting indicators
Broderick-Westrope Sep 20, 2024
05e0d07
feat: make Model fields exported
Broderick-Westrope Sep 20, 2024
f33ff4f
test: add ListState unit tests
Broderick-Westrope Sep 20, 2024
095aa3f
test: add TestNewIntState
Broderick-Westrope Sep 20, 2024
31894e9
test: add TestIntState_GetValue
Broderick-Westrope Sep 20, 2024
f4009e3
test: add TestIntState_Next
Broderick-Westrope Sep 20, 2024
703c5dc
test: add TestIntState_Prev
Broderick-Westrope Sep 20, 2024
b829c18
feat: add styles
Broderick-Westrope Sep 20, 2024
08508eb
feat: add jumping
Broderick-Westrope Sep 20, 2024
2327f73
chore: add doc comment for Model
Broderick-Westrope Sep 20, 2024
446bc18
test: add TestIntState_JumpForward & TestIntState_JumpBackward
Broderick-Westrope Sep 20, 2024
fa8a658
test: add TestIntState_NextExists
Broderick-Westrope Sep 20, 2024
12da11b
test: add TestIntState_PrevExists
Broderick-Westrope Sep 20, 2024
196a021
test: add TestListState_NextExists & TestListState_PrevExists
Broderick-Westrope Sep 20, 2024
aea9f2f
test: add TestListState_JumpForward & TestListState_JumpBackward
Broderick-Westrope Sep 20, 2024
e3f4dbb
test: add TestNewModel
Broderick-Westrope Sep 20, 2024
f3207c3
test: add TestModel_GetValue
Broderick-Westrope Sep 20, 2024
2451ced
test: add TestGetIndicator
Broderick-Westrope Sep 20, 2024
2bf8639
test: add TestModel_View
Broderick-Westrope Sep 20, 2024
ac02662
refactor: rename Model constructor to New
Broderick-Westrope Sep 20, 2024
a3de379
feat: add stepping
Broderick-Westrope Sep 20, 2024
f5e19c0
feat: finalise State StepForward & StepBackward methods
Broderick-Westrope Sep 21, 2024
967912f
test: add StepSize in TestNew
Broderick-Westrope Sep 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions picker/intstate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package picker

type IntState struct {
min int
max int
selection int
ignoreMin bool
ignoreMax bool
}

func NewIntState(min, max, selection int, ignoreMin, ignoreMax bool) *IntState {
switch {
case selection < min && !ignoreMin:
selection = min
case selection > max && !ignoreMax:
selection = max
}

return &IntState{
min: min,
max: max,
ignoreMin: ignoreMin,
ignoreMax: ignoreMax,
selection: selection,
}
}

func (s *IntState) GetValue() interface{} {
return s.selection
}

func (s *IntState) NextExists() bool {
return s.ignoreMax ||
s.selection < s.max
}

func (s *IntState) PrevExists() bool {
return s.ignoreMin ||
s.selection > s.min
}

func (s *IntState) Next(canCycle bool) {
switch {
case s.NextExists():
s.selection++

case canCycle:
s.selection = s.min
}
}

func (s *IntState) Prev(canCycle bool) {
switch {
case s.PrevExists():
s.selection--

case canCycle:
s.selection = s.max
}
}

func (s *IntState) StepForward(size int) {
s.selection += size
if s.selection > s.max && !s.ignoreMax {
s.selection = s.max
}
}

func (s *IntState) StepBackward(size int) {
s.selection -= size
if s.selection < s.min && !s.ignoreMin {
s.selection = s.min
}
}

func (s *IntState) JumpForward() {
s.selection = s.max
}

func (s *IntState) JumpBackward() {
s.selection = s.min
}
Loading