summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.go12
-rw-r--r--src/util/util_test.go10
2 files changed, 19 insertions, 3 deletions
diff --git a/src/util/util.go b/src/util/util.go
index ec5a1ea0..c8301363 100644
--- a/src/util/util.go
+++ b/src/util/util.go
@@ -144,12 +144,22 @@ func IsTty(file *os.File) bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}
+// RunOnce runs the given function only once
+func RunOnce(f func()) func() {
+ once := Once(true)
+ return func() {
+ if once() {
+ f()
+ }
+ }
+}
+
// 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
+ state = !nextResponse
return prevState
}
}
diff --git a/src/util/util_test.go b/src/util/util_test.go
index 013f3c23..36d71bde 100644
--- a/src/util/util_test.go
+++ b/src/util/util_test.go
@@ -137,8 +137,11 @@ func TestOnce(t *testing.T) {
if o() {
t.Error("Expected: false")
}
- if o() {
- t.Error("Expected: false")
+ if !o() {
+ t.Error("Expected: true")
+ }
+ if !o() {
+ t.Error("Expected: true")
}
o = Once(true)
@@ -148,6 +151,9 @@ func TestOnce(t *testing.T) {
if o() {
t.Error("Expected: false")
}
+ if o() {
+ t.Error("Expected: false")
+ }
}
func TestRunesWidth(t *testing.T) {