diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2019-12-09 21:32:58 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2019-12-09 21:32:58 +0900 |
commit | 2b725a4db5e973d7ce90d1ac0996dcfd3c3f0116 (patch) | |
tree | c9eea79293181e3049ac974d3f8cc892bd27bf77 /src/util | |
parent | af1a5f130bbd2464131ec2ae91c538ace6801a73 (diff) |
Defer resetting multi-selection on reload
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/util.go | 10 | ||||
-rw-r--r-- | src/util/util_test.go | 18 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/util/util.go b/src/util/util.go index 95c4e1b4..0aa1d804 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -112,3 +112,13 @@ func DurWithin( func IsTty() bool { return isatty.IsTerminal(os.Stdin.Fd()) } + +// Once returns a function that returns the specified boolean value only once +func Once(nextResponse bool) func() bool { + state := nextResponse + return func() bool { + prevState := state + state = false + return prevState + } +} diff --git a/src/util/util_test.go b/src/util/util_test.go index d6a03d9d..4baa56fb 100644 --- a/src/util/util_test.go +++ b/src/util/util_test.go @@ -20,3 +20,21 @@ func TestContrain(t *testing.T) { t.Error("Expected", 3) } } + +func TestOnce(t *testing.T) { + o := Once(false) + if o() { + t.Error("Expected: false") + } + if o() { + t.Error("Expected: false") + } + + o = Once(true) + if !o() { + t.Error("Expected: true") + } + if o() { + t.Error("Expected: false") + } +} |