Merge pull request #60621 from max-sixty/skim

fix(rust): Fix skim's panic handler
This commit is contained in:
Alexey Milovidov 2024-03-04 08:16:00 +03:00 committed by GitHub
commit 9b4b4c0f8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,7 @@
use skim::prelude::*;
use term::terminfo::TermInfo;
use cxx::{CxxString, CxxVector}; use cxx::{CxxString, CxxVector};
use skim::prelude::*;
use std::panic; use std::panic;
use term::terminfo::TermInfo;
#[cxx::bridge] #[cxx::bridge]
mod ffi { mod ffi {
@ -16,7 +16,7 @@ struct Item {
} }
impl Item { impl Item {
fn new(text: String) -> Self { fn new(text: String) -> Self {
return Self{ Self {
// Text that will be printed by skim, and will be used for matching. // Text that will be printed by skim, and will be used for matching.
// //
// Text that will be shown should not contains new lines since in this case skim may // Text that will be shown should not contains new lines since in this case skim may
@ -24,16 +24,16 @@ impl Item {
text_no_newlines: text.replace("\n", " "), text_no_newlines: text.replace("\n", " "),
// This will be used when the match had been selected. // This will be used when the match had been selected.
orig_text: text, orig_text: text,
}; }
} }
} }
impl SkimItem for Item { impl SkimItem for Item {
fn text(&self) -> Cow<str> { fn text(&self) -> Cow<str> {
return Cow::Borrowed(&self.text_no_newlines); Cow::Borrowed(&self.text_no_newlines)
} }
fn output(&self) -> Cow<str> { fn output(&self) -> Cow<str> {
return Cow::Borrowed(&self.orig_text); Cow::Borrowed(&self.orig_text)
} }
} }
@ -88,14 +88,11 @@ fn skim_impl(prefix: &CxxString, words: &CxxVector<CxxString>) -> Result<String,
if output.selected_items.is_empty() { if output.selected_items.is_empty() {
return Err("No items had been selected".to_string()); return Err("No items had been selected".to_string());
} }
return Ok(output.selected_items[0].output().to_string()); Ok(output.selected_items[0].output().to_string())
} }
fn skim(prefix: &CxxString, words: &CxxVector<CxxString>) -> Result<String, String> { fn skim(prefix: &CxxString, words: &CxxVector<CxxString>) -> Result<String, String> {
let ret = panic::catch_unwind(|| { match panic::catch_unwind(|| skim_impl(prefix, words)) {
return skim_impl(prefix, words);
});
return match ret {
Err(err) => { Err(err) => {
let e = if let Some(s) = err.downcast_ref::<String>() { let e = if let Some(s) = err.downcast_ref::<String>() {
format!("{}", s) format!("{}", s)
@ -105,7 +102,7 @@ fn skim(prefix: &CxxString, words: &CxxVector<CxxString>) -> Result<String, Stri
format!("Unknown panic type: {:?}", err.type_id()) format!("Unknown panic type: {:?}", err.type_id())
}; };
Err(format!("Rust panic: {:?}", e)) Err(format!("Rust panic: {:?}", e))
}, }
Ok(res) => res, Ok(res) => res,
} }
} }