~kota/tide

fcef8d75454cdbdf7625d31e1c834b17448d0d85 — Dakota Walsh 1 year, 4 months ago 3eb2c3d
print a graph in terminal mode
2 files changed, 46 insertions(+), 1 deletions(-)

M display.go
M go.mod
M display.go => display.go +45 -0
@@ 9,6 9,9 @@ import (
	"time"
)

const graphWidth int = 36
const graphHeight int = 8

func displayTerm(index int, tides *[]Tide, now time.Time) {
	prevTide := (*tides)[index-1]
	nextTide := (*tides)[index]


@@ 23,6 26,7 @@ func displayTerm(index int, tides *[]Tide, now time.Time) {
		fmt.Printf("⬇ - low tide (%.2fm) in %v\n",
			nextTide.Height, nextDuration)
	}
	fmt.Println(graph(prevTide, nextTide, now))
}

func displaySimple(index int, tides *[]Tide, now time.Time) {


@@ 38,6 42,47 @@ func displaySimple(index int, tides *[]Tide, now time.Time) {
	}
}

// graph returns a printable string with a wave graph of the tide heights using
// the previous and next tides, and the current time. The last graph point will
// be the next Tide, the first will be after the previous tide.
func graph(prev, next Tide, now time.Time) string {
	interval := next.Time.Sub(prev.Time) / time.Duration(graphWidth)
	difference := (next.Height - prev.Height) / float64(graphHeight)
	var waves [graphWidth][graphHeight]string
	var rows [graphHeight]float64
	for i := range rows {
		rows[i] = (difference * float64(i+1)) + prev.Height
	}
	// build the wave variable
	for x, w := range waves {
		d := interval * time.Duration(x+1)
		t := prev.Time.Add(d)
		h := getHeight(prev, next, t)
		for y := range w {
			waves[x][y] = " "
		}
		for y := range w {
			if h >= rows[y] {
				if now.After(t) {
					waves[x][y] = "░"
				} else {
					waves[x][y] = "█"
				}
				break
			}
		}
	}
	// build the print string
	var s string
	for y := 0; y < graphHeight; y++ {
		for x := 0; x < graphWidth; x++ {
			s += waves[x][y]
		}
		s += "\n"
	}
	return s
}

// getHeight calculates the tide height using the previous and future
// tide heights. The forumla comes from
// https://www.linz.govt.nz/sea/tides/tide-predictions/how-calculate-tide-times-heights

M go.mod => go.mod +1 -1
@@ 2,4 2,4 @@ module git.sr.ht/~kota/tide

go 1.12

require github.com/mattn/go-isatty v0.0.13 // indirect
require github.com/mattn/go-isatty v0.0.13