Skip to content

Commit 18ceeb6

Browse files
committed
πŸ’„ Fix printing issue on Windows cmd (fix #56)
CMD handles colors differently than other terminals. Therefore we need to use the color.Output from fatih/color to ensure that the colors are printed correctly. This commit rewrites all raw printing using fmt.Printf to use the color.Output destination instead of os.Stdout.
1 parent 8efbb35 commit 18ceeb6

File tree

7 files changed

+17
-14
lines changed

7 files changed

+17
-14
lines changed

β€Žsrc/controller/clone.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func Clone(cmd *cobra.Command, args []string) {
7070
}
7171

7272
/* --------------------------------- Clone repo ------------------------------ */
73-
fmt.Printf("\nYour repo is %s and will be cloned in %s", color.GreenString(repo), color.BlueString(path))
73+
fmt.Fprintf(color.Output, "\nYour repo is %s and will be cloned in %s", color.GreenString(repo), color.BlueString(path))
7474
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build a new spinner
7575
s.Start()
7676
err = executor.Clone(repo, path, shouldConserveGitHistory)

β€Žsrc/controller/pathHelper_test.goβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os"
1010
"path/filepath"
1111
"testing"
12+
13+
"github.com/fatih/color"
1214
)
1315

1416
func Test_getAbsPathFromInput(t *testing.T) {
@@ -107,7 +109,7 @@ func Test_checkIfPathExist(t *testing.T) {
107109
for _, tt := range tests {
108110
t.Run(tt.name, func(t *testing.T) {
109111
if got := checkIfPathExist(tt.args.path); got != tt.want {
110-
fmt.Printf("Path: %s", tt.args.path)
112+
fmt.Fprintf(color.Output, "Path: %s", tt.args.path)
111113
t.Errorf("checkIfPathExist() = %v, want %v", got, tt.want)
112114
}
113115
})

β€Žsrc/controller/profile.goβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ func selectProfile(gitURL string, createPossible bool) profile.Profile {
269269
func Profiles(cmd *cobra.Command, args []string) {
270270
print.Message("Print info about the profiles \n", print.Info)
271271
profileSelected := selectProfile("", true)
272-
fmt.Printf(color.HiBlackString("Profile selected: %s \n"), color.HiBlueString(profileSelected.Alias))
273-
fmt.Printf(color.HiBlackString("Email: %s \n"), color.HiBlueString(profileSelected.Email))
274-
fmt.Printf(color.HiBlackString("Username: %s \n"), color.HiBlueString(profileSelected.Username))
275-
fmt.Printf(color.HiBlackString("Website: %s \n"), color.HiBlueString(profileSelected.Website))
276-
fmt.Printf(color.HiBlackString("Internal ID: %s \n"), color.HiBlueString(profileSelected.Id))
272+
fmt.Fprintf(color.Output, color.HiBlackString("Profile selected: %s \n"), color.HiBlueString(profileSelected.Alias))
273+
fmt.Fprintf(color.Output, color.HiBlackString("Email: %s \n"), color.HiBlueString(profileSelected.Email))
274+
fmt.Fprintf(color.Output, color.HiBlackString("Username: %s \n"), color.HiBlueString(profileSelected.Username))
275+
fmt.Fprintf(color.Output, color.HiBlackString("Website: %s \n"), color.HiBlueString(profileSelected.Website))
276+
fmt.Fprintf(color.Output, color.HiBlackString("Internal ID: %s \n"), color.HiBlueString(profileSelected.Id))
277277

278278
}
279279

@@ -290,9 +290,9 @@ func ProfilesList(cmd *cobra.Command, args []string) {
290290
return
291291
} else {
292292

293-
fmt.Println(color.HiBlackString("ID | Alias | Username | Website | Email"))
293+
fmt.Fprintln(color.Output, color.HiBlackString("ID | Alias | Username | Website | Email"))
294294
for key, val := range *profiles {
295-
fmt.Printf(color.HiBlackString("%d. %s | %s | %s | %s | %s \n"), key, val.Id, color.HiBlueString(val.Alias), color.HiBlueString(val.Username), color.HiBlueString(val.Website), color.HiBlueString(val.Email))
295+
fmt.Fprintf(color.Output, color.HiBlackString("%d. %s | %s | %s | %s | %s \n"), key, val.Id, color.HiBlueString(val.Alias), color.HiBlueString(val.Username), color.HiBlueString(val.Website), color.HiBlueString(val.Email))
296296
}
297297
}
298298
}

β€Žsrc/controller/revert.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func Revert(cmd *cobra.Command, args []string) {
9292

9393
// Prompt the user to choose a commit
9494
commit := chooseCommit(commits)
95-
fmt.Printf("I will revert the commit to %s created by %s on %s \n\n", color.HiCyanString(getTitleFromCommit(commit.Message)), color.HiCyanString(commit.Author.Name), commit.Author.When.Format("Mon Jan 2 15:04:05 2006"))
95+
fmt.Fprintf(color.Output, "I will revert the commit to %s created by %s on %s \n\n", color.HiCyanString(getTitleFromCommit(commit.Message)), color.HiCyanString(commit.Author.Name), commit.Author.When.Format("Mon Jan 2 15:04:05 2006"))
9696

9797
err = executor.GitRevert(commit.Hash.String())
9898
if err != nil {

β€Žsrc/controller/status.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Status(cmd *cobra.Command, args []string) {
3535
if err != nil {
3636
exitOnError("Sorry, I can't get the current branch 😒", err)
3737
}
38-
fmt.Printf("On branch %s\n", color.HiGreenString(branch))
38+
fmt.Fprintf(color.Output, "On branch %s\n", color.HiGreenString(branch))
3939
}
4040

4141
spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)

β€Žsrc/controller/whereami.goβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func WhereAmI(cmd *cobra.Command, args []string) {
3737
exitOnError("Sorry, I can't check if the HEAD is detached 😒", err)
3838
}
3939
if detached {
40-
fmt.Printf("HEAD is detached (lookout mode) at %s\n", color.HiGreenString(hash))
40+
fmt.Fprintf(color.Output, "HEAD is detached (lookout mode) at %s\n", color.HiGreenString(hash))
4141
return
4242
}
4343

44-
fmt.Printf("HEAD is at %s on branch %s\n", color.HiGreenString(hash), color.HiGreenString(branch))
44+
fmt.Fprintf(color.Output, "HEAD is at %s on branch %s\n", color.HiGreenString(hash), color.HiGreenString(branch))
4545

4646
}

β€Žsrc/prompt/prompt.goβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"runtime"
88
"strings"
99

10+
"github.com/fatih/color"
1011
"github.com/julien040/gut/src/print"
1112
"github.com/manifoldco/promptui"
1213
)
1314

1415
// InputLine prompts the user for an input and returns it
1516
func InputLine(message string) (string, error) {
16-
fmt.Printf("%s ", message)
17+
fmt.Fprintf(color.Output, "%s ", message)
1718
reader := bufio.NewReader(os.Stdin)
1819
input, err := reader.ReadString('\n')
1920
if err != nil {

0 commit comments

Comments
Β (0)