Skip to content

Commit

Permalink
Updates pause functionality (#32)
Browse files Browse the repository at this point in the history
* feat: [add-pause-functionality] Adds Spacebar Pausing

Adds pausing to the countdown timer by using the spacebar key.
Also added an input delay in order to avoid pausing happening
immediately when the space bar button is pressed.

* feat: [update-pause-functionality] Updates Pause

Updates the pause functionality with the following:
1. Adds pause font in the lower 3/4 of the screen
2. Changes the button press to require a spacebar instead of 'p' and 'c'
3. Adds an input buffer timeout so you don't accidentally pause and
   unpause with one button press.
4. When unpausing, adds another call of the draw function so that the
   'Paused' text is immediately removed instead of waiting for the next
   tick.
  • Loading branch information
TL Boright authored Mar 5, 2023
1 parent 05c5818 commit 10db737
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ var defaultFont = Font{
" ╚════╝ ",
},
}

var pausedText = Symbol{
"█▀▄ ▄▀▄ █ █ ▄▀▀ ██▀ █▀▄",
"█▀ █▀█ ▀▄█ ▄██ █▄▄ █▄▀",
}
35 changes: 28 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const (
Flags
`
tick = time.Second
tick = time.Second
inputDelayMS = 500 * time.Millisecond
)

var (
Expand All @@ -31,6 +32,8 @@ var (
queues chan termbox.Event
startDone bool
startX, startY int
inputStartTime time.Time
isPaused bool
)

func main() {
Expand Down Expand Up @@ -80,6 +83,7 @@ func stop() {

func countdown(timeLeft time.Duration, countUp bool, sayTheTime bool) {
var exitCode int
isPaused = false

start(timeLeft)

Expand All @@ -96,16 +100,24 @@ loop:
for {
select {
case ev := <-queues:
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC) {
if ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC {
exitCode = 1
break loop
}
if ev.Ch == 'p' || ev.Ch == 'P' {
stop()
}
if ev.Ch == 'c' || ev.Ch == 'C' {
start(timeLeft)

if pressTime := time.Now(); ev.Key == termbox.KeySpace && pressTime.Sub(inputStartTime) > inputDelayMS {
if isPaused {
start(timeLeft)
draw(timeLeft)
} else {
stop()
drawPause()
}

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

case <-ticker.C:
if countUp {
timeLeft += tick
Expand Down Expand Up @@ -148,6 +160,15 @@ func draw(d time.Duration) {
flush()
}

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

echo(pausedText, startX, startY)
flush()
}

func format(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
Expand Down

0 comments on commit 10db737

Please sign in to comment.