Heavy refactoring

This commit is contained in:
Sasha Koshka 2022-11-28 17:26:10 -05:00
parent b819da0419
commit f2cc5e8099
4 changed files with 233 additions and 219 deletions

82
draw.go Normal file
View File

@ -0,0 +1,82 @@
package main
import "fmt"
import "time"
import "git.tebibyte.media/sashakoshka/stone"
func redraw () {
width, height := application.Size()
columns = (width - 20) / 23 + 1
rows = height / 10 + 1
monthIter := viewingMonth
xOffset := (width - columns * 23) / 2 + 1
for y := 0; y < rows; y ++ {
for x := 0; x < columns; x ++ {
drawMonth (
x * 23 + xOffset, y * 10,
monthIter / 12,
time.Month(wrap(monthIter, 12) + 1))
monthIter ++
}
}
}
func drawMonth (xOffset, yOffset, year int, month time.Month) {
current := int(month) - 1 + year * 12 == currentMonth
bce := year < 0
if bce { year *= -1 }
var dateString string
dateString = fmt.Sprintf("%d ", year)
if bce { dateString += "BCE " }
dateString += month.String()
application.SetDot(xOffset + (20 - len(dateString)) / 2, yOffset)
fmt.Fprint(application, dateString)
var headerColor stone.Color
if current {
headerColor = stone.ColorRed
} else {
headerColor = stone.ColorForeground
}
for x := xOffset; x < 20 + xOffset; x ++ {
application.SetColor(x, yOffset, headerColor)
}
application.SetDot(xOffset, yOffset + 2)
fmt.Fprint (
application,
"S M T W Þ F S\n")
application.SetColor(xOffset, yOffset + 2, stone.ColorDim)
application.SetColor(xOffset + 18, yOffset + 2, stone.ColorDim)
dayIter := 0 - int(firstOfMonth(year, month).Weekday())
if dayIter <= -6 {
dayIter = 1
}
x, y, weekday := xOffset, yOffset + 3, 0
totalDays := daysInMonth(year, month)
for ; dayIter <= totalDays; dayIter ++ {
if dayIter > 0 {
application.SetDot(x, y)
fmt.Fprint(application, dayIter)
}
if current && dayIter == int(time.Now().Day()) {
application.SetColor(x, y, stone.ColorRed)
application.SetColor(x + 1, y, stone.ColorRed)
} else if weekday == 0 || weekday == 6 {
application.SetColor(x, y, stone.ColorDim)
application.SetColor(x + 1, y, stone.ColorDim)
}
weekday = (weekday + 1) % 7
x += 3
if x > xOffset + 20 {
x = xOffset
y ++
}
}
}

104
event.go Normal file
View File

@ -0,0 +1,104 @@
package main
import "time"
import "git.tebibyte.media/sashakoshka/stone"
func onScroll (x, y int) {
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += y * columns
application.Clear()
redraw()
application.Draw()
}
func onPress (button stone.Button, modifiers stone.Modifiers) {
switch button {
case stone.KeyUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns
application.Clear()
redraw()
application.Draw()
case stone.KeyDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns
application.Clear()
redraw()
application.Draw()
case stone.KeyPageUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns * rows
application.Clear()
redraw()
application.Draw()
case stone.KeyPageDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns * rows
application.Clear()
redraw()
application.Draw()
}
}
func onStart () {
drawMutex.Lock()
defer drawMutex.Unlock()
currentMonth = canonMonth(time.Now())
viewingMonth = currentMonth
redraw()
go func () {
for {
time.Sleep(time.Second)
onTick()
}
} ()
}
func onResize () {
drawMutex.Lock()
defer drawMutex.Unlock()
redraw()
}
func onTick () {
drawMutex.Lock()
defer drawMutex.Unlock()
pageSize := columns * rows
within :=
currentMonth >= viewingMonth &&
currentMonth < viewingMonth + pageSize
newMonth := canonMonth(time.Now())
if currentMonth != newMonth {
currentMonth = newMonth
if within {
if currentMonth < viewingMonth {
viewingMonth -= pageSize
} else if currentMonth >= viewingMonth + pageSize {
viewingMonth += pageSize
}
}
application.Clear()
redraw()
application.Draw()
}
}

219
main.go
View File

