summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.go10
-rw-r--r--src/util/util_test.go18
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")
+ }
+}