Skip to content

Commit

Permalink
Fix parsing of fg response
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jan 17, 2024
1 parent c95c69e commit 49327bb
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/xterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,35 @@ use std::time::{Duration, Instant};
const MIN_TIMEOUT: Duration = Duration::from_millis(100);

pub(crate) fn foreground_color(options: QueryOptions) -> Result<Color> {
query_color("\x1b]10;?\x07", options, TerminalKind::from_env())
query_color(
"\x1b]10;?\x07",
"\x1b]10;",
options,
TerminalKind::from_env(),
)
}

pub(crate) fn background_color(options: QueryOptions) -> Result<Color> {
query_color("\x1b]11;?\x07", options, TerminalKind::from_env())
query_color(
"\x1b]11;?\x07",
"\x1b]11;",
options,
TerminalKind::from_env(),
)
}

fn query_color(query: &str, options: QueryOptions, terminal: TerminalKind) -> Result<Color> {
query_color_raw(query, options, terminal).and_then(parse_response)
fn query_color(
query: &str,
response_prefix: &str,
options: QueryOptions,
terminal: TerminalKind,
) -> Result<Color> {
query_color_raw(query, options, terminal).and_then(|r| parse_response(r, response_prefix))
}

fn parse_response(response: String) -> Result<Color> {
fn parse_response(response: String, prefix: &str) -> Result<Color> {
response
.strip_prefix("\x1b]11;")
.strip_prefix(prefix)
.and_then(|response| {
response
.strip_suffix('\x07')
Expand Down

0 comments on commit 49327bb

Please sign in to comment.