Skip to content

Commit

Permalink
Add fullscreen display mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Johnson committed Apr 19, 2016
1 parent 97dda18 commit fc3ec3e
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 3 deletions.
171 changes: 171 additions & 0 deletions display/display.go
@@ -0,0 +1,171 @@
package display

import "github.com/nsf/termbox-go"

const (
BigCharWidth = 3
BigCharHeight = 5
)

var bigChars = map[rune][BigCharHeight]string{
0: {
"X X",
" X ",
"X X",
" X ",
"X X",
},
' ': {
" ",
" ",
" ",
" ",
" ",
},
'0': {
"XXX",
"X X",
"X X",
"X X",
"XXX",
},
'1': {
" X ",
"XX ",
" X ",
" X ",
"XXX",
},
'2': {
"XXX",
" X",
"XXX",
"X ",
"XXX",
},
'3': {
"XXX",
" X",
"XXX",
" X",
"XXX",
},
'4': {
"X X",
"X X",
"XXX",
" X",
" X",
},
'5': {
"XXX",
"X ",
"XXX",
" X",
"XXX",
},
'6': {
"XXX",
"X ",
"XXX",
"X X",
"XXX",
},
'7': {
"XXX",
" X",
" X",
" X ",
" X ",
},
'8': {
"XXX",
"X X",
"XXX",
"X X",
"XXX",
},
'9': {
"XXX",
"X X",
"XXX",
" X",
" X",
},
':': {
" ",
" X ",
" ",
" X ",
" ",
},
'.': {
" ",
" ",
" ",
" XX",
" XX",
},
}

type Point struct {
X, Y int
Fg, Bg termbox.Attribute
}

func (p Point) Char(ch rune) {
termbox.SetCell(p.X, p.Y, ch, p.Fg, p.Bg)
}

func (p Point) Str(s string) {
for _, c := range s {
p.Char(c)
p.X++
}
}

func (p Point) Pattern(pattern [BigCharHeight]string) {
for y, line := range pattern {
for x, c := range line {
q := p
q.X += x
q.Y += y
if c != ' ' {
q.Fg, q.Bg = p.Bg, p.Fg
}
q.Char(' ')
}
}
}

func (p Point) BigChar(ch rune) {
pattern, ok := bigChars[ch]
if !ok {
pattern = bigChars[0]
}
p.Pattern(pattern)
}

func (p Point) BigStr(s string) {
xOffset := p.X
for i, c := range s {
p.X = xOffset + i*(BigCharWidth+1)
p.BigChar(c)
}
}

func (p Point) ProgressBar(length, cur, total int) {
divider := (length * cur) / total
for x := 0; x < length; x++ {
ch := ' '
q := p
q.X += x
if x == divider {
ch = '░'
}
if x < divider {
q.Fg, q.Bg = p.Bg, p.Fg
}
q.Char(ch)
}
}
2 changes: 1 addition & 1 deletion pomodoro.go
Expand Up @@ -35,7 +35,7 @@ func main() {
if *simple {
simpleCountdown(finish)
} else {
fullscreenCountdown()
fullscreenCountdown(start, finish)
}

if !*silence {
Expand Down
108 changes: 106 additions & 2 deletions termbox.go
@@ -1,5 +1,109 @@
package main

func fullscreenCountdown() {
// Todo
import (
"fmt"
"os"
"strings"
"time"

"github.com/carlmjohnson/pomodoro/display"
"github.com/nsf/termbox-go"
)

func fullscreenCountdown(start, finish time.Time) {
err := termbox.Init()
if err != nil {
fmt.Fprintln(os.Stderr, "Couldn't open display:", err)
os.Exit(2)
}
defer termbox.Close()

// Leaks a goroutine
ticker := time.Tick(100 * time.Millisecond)
quit := make(chan struct{})
// Leaks if not quit
go func() {
defer close(quit)
for {
e := termbox.PollEvent()
// Quit on any of the common keys for quitting
if strings.ContainsRune("CcDdQqXx", e.Ch) ||
e.Key == termbox.KeyCtrlC ||
e.Key == termbox.KeyCtrlD ||
e.Key == termbox.KeyCtrlQ ||
e.Key == termbox.KeyCtrlX {
return
}
}
}()

for render(start, finish) {
select {
case <-ticker:
case <-quit:
termbox.Close()
os.Exit(1)
return
}
}

}

func render(start, finish time.Time) bool {
now := time.Now()
remaining := -now.Sub(finish)
if remaining < 0 {
return false
}

const timeFmt = "3:04:05pm"
screenW, screenH := termbox.Size()
centerX := screenW / 2
centerY := screenH / 2

termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

startStr := start.Format(timeFmt)
display.Point{
0, 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Start")
display.Point{
0, 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(startStr)

nowStr := now.Format(timeFmt)
display.Point{
centerX - (len(nowStr) / 2), 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Now")
display.Point{
centerX - (len(nowStr) / 2), 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(nowStr)

finishStr := finish.Format(timeFmt)
display.Point{
screenW - len(finishStr), 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Finish")
display.Point{
screenW - len(finishStr), 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(finishStr)

remainingStr := formatter(remaining)
display.Point{
centerX - (len(remainingStr) * (display.BigCharWidth + 1) / 2), centerY,
termbox.ColorBlue, termbox.ColorDefault,
}.BigStr(remainingStr)

display.Point{
0, centerY + 6,
termbox.ColorBlue, termbox.ColorWhite,
}.ProgressBar(screenW, int(start.Sub(now)), int(start.Sub(finish)))

termbox.Flush()
return true
}

0 comments on commit fc3ec3e

Please sign in to comment.