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

Provide a way to find out if given stdio stream is attached to a terminal #2789

Closed
DartBot opened this issue Apr 28, 2012 · 10 comments
Closed
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Apr 28, 2012

This issue was originally filed by ladicek@gmail.com


Sometimes, it's good to know if a given stdio stream (stdout, most likely) is attached to a terminal. For example, the dart2js compiler output contains ANSI codes for coloring, which is nice for human, but if you redirect the output to a file (or a pipe, whatever), then it's a mess. This situation could be easily fixed, provided that there is a way to find out if stdout is actually attached to a terminal or not.

I'm attaching a patch against the Dart VM that uses 'isatty' on Unices and '_isatty' on Windows (but I'm not sure that _isatty is the right way, see the comment in file_win.cc). Please, let me know if you agree with this change, so that I can issue a pull request, or if you want to discuss it some more.


Attachment:
0001-added-an-isTerminal-function-to-dart-io-for-testing-.patch (4.61 KB)

@DartBot
Copy link
Author

DartBot commented Apr 28, 2012

This comment was originally written by ladicek@gmail.com


Quick note about tests: I'm not familliar with Dart testing, and I can't currently imagine a way to test this feature. If I'd run a testing Dart script from Dart test, then it would never be attached to a terminal, so I could only test a half of it. But maybe that's better than nothing, so I'll take a look at it.

@DartBot
Copy link
Author

DartBot commented Apr 28, 2012

This comment was originally written by ladicek@gmail.com


OK, adding second version of the patch with a simple test.


Attachment:
0001-added-an-isTerminal-function-to-dart-io-for-testing-.patch (6.07 KB)

@dgrove
Copy link
Contributor

dgrove commented Apr 28, 2012

cc @sgjesse.
cc @madsager.
Added Area-IO, Triaged labels.

@sgjesse
Copy link
Contributor

sgjesse commented Apr 30, 2012

In stdio.dart we already have the function _getStdioHandleType that calls a native function to determine whether a file descriptor is bound to a terminal, pipe, file or socket. We should leverage that either by adding a general function to get the type of a stream

class StreamType {
  static final StreamType TERMINAL = const StreamType("TERMINAL");
  static final StreamType PIPE = const StreamType("PIPE");
  static final StreamType FILE = const StreamType("FILE");
  static final StreamType SOCKET = const StreamType("SOCKET");
  const StreamType(String this.name);
  final String name;
}

StreamType streamType(stream) {
}

or add a type getter to the InputStream and OutputStream interfaces.

The problem with the general function is that it will not be able to handle new stream types. E.g we already have the list input and output streams. If we have the type getter on the stream interfaces it would be possible to add new types, e.g.

// List stream implementation file.

static final StreamType _listStreamType = const StreamType("LIST");

class ListInputStream implements InputStream {

  ...

  StreamType type() => _listStreamType;

  ...
}


Set owner to @sgjesse.
Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.

@DartBot
Copy link
Author

DartBot commented Apr 30, 2012

This comment was originally written by ladicek@gmail.com


AFAIU, _getStdioHandleType returns "terminal" everytime the stream is attached to a character device:

    if (S_ISCHR(buf.st_mode)) return kTerminal;

I don't really understand POSIX, but I don't think that when S_ISCHR() is true, isatty() is also true. I'll leave the decision up to you.

@madsager
Copy link
Contributor

madsager commented Jun 7, 2012

Added this to the Later milestone.

@dgrove
Copy link
Contributor

dgrove commented Jan 4, 2013

Lack of this is feature appears to be at the root of some ugly output when I invoke the web ui compiler from the editor.

In the editor, if I run build.dart manually (ie, I click "run") for a web ui app, I get the following:

dart --enable-checked-mode build.dart

Total time spent on web/books.html -- �[32m239 ms�[0m

The web ui compiler is coloring its output, which is nice on terminals, but is not nice elsewhere. It would be great for the compiler to know whether it's in a terminal or not.

Any chance of getting this soonish?

@sgjesse
Copy link
Contributor

sgjesse commented Jan 8, 2013

Added Started label.

@sgjesse
Copy link
Contributor

sgjesse commented Jan 9, 2013

Fixed in https://code.google.com/p/dart/source/detail?r=16839.


Added Fixed label.

@kevmoo
Copy link
Member

kevmoo commented May 14, 2014

Removed Area-IO label.
Added Area-Library, Library-IO labels.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io labels May 14, 2014
@DartBot DartBot added this to the Later milestone May 14, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants