commit b95ee4f1f111438a10704a8fb05dcdf5404b73eb Author: zervo Date: Thu Mar 13 07:54:44 2025 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f14d3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +stopwatch diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d65c44f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module stopwatch + +go 1.24.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8752921 --- /dev/null +++ b/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + var start time.Time + var elapsed time.Duration + var running bool + + go func() { + for { + time.Sleep(1 * time.Second) + if running { + elapsed = time.Since(start) + fmt.Printf("\rElapsed Time: %02d:%02d:%02d", int(elapsed.Hours()), int(elapsed.Minutes())%60, int(elapsed.Seconds())%60) + } + } + }() + + for { + fmt.Println("> s: Start, p: Pause, r: Reset, q: Quit") + var input string + fmt.Scanln(&input) + + switch input { + case "s": + if !running { + start = time.Now().Add(-elapsed) + running = true + fmt.Println("Stopwatch started.") + } + case "p": + if running { + running = false + fmt.Printf("\nPaused at: %02d:%02d:%02d\n", int(elapsed.Hours()), int(elapsed.Minutes())%60, int(elapsed.Seconds())%60) + } + case "r": + start = time.Time{} + elapsed = 0 + running = false + fmt.Println("\nStopwatch reset.") + case "q": + fmt.Println("\nExiting") + return + default: + fmt.Println("\nInvalid input, please try again.") + } + } +}