Skip to content

Commit

Permalink
enable ANSI on Windows (#307)
Browse files Browse the repository at this point in the history
* enable ANSI on Windows

* fix for TERM not being set on Windows

* rename

* remove superfluous flush

* move EnableANSI to commands
  • Loading branch information
lionello committed Apr 25, 2024
1 parent 05becc0 commit d7098f5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
provider = cliClient.Provider(pkg.Getenv("DEFANG_PROVIDER", "auto"))
)

func Execute(ctx context.Context) {
func Execute(ctx context.Context) error {
if err := RootCmd.ExecuteContext(ctx); err != nil {
if !errors.Is(err, context.Canceled) {
term.Error("Error:", err)
Expand Down Expand Up @@ -87,8 +87,7 @@ func Execute(ctx context.Context) {
printDefangHint("Please use the following command to see the Defang terms of service:", "terms")
}

FlushAllTracking() // TODO: track errors/panics
os.Exit(int(code))
return ExitCode(code)
}

if hasTty && term.HadWarnings {
Expand All @@ -104,7 +103,7 @@ func Execute(ctx context.Context) {
}
}
}

return nil
}

func SetupCommands() {
Expand Down Expand Up @@ -251,10 +250,14 @@ var RootCmd = &cobra.Command{

// Do this first, since any errors will be printed to the console
switch colorMode {
case ColorAlways:
term.ForceColor(true)
case ColorNever:
term.ForceColor(false)
case ColorAlways:
term.ForceColor(true)
fallthrough
default:
restore := term.EnableANSI()
cobra.OnFinalize(restore)
}

switch provider {
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/cli/command/exitcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package command

import "fmt"

type ExitCode int

func (e ExitCode) Error() string {
return fmt.Sprintf("exit code %d", e)
}
14 changes: 11 additions & 3 deletions src/cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ func main() {
signal.Stop(sigs)
term.Debug("Received interrupt signal; cancelling...")
command.Track("User Interrupted")
command.FlushAllTracking()
cancel()
}()

command.SetupCommands()
command.Execute(ctx)
command.FlushAllTracking()
err := command.Execute(ctx)
command.FlushAllTracking() // TODO: track errors/panics

if err != nil {
// If the error is a command.ExitCode, use its value as the exit code
ec, ok := err.(command.ExitCode)
if !ok {
ec = 1 // should not happen since we always return ExitCode
}
os.Exit(int(ec))
}
}
3 changes: 3 additions & 0 deletions src/pkg/local/local.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package local

import (
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/local/local_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package local

import (
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/term/colorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
IsTerminal = term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) && os.Getenv("TERM") != ""
IsTerminal = term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) && isTerminal()
Stdout = termenv.NewOutput(os.Stdout)
Stderr = termenv.NewOutput(os.Stderr)
CanColor = doColor(Stdout)
Expand Down
14 changes: 14 additions & 0 deletions src/pkg/term/colorizer_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !windows
// +build !windows

package term

import "os"

func EnableANSI() func() {
return func() {}
}

func isTerminal() bool {
return os.Getenv("TERM") != ""
}
5 changes: 5 additions & 0 deletions src/pkg/term/colorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ func TestOutput(t *testing.T) {
}
// Output(Stdout, InfoColor, "Hello, World!")
}

func TestEnableANSI(t *testing.T) {
restore := EnableANSI()
restore()
}
22 changes: 22 additions & 0 deletions src/pkg/term/colorizer_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows
// +build windows

package term

import (
"github.com/muesli/termenv"
)

func EnableANSI() func() {
mode, err := termenv.EnableWindowsANSIConsole()
if err != nil {
return func() {}
}
return func() {
termenv.RestoreWindowsConsole(mode)
}
}

func isTerminal() bool {
return true
}

0 comments on commit d7098f5

Please sign in to comment.