Skip to content

Commit ed5fd28

Browse files
committed
Added column order to theme configuration
1 parent b8b929a commit ed5fd28

File tree

4 files changed

+99
-43
lines changed

4 files changed

+99
-43
lines changed

main

112 Bytes
Binary file not shown.

redovc/screen_printer.go

+43-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redovc
22

33
import (
4+
"fmt"
45
"io"
56
"sort"
67
"strconv"
@@ -18,6 +19,14 @@ type ScreenPrinter struct {
1819
UnicodeSupport bool
1920
}
2021

22+
const (
23+
idColumnWidth = "%-4s"
24+
completedColumnWidth = "%-3s"
25+
infoColumnWidth = "%-3s"
26+
dueColumnWidth = "%-10s"
27+
statusColumnWidth = "%-10s"
28+
)
29+
2130
// NewScreenPrinter creates a new screeen printer.
2231
func NewScreenPrinter(unicodeSupport bool) *ScreenPrinter {
2332
w := new(io.Writer)
@@ -49,41 +58,41 @@ func (f *ScreenPrinter) Print(groupedTodos *GroupedTodos, printNotes bool, showI
4958
}
5059

5160
func (f *ScreenPrinter) printTodo(tabby *tabby.Tabby, todo *Todo, printNotes bool, showInfoFlags bool) {
61+
id := fmt.Sprintf(idColumnWidth, f.formatID(todo.ID, todo.IsPriority))
62+
completed := fmt.Sprintf(completedColumnWidth, f.formatCompleted(todo.Completed))
63+
info := fmt.Sprintf(infoColumnWidth, f.formatInfoFlags(todo))
64+
due := fmt.Sprintf(dueColumnWidth, f.formatDue(todo.Due, todo.IsPriority, todo.Completed))
65+
status := fmt.Sprintf(statusColumnWidth, f.formatStatus(todo.Status, todo.IsPriority))
66+
subject := f.formatSubject(todo.Subject, todo.IsPriority)
67+
68+
columns := OrderColumns(showInfoFlags, id, completed, info, due, status, subject)
69+
5270
if showInfoFlags {
53-
tabby.AddLine(
54-
f.formatID(todo.ID, todo.IsPriority),
55-
f.formatCompleted(todo.Completed),
56-
f.formatInfoFlags(todo),
57-
f.formatDue(todo.Due, todo.IsPriority, todo.Completed),
58-
f.formatStatus(todo.Status, todo.IsPriority),
59-
f.formatSubject(todo.Subject, todo.IsPriority))
71+
tabby.AddLine(columns[0], columns[1], columns[2], columns[3], columns[4], columns[5])
6072
} else {
61-
tabby.AddLine(
62-
f.formatID(todo.ID, todo.IsPriority),
63-
f.formatCompleted(todo.Completed),
64-
f.formatDue(todo.Due, todo.IsPriority, todo.Completed),
65-
f.formatStatus(todo.Status, todo.IsPriority),
66-
f.formatSubject(todo.Subject, todo.IsPriority))
73+
tabby.AddLine(columns[0], columns[1], columns[2], columns[3], columns[4])
6774
}
6875

6976
if printNotes {
7077
for nid, note := range todo.Notes {
7178
tabby.AddLine(
72-
" "+noteIDColor.Sprint(strconv.Itoa(nid)),
73-
noteTextColor.Sprint(""),
74-
noteTextColor.Sprint(""),
75-
noteTextColor.Sprint(""),
76-
noteTextColor.Sprint(""),
77-
noteTextColor.Sprint(note))
79+
" "+noteIDColor.Sprint(strconv.Itoa(nid)),
80+
noteTextColor.Sprintf(idColumnWidth, ""),
81+
noteTextColor.Sprintf(completedColumnWidth, ""),
82+
noteTextColor.Sprintf(infoColumnWidth, ""),
83+
noteTextColor.Sprintf(dueColumnWidth, ""),
84+
noteTextColor.Sprintf(statusColumnWidth, ""),
85+
noteTextColor.Sprintf(""),
86+
noteTextColor.Sprint("- "+note))
7887
}
7988
}
8089
}
8190

8291
func (f *ScreenPrinter) formatID(ID int, isPriority bool) string {
8392
if isPriority {
84-
return taskIDPriColor.Sprint(strconv.Itoa(ID))
93+
return taskIDPriColor.Sprintf(strconv.Itoa(ID))
8594
}
86-
return taskIDColor.Sprint(strconv.Itoa(ID))
95+
return taskIDColor.Sprintf(strconv.Itoa(ID))
8796
}
8897

8998
func (f *ScreenPrinter) formatCompleted(completed bool) string {
@@ -98,7 +107,7 @@ func (f *ScreenPrinter) formatCompleted(completed bool) string {
98107

99108
func (f *ScreenPrinter) formatDue(due string, isPriority bool, completed bool) string {
100109
if due == "" {
101-
return whiteFg.Sprint(" ")
110+
return whiteFg.Sprintf("")
102111
}
103112
dueTime, _ := time.Parse(DATE_FORMAT, due)
104113

@@ -110,21 +119,15 @@ func (f *ScreenPrinter) formatDue(due string, isPriority bool, completed bool) s
110119

111120
func (f *ScreenPrinter) formatStatus(status string, isPriority bool) string {
112121
if status == "" {
113-
return statusColor.Sprint(" ")
114-
}
115-
116-
if len(status) < 10 {
117-
for x := len(status); x <= 10; x++ {
118-
status += " "
119-
}
122+
return statusColor.Sprintf("")
120123
}
121124

122-
statusRune := []rune(status)
125+
//statusRune := []rune(status)
123126

124127
if isPriority {
125-
return statusPriColor.Sprintf("%-10v", string(statusRune[0:10]))
128+
return statusPriColor.Sprintf(status) //string(statusRune[0:10]))
126129
}
127-
return statusColor.Sprintf("%-10s", string(statusRune[0:10]))
130+
return statusColor.Sprintf(status) //string(statusRune[0:10]))
128131
}
129132

130133
func (f *ScreenPrinter) formatInfoFlags(todo *Todo) string {
@@ -150,25 +153,25 @@ func (f *ScreenPrinter) formatInfoFlags(todo *Todo) string {
150153

151154
func (f *ScreenPrinter) printDue(due time.Time, completed bool) string {
152155
if isToday(due) {
153-
return todayColor.Sprint("today ")
156+
return todayColor.Sprintf("today")
154157
} else if isTomorrow(due) {
155-
return tomorrowColor.Sprint("tomorrow ")
158+
return tomorrowColor.Sprintf("tomorrow")
156159
} else if isPastDue(due) && !completed {
157-
return overdueColor.Sprint(due.Format("Mon Jan 02"))
160+
return overdueColor.Sprintf(due.Format("Mon Jan 02"))
158161
}
159162

160-
return otherDue.Sprint(due.Format("Mon Jan 02"))
163+
return otherDue.Sprintf(due.Format("Mon Jan 02"))
161164
}
162165

163166
func (f *ScreenPrinter) printPriorityDue(due time.Time, completed bool) string {
164167
if isToday(due) {
165-
return todayPriColor.Sprint("today ")
168+
return todayPriColor.Sprintf("today")
166169
} else if isTomorrow(due) {
167-
return tomorrowPriColor.Sprint("tomorrow ")
170+
return tomorrowPriColor.Sprintf("tomorrow")
168171
} else if isPastDue(due) && !completed {
169-
return overduePriColor.Sprint(due.Format("Mon Jan 02"))
172+
return overduePriColor.Sprintf(due.Format("Mon Jan 02"))
170173
}
171-
return otherDuePriColor.Sprint(due.Format("Mon Jan 02"))
174+
return otherDuePriColor.Sprintf(due.Format("Mon Jan 02"))
172175
}
173176

174177
func (f *ScreenPrinter) formatSubject(subject string, isPriority bool) string {

redovc/theme.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
type Theme struct {
1414
Columns []struct {
15-
Desc string `json:"desc"`
16-
Nameame string `json:"columnname"`
17-
Index int `json:"index"`
15+
Desc string `json:"desc"`
16+
Name string `json:"columnname"`
17+
Index int `json:"index"`
1818
} `json:"Columns"`
1919
Colors []struct {
2020
Desc string `json:"desc"`
@@ -237,6 +237,42 @@ func StringToColor(colorString string, bold bool) *color.Color {
237237
}
238238
}
239239

240+
func OrderColumns(showInfoFlags bool, id string, completed string, info string, due string, status string, subject string) []string {
241+
statusIndex := -1
242+
columnList := []string{"", "", "", "", "", ""}
243+
theme, err := GetTheme()
244+
if err != nil {
245+
fmt.Printf("Theme file found but could not load: %v\n", err)
246+
}
247+
248+
for _, column := range theme.Columns {
249+
colName := column.Name
250+
index := column.Index
251+
252+
switch colName {
253+
case "id":
254+
columnList[index] = id
255+
case "completed":
256+
columnList[index] = completed
257+
case "information":
258+
columnList[index] = info
259+
case "due":
260+
columnList[index] = due
261+
case "status":
262+
statusIndex = index
263+
columnList[index] = status
264+
case "subject":
265+
columnList[index] = subject
266+
}
267+
}
268+
269+
if !showInfoFlags {
270+
RemoveFromStringArray(columnList, statusIndex)
271+
}
272+
273+
return columnList
274+
}
275+
240276
const themeTemplate = `
241277
{
242278
"Columns":

redovc/util.go

+17
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,20 @@ func JSONtoCSV(jsonData []byte) {
132132
//Following line prints CSV
133133
fmt.Println(got)
134134
}
135+
136+
func RemoveFromStringArray(array []string, indexToDelete int) []string {
137+
// check if the index is within array bounds
138+
if indexToDelete < 0 || indexToDelete >= len(array) {
139+
return array
140+
} else {
141+
// delete an element from the array
142+
newLength := 0
143+
for index := range array {
144+
if indexToDelete != index {
145+
array[newLength] = array[index]
146+
newLength++
147+
}
148+
}
149+
return array[:newLength]
150+
}
151+
}

0 commit comments

Comments
 (0)