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

Add color support for Windows consoles #2804

Merged
merged 2 commits into from Jun 29, 2016
Merged

Add color support for Windows consoles #2804

merged 2 commits into from Jun 29, 2016

Conversation

skeleten
Copy link
Contributor

Unfortunately, the term API makes this PR rather inelegant, any hints/pointers to make it better are highly appreciated!

Fixes #2803

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@retep998
Copy link
Member

retep998 commented Jun 26, 2016

Note that this PR removes support for terminfo consoles on Windows. Ideally what this should do is first check for Windows console support, and then if that fails try terminfo, and only if that also fails fallback to no color.

Also note that regardless of how this code is written, it is still fundamentally flawed due to Stebalien/term#62 but hopefully still an improvement over what we have currently.

},
// This should never happens, since we checked that the creation
// would succeed above.
Err(e) => unreachable!("Could not create WinConsole: {:?}", e),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this punt the error upwards as even though the first one succeeded the second one may still have a small window where it could fail?

Copy link
Member

@retep998 retep998 Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That small window being someone calls FreeConsole between the first and second call to WinConsole::new. I can't think of any other way only the second call could fail.

Copy link
Contributor Author

@skeleten skeleten Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is easy enough though - and If I replace the unreachable! with Shell::get_terminfo_term the previous check should be obsolete, it seems more elegant that way to me, actually.
Edit - after checking with @retep998, the problem is that creating a WinConsole consumes the writer - making anything but an error impossible. I will make the functions return a Result which, however opens up the question where the error can ultimately get handled properly

@alexcrichton
Copy link
Member

Thanks @skeleten!

@skeleten
Copy link
Contributor Author

I added a commit - Letting the errors bubble up and added a create_fallback method that directly creates a NoColor term around the given Box. This is used by the method shell in cargo/lib.rs in case the usual creation fails.

@@ -147,27 +147,61 @@ impl MultiShell {
}

impl Shell {
pub fn create(out: Box<Write + Send>, config: ShellConfig) -> Shell {
pub fn create(out: Box<Write + Send>, config: ShellConfig) -> CargoResult<Shell> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only ever really called in this one crate, so perhaps the logic of create_fallback could be folded into create itself?

That is, could we remove create_fallback?

Copy link
Contributor Author

@skeleten skeleten Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem there is, that the out is consumed - It'd needed to be newly created by the caller. As such I'm not really sure if that's at all possible.

If Rust were to support default arguments, it could be easily be one function, but without them I do not really see how at the moment.

Edit: I suppose the flag could be added the ShellConfig. Thoughts on that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could take FnMut() -> Box<Write + Send> and just call that multiple times if necessary? The actual handles are just stdout/stderr so they can be manufactured multiple tiems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a great idea! I'll be rewriting it in such a manner straight away :)

@alexcrichton
Copy link
Member

Looks good to me, thanks! Can you squash the commits as well?

@alexcrichton
Copy link
Member

@bors: r+

Thanks @skeleten!

@bors
Copy link
Collaborator

bors commented Jun 29, 2016

📌 Commit 6edf535 has been approved by alexcrichton

@retep998
Copy link
Member

@skeleten You appear to have a merge commit in your PR.

@bors
Copy link
Collaborator

bors commented Jun 29, 2016

⌛ Testing commit 6edf535 with merge 5716f32...

bors added a commit that referenced this pull request Jun 29, 2016
Add color support for Windows consoles

Unfortunately, the `term` API makes this PR rather inelegant, any hints/pointers to make it better are highly appreciated!

Fixes #2803
@bors
Copy link
Collaborator

bors commented Jun 29, 2016

@bors bors merged commit 6edf535 into rust-lang:master Jun 29, 2016
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

Successfully merging this pull request may close these issues.

None yet

5 participants