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

Prototype: Detect dark/light asynchronously #12238

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ repository = "https://github.com/helix-editor/helix"
homepage = "https://helix-editor.com"
license = "MPL-2.0"
rust-version = "1.76"

[patch.crates-io]
crossterm = { git = "https://github.com/bash/crossterm", branch = "parse-osc" }
terminal-colorsaurus = { git = "https://github.com/bash/terminal-colorsaurus", branch = "parse-utils" }
1 change: 1 addition & 0 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tokio-stream = "0.1"
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
arc-swap = { version = "1.7.1" }
termini = "1"
terminal-colorsaurus = { version = "0.4.7", default-features = false }

# Logging
fern = "0.7"
Expand Down
10 changes: 10 additions & 0 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,16 @@ impl Application {
};
// Handle key events
let should_redraw = match event.unwrap() {
CrosstermEvent::OscString(osc_string) => {
if let Some(color_string) = osc_string.strip_prefix(b"11;") {
if let Some(color) = terminal_colorsaurus::parse::xparsecolor(color_string) {
let is_light = color.perceived_lightness() >= 50;
self.editor.set_status(format!("is_light = {}", is_light))
}
}

true
}
CrosstermEvent::Resize(width, height) => {
self.terminal
.resize(Rect::new(0, 0, width, height))
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ impl Component for EditorView {
};

match event {
Event::Noop => EventResult::Consumed(None),
Event::Paste(contents) => {
self.handle_non_key_input(&mut cx);
cx.count = cx.editor.count;
Expand Down
23 changes: 23 additions & 0 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ where
)
)?;
}
execute!(self.buffer, QueryBackgroundColor)?;
Ok(())
}

Expand Down Expand Up @@ -462,3 +463,25 @@ impl Command for SetUnderlineColor {
))
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QueryBackgroundColor;

impl crossterm::Command for QueryBackgroundColor {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
f.write_str("\x1b]11;?\x07")
}

#[cfg(windows)]
fn execute_winapi(&self) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"QueryBackgroundColor not supported by winapi.",
))
}

#[cfg(windows)]
fn is_ansi_code_supported(&self) -> bool {
true
}
}
2 changes: 2 additions & 0 deletions helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Event {
Paste(String),
Resize(u16, u16),
IdleTimeout,
Noop,
}

#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
Expand Down Expand Up @@ -441,6 +442,7 @@ impl From<crossterm::event::Event> for Event {
crossterm::event::Event::FocusGained => Self::FocusGained,
crossterm::event::Event::FocusLost => Self::FocusLost,
crossterm::event::Event::Paste(s) => Self::Paste(s),
crossterm::event::Event::OscString(_s) => Self::Noop,
}
}
}
Expand Down