-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.go
62 lines (52 loc) · 1.37 KB
/
countdown.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
package main
import (
"fmt"
"os"
"os/exec"
"time"
)
// function to act as a countdown timer
func CountdownTimer() {
var timer int
// check if the the timer argument is passed
if len(os.Args) > 2 {
// if the timer argument is passed, set the timer to the value of the argument
fmt.Sscanf(os.Args[2], "%d", &timer)
} else {
// if the timer argument is not passed, ask the user to enter the timer value
fmt.Print("Enter the timer value in seconds: ")
fmt.Scanf("%d", &timer)
// remove the previous line from the terminal
fmt.Printf("\033[1A\033[2K")
}
// if the timer value is less than 1, exit the program
if timer < 1 {
// print that time is up
fmt.Println("Time is up!")
return
}
// hide the cursor when the timer is running
_, _ = exec.Command("tput", "civis").Output()
// show the cursor again when the program exits
defer func() {
_, _ = exec.Command("tput", "cnorm").Output()
}()
// print the remaining time and keep updating it
// the timer will stop when the time is up or when the user presses Ctrl+C
// clear the the last printed time when the timer is updated
for {
fmt.Print("\033[K")
fmt.Printf("\r%d", timer)
fmt.Print("\033[K")
time.Sleep(time.Second)
timer--
if timer < 0 {
fmt.Println()
break
}
}
// clear the the last printed line
fmt.Print("\033[1A\033[2K")
// print that time is up
fmt.Println("Time is up!")
}