@ -1,7 +1,5 @@
package main package main
import "fmt"
import "time"
import "sync" import "sync"
import "bytes" import "bytes"
import "image" import "image"
@ -52,220 +50,3 @@ func main () {
err = application.Run() err = application.Run()
if err != nil { panic(err) } if err != nil { panic(err) }
} }
func onScroll (x, y int) {
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += y * columns
application.Clear()
redraw()
application.Draw()
}
func onPress (button stone.Button, modifiers stone.Modifiers) {
switch button {
case stone.KeyUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns
application.Clear()
redraw()
application.Draw()
case stone.KeyDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns
application.Clear()
redraw()
application.Draw()
case stone.KeyPageUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns * rows
application.Clear()
redraw()
application.Draw()
case stone.KeyPageDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns * rows
application.Clear()
redraw()
application.Draw()
}
}
func onStart () {
currentMonth = canonMonth(time.Now())
viewingMonth = currentMonth
redraw()
go tick()
}
func onResize () {
drawMutex.Lock()
defer drawMutex.Unlock()
redraw()
}
func tick () {
for {
time.Sleep(time.Second)
drawMutex.Lock()
pageSize := columns * rows
within :=
currentMonth >= viewingMonth &&
currentMonth < viewingMonth + pageSize
newMonth := canonMonth(time.Now())
if currentMonth != newMonth {
currentMonth = newMonth
if within {
if currentMonth < viewingMonth {
viewingMonth -= pageSize
} else if currentMonth >= viewingMonth + pageSize {
viewingMonth += pageSize
}
}
application.Clear()
redraw()
application.Draw()
}
drawMutex.Unlock()
}
}
func redraw () {
width, height := application.Size()
columns = (width - 20) / 23 + 1
rows = height / 10 + 1
monthIter := viewingMonth
xOffset := (width - columns * 23) / 2 + 1
for y := 0; y < rows; y ++ {
for x := 0; x < columns; x ++ {
drawMonth (
x * 23 + xOffset, y * 10,
monthIter / 12,
time.Month(wrap(monthIter, 12) + 1))
monthIter ++
}
}
}
func wrap (value, around int) (wrapped int) {
wrapped = value % around
if wrapped < 0 {
wrapped += around
}
return
}
func drawMonth (xOffset, yOffset, year int, month time.Month) {
current := int(month) - 1 + year * 12 == currentMonth
bce := year < 0
if bce { year *= -1 }
var dateString string
dateString = fmt.Sprintf("%d ", year)
if bce { dateString += "BCE " }
dateString += month.String()
application.SetDot(xOffset + (20 - len(dateString)) / 2, yOffset)
fmt.Fprint(application, dateString)
var headerColor stone.Color
if current {
headerColor = stone.ColorRed
} else {
headerColor = stone.ColorForeground
}
for x := xOffset; x < 20 + xOffset; x ++ {
application.SetColor(x, yOffset, headerColor)
}
application.SetDot(xOffset, yOffset + 2)
fmt.Fprint (
application,
"S M T W Þ F S\n")
application.SetColor(xOffset, yOffset + 2, stone.ColorDim)
application.SetColor(xOffset + 18, yOffset + 2, stone.ColorDim)
dayIter := 0 - int(firstOfMonth(year, month).Weekday())
if dayIter <= -6 {
dayIter = 1
}
x, y, weekday := xOffset, yOffset + 3, 0
totalDays := daysInMonth(year, month)
for ; dayIter <= totalDays; dayIter ++ {
if dayIter > 0 {
application.SetDot(x, y)
fmt.Fprint(application, dayIter)
}
if current && dayIter == int(time.Now().Day()) {
application.SetColor(x, y, stone.ColorRed)
application.SetColor(x + 1, y, stone.ColorRed)
} else if weekday == 0 || weekday == 6 {
application.SetColor(x, y, stone.ColorDim)
application.SetColor(x + 1, y, stone.ColorDim)
}
weekday = (weekday + 1) % 7
x += 3
if x > xOffset + 20 {
x = xOffset
y ++
}
}
}
func firstOfMonth (year int, month time.Month) (day time.Time) {
day = time.Date(year, month, 0, 0, 0, 0, 0, time.Local)
return
}
func daysInMonth (year int, month time.Month) (days int) {
switch month {
case 1: days = 31
case 2:
// betcha didn't know this about leap years
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
days = 29
} else {
days = 28
}
case 3: days = 31
case 4: days = 30
case 5: days = 31
case 6: days = 30
case 7: days = 31
case 8: days = 31
case 9: days = 30
case 10: days = 31
case 11: days = 30
case 12: days = 31
}
return
}
func canonMonth (when time.Time) (month int) {
month = int(when.Month() - 1) + when.Year() * 12
return
}

47
util.go Normal file
View File

@ -0,0 +1,47 @@
package main
import "time"
func wrap (value, around int) (wrapped int) {
wrapped = value % around
if wrapped < 0 {
wrapped += around
}
return
}
func firstOfMonth (year int, month time.Month) (day time.Time) {
day = time.Date(year, month, 0, 0, 0, 0, 0, time.Local)
return
}
func daysInMonth (year int, month time.Month) (days int) {
switch month {
case 1: days = 31
case 2:
// betcha didn't know this about leap years
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
days = 29
} else {
days = 28
}
case 3: days = 31
case 4: days = 30
case 5: days = 31
case 6: days = 30
case 7: days = 31
case 8: days = 31
case 9: days = 30
case 10: days = 31
case 11: days = 30
case 12: days = 31
}
return
}
func canonMonth (when time.Time) (month int) {
month = int(when.Month() - 1) + when.Year() * 12
return
}