~kota/tide

ffa0fb54de6f9bcba37e60a028bad534fb5905a4 — Dakota Walsh 1 year, 4 months ago dea5d7f
print information about the next tide for term mode
2 files changed, 13 insertions(+), 16 deletions(-)

M display.go
M main.go
M display.go => display.go +13 -15
@@ 12,14 12,16 @@ import (
func displayTerm(index int, tides *[]Tide, now time.Time) {
	prevTide := (*tides)[index-1]
	nextTide := (*tides)[index]
	nextDuration := fmtDuration(nextTide.Time.Sub(now))
	height := getCurrentHeight(prevTide, nextTide, now)
	// nearest := getNearestTide(prevTide, nextTide, now)
	rising := getRising(prevTide, nextTide)
	fmt.Printf("%.2fm", height)
	if rising {
		fmt.Printf("⬆\n")
		fmt.Printf("⬆ - high tide (%.2fm) in %v\n",
			nextTide.Height, nextDuration)
	} else {
		fmt.Printf("⬇\n")
		fmt.Printf("⬇ - low tide (%.2fm) in %v\n",
			nextTide.Height, nextDuration)
	}
}



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

func fmtDuration(d time.Duration) string {
	d = d.Round(time.Minute)
	h := d / time.Hour
	d -= h * time.Hour
	m := d / time.Minute
	return fmt.Sprintf("%02dh%02dm", h, m)
}

// getCurrentHeight calculates the current 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


@@ 50,18 60,6 @@ func getCurrentHeight(prev, next Tide, now time.Time) float64 {
	return h
}

// getNearestTide returns either the previous or next tide for a given time
// depending on which is closer in time.
func getNearestTide(prev, next Tide, now time.Time) Tide {
	p := now.Sub(prev.Time)
	n := next.Time.Sub(now)
	if p < n {
		return prev
	} else {
		return next
	}
}

// getRising returns true if tide is rising based on the previous and next
// tides.
func getRising(prev, next Tide) bool {

M main.go => main.go +0 -1
@@ 51,7 51,6 @@ func main() {
	// non-terminal.
	now := time.Now()
	if isatty.IsTerminal(os.Stdout.Fd()) {
		fmt.Println("Is Terminal")
		for i, v := range tides {
			if v.Time.After(now) {
				displayTerm(i, &tides, now)