Reformat with gofmt

This commit is contained in:
Petar Kapriš 2021-09-04 11:50:23 +02:00 committed by Петар Каприш
parent 66695e069b
commit 72916575bd

36
Main.go
View file

@ -3,18 +3,17 @@ package main
import (
"log"
"os"
"time"
"strconv"
"time"
"unicode/utf8"
"github.com/gdamore/tcell/v2"
)
const (
dayWidth = 2
smallGap = 1
largeGap = 3
largeGap = 3
monthWidth = 7*dayWidth + 6*smallGap
monthHeight = (31+6+(7-1))/7 + 2
/* monthHeight is the height of a 31-day month starting on Sunday (month
@ -31,7 +30,7 @@ var (
)
var (
wall tcell.Screen
wall tcell.Screen
defStyle tcell.Style
selStyle tcell.Style
)
@ -60,14 +59,14 @@ func date(year int, month time.Month, day int) time.Time {
}
func centeredText(s string, x, y, width int, scr tcell.Screen, style tcell.Style) {
start := x + max((width - utf8.RuneCountInString(s)) / 2, 0)
start := x + max((width-utf8.RuneCountInString(s))/2, 0)
i := 0
for _, char := range s {
if i >= width {
break;
break
}
scr.SetContent(start + i, y, char, nil, style)
scr.SetContent(start+i, y, char, nil, style)
i++
}
}
@ -79,7 +78,7 @@ func wrappedText(s string, scr tcell.Screen, style tcell.Style) {
for _, char := range s {
if x >= w {
if y >= h {
break;
break
}
x = 0
y++
@ -93,10 +92,10 @@ func drawMonth(m time.Month, x, y int) {
centeredText(months[m], x, y, monthWidth, wall, defStyle)
for i := 1; i <= 7; i++ {
centeredText(weekdays[i % 7], x + (i - 1) * (dayWidth + smallGap), y + 1, dayWidth, wall, defStyle)
centeredText(weekdays[i%7], x+(i-1)*(dayWidth+smallGap), y+1, dayWidth, wall, defStyle)
}
dayNum := date(selTime.Year(), m + 1, 0).Day()
dayNum := date(selTime.Year(), m+1, 0).Day()
for i := 1; i <= dayNum; i++ {
weekday := int(date(selTime.Year(), m, i).Weekday())
style := defStyle
@ -105,8 +104,8 @@ func drawMonth(m time.Month, x, y int) {
}
centeredText(
strconv.Itoa(i),
x + ((weekday - 1 + 7) % 7) * (dayWidth + smallGap),
y + 2,
x+((weekday-1+7)%7)*(dayWidth+smallGap),
y+2,
2,
wall,
style)
@ -119,7 +118,7 @@ func drawMonth(m time.Month, x, y int) {
func drawWall() {
wall.Clear()
w, h := wall.Size()
if w < monthWidth + 2 * smallGap || h < monthHeight + 2 * smallGap + titleHeight {
if w < monthWidth+2*smallGap || h < monthHeight+2*smallGap+titleHeight {
wrappedText("Екран је премали за календар.", wall, defStyle)
return
}
@ -127,13 +126,13 @@ func drawWall() {
monthsHigh := (h - smallGap - titleHeight) / (monthHeight + smallGap)
monthsDrawn := min(monthsHigh*monthsWide, 12) // TODO: izraziti 12 drugacije??
startingMonth := time.Month((int(selTime.Month()) - 1) / monthsDrawn * monthsDrawn + 1)
endingMonth := time.Month(min(12, int(startingMonth) + monthsDrawn - 1))
startingMonth := time.Month((int(selTime.Month())-1)/monthsDrawn*monthsDrawn + 1)
endingMonth := time.Month(min(12, int(startingMonth)+monthsDrawn-1))
centerVert := (w - monthsWide*monthWidth - (monthsWide-1)*largeGap) / 2
for i, m := 0, startingMonth ; i < monthsHigh ; i++ {
for j := 0 ; j < monthsWide ; j++ {
drawMonth(m, centerVert + j*(monthWidth+largeGap), titleHeight + smallGap + i*(monthHeight+smallGap))
for i, m := 0, startingMonth; i < monthsHigh; i++ {
for j := 0; j < monthsWide; j++ {
drawMonth(m, centerVert+j*(monthWidth+largeGap), titleHeight+smallGap+i*(monthHeight+smallGap))
if m >= endingMonth {
return
}
@ -160,7 +159,6 @@ func main() {
wall.Clear()
drawWall()
quit := func() {
wall.Fini()
os.Exit(0)