Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable ANSI on Windows #307

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 @@ -84,8 +84,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 @@ -101,7 +100,7 @@ func Execute(ctx context.Context) {
}
}
}

return nil
}

func SetupCommands() {
Expand Down Expand Up @@ -248,10 +247,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 @@ -30,3 +30,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
}