From 75b0005619f6e6d3e5e0ba775083b16cd46dac30 Mon Sep 17 00:00:00 2001 From: Sabu Siyad Date: Tue, 28 Jun 2022 23:52:38 +0530 Subject: [PATCH] Add hexadecimal support to --embed Closes #6145. --- CHANGELOG.md | 1 + alacritty/src/cli.rs | 31 +++++++++++++++++++++++++++++-- extra/alacritty.man | 2 +- extra/completions/_alacritty | 2 +- extra/completions/alacritty.fish | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 507f3ccc..8591416e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Escape sequence to set underline color (`CSI 58 : 2 : Ps : Ps : Ps m`/`CSI 58 : 5 : Ps m`) - Escape sequence to reset underline color (`CSI 59 m`) - Vi mode keybinding (z) to center view around vi mode cursor +- Accept hexadecimal values starting with `0x` for `--embed` ### Changed diff --git a/alacritty/src/cli.rs b/alacritty/src/cli.rs index be377700..8872ec99 100644 --- a/alacritty/src/cli.rs +++ b/alacritty/src/cli.rs @@ -1,4 +1,5 @@ use std::cmp::max; +use std::os::raw::c_ulong; use std::path::PathBuf; #[cfg(unix)] @@ -25,7 +26,7 @@ pub struct Options { #[clap(long)] pub ref_test: bool, - /// Defines the X11 window ID (as a decimal integer) to embed Alacritty within. + /// X11 window ID to embed Alacritty within (decimal or hexadecimal with "0x" prefix). #[clap(long)] pub embed: Option, @@ -100,7 +101,7 @@ impl Options { } config.window.dynamic_title &= self.window_options.window_identity.title.is_none(); - config.window.embed = self.embed.as_ref().and_then(|embed| embed.parse().ok()); + config.window.embed = self.embed.as_ref().and_then(|embed| parse_hex_or_decimal(embed)); config.debug.print_events |= self.print_events; config.debug.log_level = max(config.debug.log_level, self.log_level()); config.debug.ref_test |= self.ref_test; @@ -173,6 +174,14 @@ fn parse_class(input: &str) -> Result { } } +/// Convert to hex if possible, else decimal +fn parse_hex_or_decimal(input: &str) -> Option { + input + .strip_prefix("0x") + .and_then(|value| c_ulong::from_str_radix(value, 16).ok()) + .or_else(|| input.parse().ok()) +} + /// Terminal specific cli options which can be passed to new windows via IPC. #[derive(Serialize, Deserialize, Args, Default, Debug, Clone, PartialEq, Eq)] pub struct TerminalOptions { @@ -393,6 +402,24 @@ mod tests { assert!(class.is_err()); } + #[test] + fn valid_decimal() { + let value = parse_hex_or_decimal("10485773"); + assert_eq!(value, Some(10485773)); + } + + #[test] + fn valid_hex_to_decimal() { + let value = parse_hex_or_decimal("0xa0000d"); + assert_eq!(value, Some(10485773)); + } + + #[test] + fn invalid_hex_to_decimal() { + let value = parse_hex_or_decimal("0xa0xx0d"); + assert_eq!(value, None); + } + #[cfg(target_os = "linux")] #[test] fn completions() { diff --git a/extra/alacritty.man b/extra/alacritty.man index 1ac63f04..eec20d60 100644 --- a/extra/alacritty.man +++ b/extra/alacritty.man @@ -52,7 +52,7 @@ Alacritty looks for the configuration file at the following paths: On Windows, the configuration file is located at %APPDATA%\\alacritty\\alacritty.yml. .TP \fB\-\-embed\fR -Defines the X11 window ID (as a decimal integer) to embed Alacritty within +X11 window ID to embed Alacritty within (decimal or hexadecimal with "0x" prefix) .TP \fB\-o\fR, \fB\-\-option\fR