From 2b9ced1bf081782976e3260891bb0f872c1e1c3e Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Tue, 30 May 2023 10:31:18 +0100 Subject: [PATCH] feat(rink): extract description from result (#37) This splits the rink result to move the bracketed text into the description, meaning copying the result will not contain text such as `(dimensionless)` or `(length)`, while still keeping this information present. --- plugins/rink/src/lib.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/plugins/rink/src/lib.rs b/plugins/rink/src/lib.rs index 4386fde..7df0d0c 100644 --- a/plugins/rink/src/lib.rs +++ b/plugins/rink/src/lib.rs @@ -43,14 +43,17 @@ fn info() -> PluginInfo { #[get_matches] fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec { match rink_core::one_line(ctx, &input) { - Ok(result) => vec![Match { - title: result.into(), - description: ROption::RNone, - use_pango: false, - icon: ROption::RNone, - id: ROption::RNone, - }] - .into(), + Ok(result) => { + let (title, desc) = parse_result(result); + vec![Match { + title: title.into(), + description: desc.map(RString::from).into(), + use_pango: false, + icon: ROption::RNone, + id: ROption::RNone, + }] + .into() + } Err(_) => RVec::new(), } } @@ -59,3 +62,17 @@ fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec { fn handler(selection: Match) -> HandleResult { HandleResult::Copy(selection.title.into_bytes()) } + +/// Extracts the title and description from `rink` result. +/// The description is anything inside brackets from `rink`, if present. +fn parse_result(result: String) -> (String, Option) { + result + .split_once(" (") + .map(|(title, desc)| { + ( + title.to_string(), + Some(desc.trim_end_matches(')').to_string()), + ) + }) + .unwrap_or((result, None)) +}