-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bash/windows
Windows
- Loading branch information
Showing
17 changed files
with
334 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Waiting for Console Input with a Timeout | ||
This can be achieved using `WaitForSingleObject`. | ||
|
||
```rust | ||
use crate::{Error, Result}; | ||
use std::io; | ||
use std::os::windows::io::AsRawHandle; | ||
use std::time::Duration; | ||
use terminal_trx::Transceive; | ||
use windows_sys::Win32::Foundation::{WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT}; | ||
use windows_sys::Win32::System::Threading::WaitForSingleObject; | ||
|
||
pub(crate) fn poll_read(terminal: &dyn Transceive, timeout: Duration) -> Result<()> { | ||
let handle = terminal.input_buffer_handle(); | ||
match unsafe { | ||
WaitForSingleObject(handle.as_raw_handle() as isize, timeout.as_millis() as u32) | ||
} { | ||
// The state of the specified object is signaled. | ||
WAIT_OBJECT_0 => Ok(()), | ||
WAIT_ABANDONED | WAIT_TIMEOUT => Err(Error::Timeout(timeout)), | ||
_ => Err(Error::Io(io::Error::last_os_error())), | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Windows | ||
## Windows Terminal | ||
The Win32 API provides access to the console foreground and background colors. | ||
However, this currently does not work in Windows Terminal. [Incorrect colors are reported] as a result. | ||
|
||
Hopefully Windows Terminal [will support] querying for the colors using the OSC sequences. | ||
|
||
## Other Terminals | ||
Terminals relying on [conpty] that support OSC sequences on other platforms | ||
do not support them of Windows because [conhost intercepts these OSC sequences]. | ||
|
||
[Incorrect colors are reported]: https://github.com/microsoft/terminal/issues/10639 | ||
[will support]: https://github.com/microsoft/terminal/issues/3718 | ||
[conpty]: https://learn.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session | ||
[conhost intercepts these OSC sequences]: https://github.com/microsoft/terminal/issues/1173 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
# term-color | ||
TODO | ||
Determines the background and foreground color of the terminal | ||
using the `OSC 10` and `OSC 11` terminal sequence. | ||
On Windows, the colors are queried using the Win32 Console API. | ||
|
||
This is useful for answering the question *"Is this terminal dark or light?"*. | ||
|
||
## Example | ||
```rust,no_run | ||
use term_color::{background_color, QueryOptions}; | ||
let bg = background_color(QueryOptions::default()); | ||
// Perceived lightness is a value between 0 (black) and 100 (white) | ||
let is_light = bg.map(|c| c.perceived_lightness() >= 50).unwrap_or_default(); | ||
``` | ||
|
||
## Wishlist | ||
These are some features that I would like to include in this crate, | ||
but have not yet had the time to implement. PRs are welcome :) | ||
|
||
* [ ] A CLI tool version of this library. | ||
* [ ] Improve dynamic latency strategy (iTerm is a bit of a headache here as it has quite a big range when it comes to latency). | ||
* [ ] Add support for `$COLORFGBG` | ||
(I don't know if this is a good idea actually, the [readme](https://github.com/Canop/terminal-light#colorfgbg-strategy) of `terminal-light` lists some downsides.) | ||
|
||
## Inspiration | ||
This crate borrows ideas from many other projects. This list is by no means exhaustive. | ||
|
||
* [xterm-query]: Use `mio` to wait for the terminal's response with a timeout. | ||
* [termbg]: Lists a lot of terminals which served as a good starting point for me to test terminals as well. | ||
* [macOS doesn't like polling /dev/tty][macos-dev-tty] by Nathan Craddock | ||
* [This excellent answer on Stack Overflow][perceived-lightness] for determining the perceived lightness of a color. | ||
|
||
## License | ||
Licensed under either of | ||
|
||
* Apache License, Version 2.0 | ||
([license-apache.txt](license-apache.txt) or http://www.apache.org/licenses/LICENSE-2.0) | ||
([license-apache.txt](license-apache.txt) or <http://www.apache.org/licenses/LICENSE-2.0>) | ||
* MIT license | ||
([license-mit.txt](license-mit.txt) or http://opensource.org/licenses/MIT) | ||
([license-mit.txt](license-mit.txt) or <http://opensource.org/licenses/MIT>) | ||
|
||
at your option. | ||
|
||
## Contribution | ||
Unless you explicitly state otherwise, any contribution intentionally submitted | ||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be | ||
dual licensed as above, without any additional terms or conditions. | ||
|
||
[xterm-query]: https://github.com/Canop/xterm-query | ||
[termbg]: https://github.com/dalance/termbg | ||
[macos-dev-tty]: https://nathancraddock.com/blog/macos-dev-tty-polling/ | ||
[perceived-lightness]: https://stackoverflow.com/a/56678483 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
use std::error::Error; | ||
|
||
use term_color::QueryOptions; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let color = term_color::background_color(QueryOptions::default())?; | ||
dbg!(color.perceived_lightness()); | ||
dbg!(color.perceived_lightness() <= 50); | ||
dbg!(color); | ||
dbg!(term_color::foreground_color(QueryOptions::default())?); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.