Skip to content

Commit

Permalink
Handle terminal resizing & revision of pausedText (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timgrau authored Sep 20, 2023
1 parent f77e862 commit 5a87ef3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
8 changes: 6 additions & 2 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ var defaultFont = Font{
}

var pausedText = Symbol{
"█▀▄ ▄▀▄ █ █ ▄▀▀ ██▀ █▀▄",
"█▀ █▀█ ▀▄█ ▄██ █▄▄ █▄▀",
"██████╗ █████╗ ██╗ ██╗ ███████╗ ███████╗ ██████╗ ",
"██╔══██╗ ██╔══██╗ ██║ ██║ ██╔════╝ ██╔════╝ ██╔══██╗",
"██████╔╝ ███████║ ██║ ██║ ███████╗ █████╗ ██║ ██║",
"██╔═══╝ ██╔══██║ ██║ ██║ ╚════██║ ██╔══╝ ██║ ██║",
"██║ ██║ ██║ ███████║ ███████║ ███████╗ ██████╔╝",
"╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚══════╝ ╚══════╝ ╚═════╝ ",
}
32 changes: 17 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ var (
timer *time.Timer
ticker *time.Ticker
queues chan termbox.Event
startDone bool
startX, startY int
w, h int
inputStartTime time.Time
isPaused bool
)
Expand Down Expand Up @@ -92,10 +91,10 @@ func countdown(totalDuration time.Duration, countUp bool, sayTheTime bool) {
timeLeft := totalDuration
var exitCode int
isPaused = false

w, h = termbox.Size()
start(timeLeft)

draw(durationToDraw(timeLeft, totalDuration, countUp))
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h)
if sayTheTime {
go say(timeLeft)
}
Expand All @@ -112,19 +111,27 @@ loop:
if pressTime := time.Now(); ev.Key == termbox.KeySpace && pressTime.Sub(inputStartTime) > inputDelayMS {
if isPaused {
start(timeLeft)
draw(durationToDraw(timeLeft, totalDuration, countUp))
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h)
} else {
stop()
drawPause()
drawPause(w, h)
}

isPaused = !isPaused
inputStartTime = time.Now()
}

if ev.Type == termbox.EventResize {
w, h = termbox.Size()
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h)

if isPaused {
drawPause(w, h)
}
}
case <-ticker.C:
timeLeft -= tick
draw(durationToDraw(timeLeft, totalDuration, countUp))
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h)
if sayTheTime {
go say(timeLeft)
}
Expand All @@ -139,17 +146,13 @@ loop:
}
}

func draw(d time.Duration) {
w, h := termbox.Size()
func draw(d time.Duration, w int, h int) {
clear()

str := format(d)
text := toText(str)

if !startDone {
startDone = true
startX, startY = w/2-text.width()/2, h/2-text.height()/2
}
startX, startY := w/2-text.width()/2, h/2-text.height()/2

x, y := startX, startY
for _, s := range text {
Expand All @@ -160,8 +163,7 @@ func draw(d time.Duration) {
flush()
}

func drawPause() {
w, h := termbox.Size()
func drawPause(w int, h int) {
startX := w/2 - pausedText.width()/2
startY := h * 3 / 4

Expand Down

0 comments on commit 5a87ef3

Please sign in to comment.