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

Should only enable colors if there's a terminal attached #1

Open
OnkelTem opened this issue Aug 27, 2024 · 1 comment
Open

Should only enable colors if there's a terminal attached #1

OnkelTem opened this issue Aug 27, 2024 · 1 comment

Comments

@OnkelTem
Copy link

OnkelTem commented Aug 27, 2024

Consider an example:

import 'package:chalkdart/chalk.dart';

void main() {
  print(chalk.blue('Hello World!'));
}

When we run it with dart run, it outputs blue text in the terminal.

However if we redirect the output to a file with dart run > out.txt, the escape-sequences are also get copied:

�[34mHello World!�[39m

I believe it's not a correct default behavior. In the simplest case, we can detect the presence of the terminal this way:

import 'dart:io';
import 'package:chalkdart/chalk.dart';

void main(List<String> arguments) {
  if (stdout.hasTerminal) {
    print(chalk.blue('Hello World!'));
  } else {
    print('Hello World!');
  }
}

— but at the chalkdart's side.

@OnkelTem
Copy link
Author

OnkelTem commented Aug 27, 2024

I found a better way to force chalkdart to disable colors:

import 'dart:io';
import 'package:chalkdart/chalk.dart';

void main() {
  Chalk.ansiColorLevelForNewInstances = stdout.hasTerminal ? 3 : 0;
  print(chalk.blue('Hello World!'));
}

Still I believe it should be the default chakdart behavior to allow for clean piping to files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant