-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log formatter to use with progress bars, enable CSI on windows
- Loading branch information
Showing
6 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters