@@ -2,7 +2,10 @@ package month
22
33import (
44 "fmt"
5+ "github.com/raspi/kallu/locales/base"
6+ "strings"
57 "time"
8+ "unicode/utf8"
69)
710
811type Month struct {
@@ -82,7 +85,7 @@ func (mon Month) GetNextMonth() Month {
8285 return New (last .Year (), last .Month (), mon .dow , mon .now )
8386}
8487
85- func (mon Month ) PrintMonth (months []Month ) {
88+ func (mon Month ) PrintMonth (months []Month , locale base. Locale ) {
8689 const (
8790 esc = "\033 ["
8891 Clear = esc + "0m"
@@ -93,45 +96,40 @@ func (mon Month) PrintMonth(months []Month) {
9396 DefaultFG = SetForeground + "250m"
9497 )
9598
99+ weekLocalized := locale .GetWeek ()
100+ weekdaysLocalized := locale .GetWeekDays ()
101+ monthsLocalized := locale .GetMonths ()
102+
96103 monthCount := len (months )
97104
98- maxweeks := 0
99- for _ , m := range months {
100- start , end := m .GetStartEndWeek ()
101- weeks , _ := m .GetDaysWeeks (start , end )
102- if weeks > maxweeks {
103- maxweeks = weeks
104- }
105+ var headerCol []string
106+ headerSize := 0
107+
108+ curr := mon .getStart ()
109+
110+ // "Week" localized
111+ headerSize += 5
112+ headerCol = append (headerCol , weekLocalized )
113+
114+ // Day names
115+ for di := 0 ; di < 7 ; di ++ {
116+ headerSize += 4
117+ headerCol = append (headerCol , weekdaysLocalized [curr .Weekday ()])
118+ curr = curr .AddDate (0 , 0 , 1 )
105119 }
120+ headerSize += 1
106121
107122 fmt .Print (SetBackground + "238m" )
108123 fmt .Print (SetForeground + "245m" )
109124
110125 // Print month and year header
111126 for mIdx , m := range months {
112- header := fmt .Sprintf (`%v %4v` , m .GetMonth ().Month (), m .GetMonth ().Year ())
127+ header := fmt .Sprintf (`%v %4v` , monthsLocalized [ m .GetMonth ().Month ()] , m .GetMonth ().Year ())
113128 if mon .now .Year () == m .m .Year () && mon .now .Month () == m .m .Month () {
114129 header = "[" + header + "]"
115130 }
116131
117- // Week Mon Tue Wed Thu Fri Sat Sun
118-
119- required := 34
120- paddedHeader := header
121-
122- for i := 0 ; i < required ; i ++ {
123- if len (paddedHeader ) < required {
124- // Add padding to both sides
125- paddedHeader = " " + paddedHeader + " "
126- }
127- }
128-
129- if len (paddedHeader ) > required {
130- // Cut
131- paddedHeader = paddedHeader [0 :required ]
132- }
133-
134- o := paddedHeader
132+ o := centerString (header , headerSize )
135133
136134 if mIdx < monthCount - 1 {
137135 o += " | "
@@ -147,22 +145,38 @@ func (mon Month) PrintMonth(months []Month) {
147145 fmt .Print (SetForeground + "245m" )
148146 fmt .Print (SetUnderlineOn )
149147
150- for mIdx , m := range months {
151- curr := m .getStart ()
148+ for mIdx , _ := range months {
152149 if mIdx > 0 {
153150 // Separator
154151 fmt .Print (" | " )
155152 }
156153
157- fmt .Print ("Week " )
158- for di := 0 ; di < 7 ; di ++ {
159- fmt .Printf (`%v ` , curr .Weekday ().String ()[0 :3 ])
160- curr = curr .AddDate (0 , 0 , 1 )
154+ for idx , hdr := range headerCol {
155+ switch idx {
156+ case 0 : // Week
157+ fmt .Printf (`%4s ` , hdr )
158+ default :
159+ fmt .Printf (`%4s` , hdr )
160+ }
161+
162+ if idx == 7 {
163+ fmt .Print (` ` )
164+ }
161165 }
162166 }
163167
164168 fmt .Println (Clear )
165169
170+ // Calculate week row count
171+ maxweeks := 0
172+ for _ , m := range months {
173+ start , end := m .GetStartEndWeek ()
174+ weeks , _ := m .GetDaysWeeks (start , end )
175+ if weeks > maxweeks {
176+ maxweeks = weeks
177+ }
178+ }
179+
166180 for weekIndex := 0 ; weekIndex < maxweeks + 1 ; weekIndex ++ {
167181
168182 if (weekIndex & 1 ) == 0 {
@@ -253,3 +267,8 @@ func (mon Month) PrintMonth(months []Month) {
253267 fmt .Println (Clear )
254268 }
255269}
270+
271+ func centerString (str string , width int ) string {
272+ spaces := int (float64 (width - utf8 .RuneCountInString (str )) / 2 )
273+ return strings .Repeat (" " , spaces ) + str + strings .Repeat (" " , width - (spaces + utf8 .RuneCountInString (str )))
274+ }
0 commit comments