Skip to content

Commit

Permalink
Add log formatter to use with progress bars, enable CSI on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 30, 2024
1 parent 2823f38 commit 452318e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cslog/clearline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cslog

import (
log "github.com/sirupsen/logrus"
)

// ClearLineFormatter is a logrus formatter that clears each line before printing
// used in cases where the log line is updated in place (e.g. progress bar)
//
// Make sure the logger is set to print on a terminal before using this or it will
// pollute the log with control characters.
type ClearLineFormatter struct {
log.TextFormatter
}

func (f *ClearLineFormatter) Format(entry *log.Entry) ([]byte, error) {
const clearLine = "\r\033[K"

result, err := f.TextFormatter.Format(entry)
if err != nil {
return nil, err
}
return append([]byte(clearLine), result...), nil
}
10 changes: 10 additions & 0 deletions cstty/tty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cstty

import (
isatty "github.com/mattn/go-isatty"
)

// IsTTY returns true if the given file is an interactive terminal.
func IsTTY(fd uintptr) bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}
7 changes: 7 additions & 0 deletions cstty/vt_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build unix

package cstty

func EnableVirtualTerminalProcessing(fd uintptr) error {
return nil
}
17 changes: 17 additions & 0 deletions cstty/vt_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cstty

import (
"golang.org/x/sys/windows"
)

// EnableVirtualTerminalProcessing enables ANSI sequences on a given file descriptor.
// This only works on Windows 10+ but we can't do anything about older versions.
func EnableVirtualTerminalProcessing(fd uintptr) error {
var mode uint32
handle := windows.Handle(fd)
if err := windows.GetConsoleMode(handle, &mode); err != nil {
return err
}
mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
return windows.SetConsoleMode(handle, mode)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/coreos/go-systemd/v22 v22.5.0
github.com/mattn/go-isatty v0.0.20
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand All @@ -13,6 +15,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down

0 comments on commit 452318e

Please sign in to comment.