parse OSC 4
This code is used to change the color palette at runtime. We can parse it but not do anything useful with it yet.
This commit is contained in:
1 parent
fb1a5b42d2
commit
d18dbe9ff7
3 files changed
+33
-2
No files matched your search
@@ -2100,6 +2100,13 @@ impl<'a> Performer<'a> {
|
||||
OperatingSystemCommand::SystemNotification(message) => {
|
||||
eprintln!("Application sends SystemNotification: {}", message);
|
||||
}
|
||||
OperatingSystemCommand::ChangeColorNumber(palette_index, colorspec) => {
|
||||
eprintln!(
|
||||
"ChangeColorNumber {} to {}",
|
||||
palette_index,
|
||||
colorspec.to_rgb_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -128,6 +128,16 @@ impl RgbColor {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a color from an SVG/CSS3 color name.
|
||||
/// or from a string of the form `#RRGGBB` where
|
||||
/// R, G and B are all hex digits.
|
||||
/// Returns None if the supplied name is not recognized.
|
||||
/// The list of names can be found here:
|
||||
/// <https://ogeon.github.io/docs/palette/master/palette/named/index.html>
|
||||
pub fn from_named_or_rgb_string(s: &str) -> Option<Self> {
|
||||
RgbColor::from_rgb_str(&s).or_else(|| RgbColor::from_named(&s))
|
||||
}
|
||||
}
|
||||
|
||||
/// This is mildly unfortunate: in order to round trip RgbColor with serde
|
||||
@@ -153,8 +163,7 @@ impl<'de> Deserialize<'de> for RgbColor {
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
RgbColor::from_rgb_str(&s)
|
||||
.or_else(|| RgbColor::from_named(&s))
|
||||
RgbColor::from_named_or_rgb_string(&s)
|
||||
.ok_or_else(|| format!("unknown color name: {}", s))
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::color::RgbColor;
|
||||
pub use crate::hyperlink::Hyperlink;
|
||||
use base64;
|
||||
use bitflags::bitflags;
|
||||
@@ -20,6 +21,7 @@ pub enum OperatingSystemCommand {
|
||||
SetSelection(Selection, String),
|
||||
SystemNotification(String),
|
||||
ITermProprietary(ITermProprietary),
|
||||
ChangeColorNumber(usize, RgbColor),
|
||||
|
||||
Unspecified(Vec<Vec<u8>>),
|
||||
}
|
||||
@@ -125,6 +127,17 @@ impl OperatingSystemCommand {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_change_color_number(osc: &[&[u8]]) -> Fallible<Self> {
|
||||
ensure!(osc.len() == 3, "wrong number of params");
|
||||
|
||||
let index: usize = str::from_utf8(osc[1])?.parse()?;
|
||||
let spec = str::from_utf8(osc[2])?;
|
||||
let spec = RgbColor::from_named_or_rgb_string(spec)
|
||||
.ok_or_else(|| err_msg("invalid color spec"))?;
|
||||
|
||||
Ok(OperatingSystemCommand::ChangeColorNumber(index, spec))
|
||||
}
|
||||
|
||||
fn internal_parse(osc: &[&[u8]]) -> Fallible<Self> {
|
||||
ensure!(!osc.is_empty(), "no params");
|
||||
let p1str = String::from_utf8_lossy(osc[0]);
|
||||
@@ -154,6 +167,7 @@ impl OperatingSystemCommand {
|
||||
ITermProprietary => {
|
||||
self::ITermProprietary::parse(osc).map(OperatingSystemCommand::ITermProprietary)
|
||||
}
|
||||
ChangeColorNumber => Self::parse_change_color_number(osc),
|
||||
|
||||
_ => bail!("not impl"),
|
||||
}
|
||||
@@ -221,6 +235,7 @@ impl Display for OperatingSystemCommand {
|
||||
SetSelection(s, val) => write!(f, "52;{};{}", s, base64::encode(val))?,
|
||||
SystemNotification(s) => write!(f, "9;{}", s)?,
|
||||
ITermProprietary(i) => i.fmt(f)?,
|
||||
ChangeColorNumber(index, spec) => write!(f, "4;{};{}", index, spec.to_rgb_string())?,
|
||||
};
|
||||
write!(f, "\x07")?;
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user