Skip to content

Commit

Permalink
Convert syntax to go-colortext (windows support)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatto committed Feb 17, 2015
1 parent 9cc676e commit f5bf80c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
46 changes: 34 additions & 12 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"

"github.com/codegangsta/cli"
"github.com/fatih/color"
"github.com/daviddengcn/go-colortext"
)

func main() {
Expand Down Expand Up @@ -50,7 +50,9 @@ func main() {
}
fmt.Println()
} else {
color.Cyan("There's no todo to show.")
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Println("There's no todo to show.")
ct.ResetColor()
}
}
}
Expand All @@ -63,7 +65,9 @@ func main() {

if len(c.Args()) != 1 {
fmt.Println()
color.Red("Error")
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Println("Error")
ct.ResetColor()
fmt.Println("You must provide a name to your todo.")
fmt.Println("Example: td add \"call mum\"")
fmt.Println()
Expand All @@ -87,7 +91,9 @@ func main() {
fmt.Println(err)
}

color.Cyan("\"%s\" is now added to your todos.", c.Args()[0])
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Printf("\"%s\" is now added to your todos.\n", c.Args()[0])
ct.ResetColor()
},
},
{
Expand Down Expand Up @@ -136,7 +142,9 @@ func main() {

if len(c.Args()) != 1 {
fmt.Println()
color.Red("Error")
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Println("Error")
ct.ResetColor()
fmt.Println("You must the id of the item you want to change todo.")
fmt.Println("Example: td toggle 1")
fmt.Println()
Expand All @@ -158,7 +166,9 @@ func main() {
return
}

color.Cyan("Your todo is now %s.", todo.Status)
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Printf("Your todo is now %s.\n", todo.Status)
ct.ResetColor()
},
},
{
Expand All @@ -174,7 +184,9 @@ func main() {
fmt.Println(err)
return
} else {
color.Cyan("Your list is now flushed of finished todos.")
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Println("Your list is now flushed of finished todos.")
ct.ResetColor()
}
},
},
Expand All @@ -193,7 +205,9 @@ func main() {
return
}

color.Cyan("Your list is now reordered.")
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Println("Your list is now reordered.")
ct.ResetColor()
},
},
{
Expand All @@ -203,7 +217,9 @@ func main() {
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println()
color.Red("Error")
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Println("Error")
ct.ResetColor()
fmt.Println("You must provide a string earch.")
fmt.Println("Example: td search \"project-1\"")
fmt.Println()
Expand All @@ -215,7 +231,9 @@ func main() {
collection.Search(c.Args()[0])

if len(collection.Todos) == 0 {
color.Cyan("Sorry, there's no todos containing \"%s\".", c.Args()[0])
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Printf("Sorry, there's no todos containing \"%s\".\n", c.Args()[0])
ct.ResetColor()
return
}

Expand All @@ -226,7 +244,9 @@ func main() {
}
fmt.Println()
} else {
color.Cyan("There's no todo to show.")
ct.ChangeColor(ct.Cyan, false, ct.None, false)
fmt.Println("There's no todo to show.")
ct.ResetColor()
}
},
},
Expand All @@ -238,7 +258,9 @@ func main() {

if path == "" {
fmt.Println()
color.Red("Error")
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Println("Error")
ct.ResetColor()
fmt.Println("The environment variable \"TODO_DB_PATH\" must be set.")
fmt.Println("Example: \"export TODO_DB_PATH=$HOME/Dropbox/todo.json\" in your .bash_profile")
fmt.Println()
Expand Down
34 changes: 18 additions & 16 deletions todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/fatih/color"
"github.com/daviddengcn/go-colortext"
)

type Todo struct {
Expand All @@ -18,29 +18,31 @@ type Todo struct {

func (t *Todo) MakeOutput() {
var symbole string
var colorFunction func(...interface{}) string
var color ct.Color

if t.Status == "done" {
colorFunction = color.New(color.FgGreen).SprintFunc()
color = ct.Green
symbole = "✓"
} else {
colorFunction = color.New(color.FgRed).SprintFunc()
color = ct.Red
symbole = "✕"
}

hashtag_reg := regexp.MustCompile("#[^\\s]*")

if hashtag_reg.MatchString(t.Desc) {
hashtag_output := color.New(color.FgYellow).SprintFunc()

colorify_hashtag := func(s string) string {
return hashtag_output(s)
}

t.Desc = hashtag_reg.ReplaceAllStringFunc(t.Desc, colorify_hashtag)
}

space_count := 6 - len(strconv.FormatInt(t.Id, 10))

fmt.Println(strings.Repeat(" ", space_count), t.Id, "|", colorFunction(symbole), t.Desc)
}
fmt.Print(strings.Repeat(" ", space_count), t.Id, "|")
ct.ChangeColor(color, false, ct.None, false)
fmt.Print(symbole)
ct.ResetColor()
pos := 0
for _, token := range hashtag_reg.FindAllStringIndex(t.Desc, -1) {
fmt.Print(t.Desc[pos:token[0]])
ct.ChangeColor(ct.Yellow, false, ct.None, false)
fmt.Print(t.Desc[token[0]:token[1]])
ct.ResetColor()
pos = token[1]
}
fmt.Println(t.Desc[pos:])
}

0 comments on commit f5bf80c

Please sign in to comment.