-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (66 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"time"
)
func main() {
returnPressed := make(chan bool)
go reportReturnPresses(returnPressed)
segment := 1
isBreathing := false
secondTicker := time.NewTicker(time.Second)
secondsLeftInSegment := 4 * 60
heldBreathFor := 0
fmt.Printf("#1: Held breath for 0:00 min... press return to breathe.")
for {
select {
case <-returnPressed:
isBreathing = true
if segment == 8 {
fmt.Printf(
"\r#%d: Held breath for %d:%02d min. \n",
segment, heldBreathFor/60, heldBreathFor%60)
return
}
case <-secondTicker.C:
secondsLeftInSegment--
if !isBreathing {
heldBreathFor++
fmt.Printf(
"\r#%d: Held breath for %d:%02d min... press return to breathe.",
segment, heldBreathFor/60, heldBreathFor%60)
} else {
fmt.Printf(
"\r#%d: Held breath for %d:%02d min. Wait %d:%02d min until the next breath hold.",
segment,
heldBreathFor/60, heldBreathFor%60,
secondsLeftInSegment/60, secondsLeftInSegment%60)
}
if secondsLeftInSegment < 1 {
if segment == 8 {
fmt.Printf(
"\r#%d: Held breath for %d:%02d min. \n",
segment, heldBreathFor/60, heldBreathFor%60)
return
}
fmt.Printf(
"\r#%d: Held breath for %d:%02d min, breathed for %d:%02d min. \n",
segment,
heldBreathFor/60, heldBreathFor%60,
(4*60-heldBreathFor)/60, (4*60-heldBreathFor)%60)
isBreathing = false
secondsLeftInSegment = 4 * 60
heldBreathFor = 0
segment++
fmt.Printf("#%d: Held breath for 0:00 min... press return to breathe.", segment)
}
}
}
}
func reportReturnPresses(returnPressed chan bool) {
for {
fmt.Scanln()
fmt.Print("\x1b[1A") // ANSI Escape code to move the cursor up one line.
returnPressed <- true
}
}