Strip ANSI codes for messages on simple terminals

We had been doing this for actions, but not messages.

Test: m nothing && TERM=dumb m nothing
Bug: 235414673
Change-Id: I5c6c009490abe1d02a4ae2272bd1831bd7ca9f7f
This commit is contained in:
Dan Willemsen
2022-06-08 20:32:07 -07:00
parent 5c6ebfa08c
commit e2cdecf7ba
2 changed files with 11 additions and 5 deletions

View File

@@ -46,7 +46,11 @@ func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool, quie
func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
if level >= s.outputLevel {
fmt.Fprintln(s.writer, s.formatter.message(level, message))
output := s.formatter.message(level, message)
if !s.keepANSI {
output = string(stripAnsiEscapes([]byte(output)))
}
fmt.Fprintln(s.writer, output)
}
}

View File

@@ -81,9 +81,9 @@ func TestStatusOutput(t *testing.T) {
},
{
name: "action with output with ansi codes",
calls: actionWithOuptutWithAnsiCodes,
smart: "\r\x1b[1m[ 0% 0/1] action1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] action1\x1b[0m\x1b[K\n\x1b[31mcolor\x1b[0m\n",
simple: "[100% 1/1] action1\ncolor\n",
calls: actionWithOutputWithAnsiCodes,
smart: "\r\x1b[1m[ 0% 0/1] action1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] action1\x1b[0m\x1b[K\n\x1b[31mcolor\x1b[0m\n\x1b[31mcolor message\x1b[0m\n",
simple: "[100% 1/1] action1\ncolor\ncolor message\n",
},
}
@@ -257,12 +257,14 @@ func actionWithLongDescription(stat status.StatusOutput) {
runner.finishAction(result1)
}
func actionWithOuptutWithAnsiCodes(stat status.StatusOutput) {
func actionWithOutputWithAnsiCodes(stat status.StatusOutput) {
result1WithOutputWithAnsiCodes := status.ActionResult{Action: action1, Output: "\x1b[31mcolor\x1b[0m"}
runner := newRunner(stat, 1)
runner.startAction(action1)
runner.finishAction(result1WithOutputWithAnsiCodes)
stat.Message(status.PrintLvl, "\x1b[31mcolor message\x1b[0m")
}
func TestSmartStatusOutputWidthChange(t *testing.T) {