From e9be247e3a0cf31270c2bf0f39f4b5ef23964a2d Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 8 May 2015 09:06:40 +0900 Subject: [PATCH 1/5] windows support --- attr.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/attr.go b/attr.go index 1569148..4ae7d27 100644 --- a/attr.go +++ b/attr.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "fmt" "github.com/andrew-d/go-termutil" + "github.com/mattn/go-colorable" "github.com/mgutz/ansi" "io/ioutil" "log" @@ -16,6 +17,8 @@ import ( "time" ) +var out = colorable.NewColorableStderr() + // Attr holds the data fetched from a row // Only 1 ValueXxx field should have value, the others should be nil type Attr struct { @@ -223,12 +226,12 @@ func (attr Attr) Print(w *tabwriter.Writer, verbose bool, indent int, highlighte //fmt.Printf(strings.Repeat(" ", indent)) if attr.GetMark() == 0 { - fmt.Printf("[%s] %s\n", Color(attr.GetIdentifier(), "yellow+b"), attr.Title()) + fmt.Fprintf(out, "[%s] %s\n", Color(attr.GetIdentifier(), "yellow+b"), attr.Title()) } else { - fmt.Printf("[%s] %s\n", Color(attr.GetIdentifier(), "black+b:white"), Color(attr.Title(), "default")) + fmt.Fprintf(out, "[%s] %s\n", Color(attr.GetIdentifier(), "black+b:white"), Color(attr.Title(), "default")) } if len(highlighteds) > 0 { - fmt.Println(attr.PrettyMatches(highlighteds, after)) + fmt.Fprintln(out, attr.PrettyMatches(highlighteds, after)) } } } @@ -318,7 +321,7 @@ func (attr Attr) SetAlias(db *sql.DB, alias string) { if !unset { var validAlias = regexp.MustCompile(`[^\s\d]+`) if !validAlias.MatchString(alias) { - fmt.Println("Alias must contain a non-numeric character") + fmt.Fprintln(out, "Alias must contain a non-numeric character") return } } @@ -335,9 +338,9 @@ func (attr Attr) SetAlias(db *sql.DB, alias string) { //check(err) if err == nil { if unset { - fmt.Printf("ID:%d unaliased\n", attr.GetID()) + fmt.Fprintf(out, "ID:%d unaliased\n", attr.GetID()) } else { - fmt.Printf("alias set: %s => %s\n", attr.GetIdentifier(), alias) + fmt.Fprintf(out, "alias set: %s => %s\n", attr.GetIdentifier(), alias) } } else { log.Fatalf("error while setting alias \"%s\" for ID:%d -- alias must be unique\n", alias, attr.GetID()) // , err) @@ -726,7 +729,7 @@ func InitializeDatabase(db *sql.DB) bool { log.Fatal(err) return false } - fmt.Println("repository initiated") + fmt.Fprintln(out, "repository initiated") return true } From 68a699fa0ce079abad91235698b476907470ac54 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 8 May 2015 09:06:53 +0900 Subject: [PATCH 2/5] Close file --- commands.go | 1 + 1 file changed, 1 insertion(+) diff --git a/commands.go b/commands.go index eca92a2..86d4329 100644 --- a/commands.go +++ b/commands.go @@ -157,6 +157,7 @@ func cmdNew(db *sql.DB, opts Options) bool { } else { f, err := ioutil.TempFile("", "eton-edit") check(err) + f.Close() openEditor(f.Name()) value_text = readFile(f.Name()) From 462171fca0daf976a92b1eb74d03b32529fcea58 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 8 May 2015 09:15:45 +0900 Subject: [PATCH 3/5] check error for opening $EDITOR --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 86d4329..b7e2538 100644 --- a/commands.go +++ b/commands.go @@ -386,7 +386,7 @@ func openEditor(filepath string) { cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout - cmd.Run() + check(cmd.Run()) } func readFile(filepath string) string { From 8272a2c4cb79c5ad2b27be827ba97e2188ef2eb6 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 8 May 2015 09:20:23 +0900 Subject: [PATCH 4/5] if $EDITOR quit with non-zero status, cmdNew should stop creation. --- commands.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/commands.go b/commands.go index b7e2538..f9b88e9 100644 --- a/commands.go +++ b/commands.go @@ -159,7 +159,9 @@ func cmdNew(db *sql.DB, opts Options) bool { check(err) f.Close() - openEditor(f.Name()) + if openEditor(f.Name()) == false { + return false + } value_text = readFile(f.Name()) } @@ -369,7 +371,7 @@ func cmdUnmark(db *sql.DB, opts Options) bool { /******************************************************************************/ -func openEditor(filepath string) { +func openEditor(filepath string) bool { var cmd *exec.Cmd editor := os.Getenv("EDITOR") @@ -386,7 +388,12 @@ func openEditor(filepath string) { cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout - check(cmd.Run()) + err := cmd.Run() + if err != nil { + log.Println(err) + return false + } + return true } func readFile(filepath string) string { From 3f606fb2c77e88003ba00add657a8f14ca1c50ca Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 8 May 2015 09:23:52 +0900 Subject: [PATCH 5/5] Should be Stdout --- attr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/attr.go b/attr.go index 4ae7d27..1a0c577 100644 --- a/attr.go +++ b/attr.go @@ -17,7 +17,7 @@ import ( "time" ) -var out = colorable.NewColorableStderr() +var out = colorable.NewColorableStdout() // Attr holds the data fetched from a row // Only 1 ValueXxx field should have value, the others should be nil