Skip to content

Commit

Permalink
use color stringer
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 20, 2017
1 parent f4390b3 commit 46ee717
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
18 changes: 9 additions & 9 deletions cmd/todoist/cmd/today.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ var todayCmd = &cobra.Command{
sort.Slice(items, func(i, j int) bool {
return items[i].DueDateUtc.Before(items[j].DueDateUtc)
})
var rows [][]string
var rows [][]todoist.ColorStringer
for _, i := range items {
var project string
var project todoist.Project
if p := client.Project.Resolve(i.ProjectID); p != nil {
project = p.String()
project = *p
}
var labels []string
for _, l := range i.Labels {
if j := client.Label.Resolve(l); j != nil {
labels = append(labels, j.String())
}
}
rows = append(rows, []string{
i.ID.String(),
i.DueDateUtc.Local().ColorShortString(),
strconv.Itoa(i.Priority),
rows = append(rows, []todoist.ColorStringer{
todoist.NewNoColorString(i.ID.String()),
i.DueDateUtc,
todoist.NewNoColorString(strconv.Itoa(i.Priority)),
project,
strings.Join(labels, " "),
i.Content,
todoist.NewNoColorString(strings.Join(labels, " ")),
todoist.NewNoColorString(i.Content),
})
}
fmt.Println(util.TableString(rows))
Expand Down
11 changes: 6 additions & 5 deletions cmd/util/format.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"github.com/kobtea/go-todoist/todoist"
"github.com/mattn/go-runewidth"
"regexp"
"strconv"
Expand All @@ -11,7 +12,7 @@ func StringWidthWithoutColor(s string) int {
return runewidth.StringWidth(re.ReplaceAllString(s, ""))
}

func TableString(rows [][]string) string {
func TableString(rows [][]todoist.ColorStringer) string {
if len(rows) == 0 {
return ""
}
Expand All @@ -25,8 +26,7 @@ func TableString(rows [][]string) string {
lens := make([]int, min)
for _, ss := range rows {
for i := 0; i < len(lens); i++ {
// FIXME: regex may be slow
l := StringWidthWithoutColor(ss[i])
l := runewidth.StringWidth(ss[i].String())
if l > lens[i] {
lens[i] = l
}
Expand All @@ -37,10 +37,11 @@ func TableString(rows [][]string) string {
for i := 0; i < len(rows); i++ {
for j := 0; j < len(lens); j++ {
f := runewidth.FillRight
if _, err := strconv.Atoi(rows[i][j]); err == nil {
if _, err := strconv.Atoi(rows[i][j].String()); err == nil {
f = runewidth.FillLeft
}
res += f(rows[i][j], lens[j])
colorSeqLen := runewidth.StringWidth(rows[i][j].ColorString()) - runewidth.StringWidth(rows[i][j].String())
res += f(rows[i][j].ColorString(), lens[j]+colorSeqLen)
if j < len(lens)-1 {
res += " "
}
Expand Down

0 comments on commit 46ee717

Please sign in to comment.