Skip to content

Commit

Permalink
use self table renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 20, 2017
1 parent dd7cca4 commit a2c97a0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
20 changes: 6 additions & 14 deletions cmd/todoist/cmd/today.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cmd

import (
"github.com/fatih/color"
"fmt"
"github.com/kobtea/go-todoist/cmd/util"
"github.com/kobtea/go-todoist/todoist"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
"sort"
"strconv"
"strings"
Expand All @@ -24,9 +23,7 @@ var todayCmd = &cobra.Command{
sort.Slice(items, func(i, j int) bool {
return items[i].DueDateUtc.Before(items[j].DueDateUtc)
})
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"id", "date", "pri", "project", "labels", "content"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
var rows [][]string
for _, i := range items {
var project string
if p := client.Project.Resolve(i.ProjectID); p != nil {
Expand All @@ -38,21 +35,16 @@ var todayCmd = &cobra.Command{
labels = append(labels, j.String())
}
}
date := i.DateString
if i.IsOverDueDate() {
date = color.New(color.BgRed).SprintFunc()(date)
}
table.Append([]string{
rows = append(rows, []string{
i.ID.String(),
date,
i.DueDateUtc.Local().ColorShortString(),
strconv.Itoa(i.Priority),
project,
strings.Join(labels, " "),
i.Content,
})

}
table.Render()
fmt.Println(util.TableString(rows))
return nil
},
}
Expand Down
53 changes: 53 additions & 0 deletions cmd/util/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package util

import (
"github.com/mattn/go-runewidth"
"regexp"
"strconv"
)

func StringWidthWithoutColor(s string) int {
re := regexp.MustCompile("\x1b\\[[0-9;]*m")
return runewidth.StringWidth(re.ReplaceAllString(s, ""))
}

func TableString(rows [][]string) string {
if len(rows) == 0 {
return ""
}
min := len(rows[0])
for _, i := range rows {
if len(i) < min {
min = len(i)
}
}
// retrieve max width in each columns
lens := make([]int, min)
for _, ss := range rows {
for i := 0; i < len(lens); i++ {
// FIXME: regex may be slow
l := StringWidthWithoutColor(ss[i])
if l > lens[i] {
lens[i] = l
}
}
}
// format rows into table
var res 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 {
f = runewidth.FillLeft
}
res += f(rows[i][j], lens[j])
if j < len(lens)-1 {
res += " "
}
}
if i < len(rows)-1 {
res += "\n"
}
}
return res
}
14 changes: 13 additions & 1 deletion todoist/time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package todoist

import (
"github.com/fatih/color"
"strconv"
"time"
)
Expand Down Expand Up @@ -44,6 +45,10 @@ func (t Time) After(u Time) bool {
return t.Time.After(u.Time)
}

func (t Time) Local() Time {
return Time{t.Time.Local()}
}

func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
Expand All @@ -64,9 +69,16 @@ func (t *Time) UnmarshalJSON(b []byte) (err error) {
return nil
}

func (t *Time) ShortString() string {
func (t Time) ShortString() string {
if t.IsZero() {
return ""
}
return t.Time.Local().Format(shortLayout)
}

func (t Time) ColorShortString() string {
if !t.IsZero() && t.Before(Time{time.Now()}) {
return color.New(color.BgRed).Sprint(t.ShortString())
}
return t.ShortString()
}

0 comments on commit a2c97a0

Please sign in to comment.