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

Allow effectively overriding isTTY for mocking console output #105

Closed
heaths opened this issue Nov 13, 2022 · 0 comments · Fixed by #109 or #117
Closed

Allow effectively overriding isTTY for mocking console output #105

heaths opened this issue Nov 13, 2022 · 0 comments · Fixed by #109 or #117

Comments

@heaths
Copy link

heaths commented Nov 13, 2022

In order to test CLIs it would be great if

func (o *Output) isTTY() bool {
could effectively be overridden, such we could force treating Output.tty as a TTY or not. This is useful to provide a buffer such that any writes to an Output could include ANSI sequences, depending on which profile we might force. This way, if a CLI wants to test color output or maybe even varies the output based on whether it's a TTY (say, output TSV if not, or using a text/tablewriter if it is a TTY) it can force it.

E.g.

func Test(t *testing.T) {
 buffer := bytes.Buffer{}
  output = termenv.NewOutput(buffer, WithTTY(true), WithProfile(termenv.TrueColor))
  s := output.String("Hello, world!")
  s.Bold()
  fmt.Println(s);
  want := "\x1b[1mHello, world!\x1b[m"
  if got := buffer.String(); got != want  {
    t.Fatal("want %q; got %q", want, got)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant