From cf0e0436bedf79c35f84200a09ff943c9ac20024 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Dec 2022 16:45:00 +0100 Subject: [PATCH] skim: do not panic if terminal is not available Signed-off-by: Azat Khuzhin --- base/base/ReplxxLineReader.cpp | 10 +++++++++- rust/skim/Cargo.lock | 1 + rust/skim/Cargo.toml | 1 + rust/skim/include/skim.h | 2 +- rust/skim/src/lib.rs | 19 ++++++++++++------- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/base/base/ReplxxLineReader.cpp b/base/base/ReplxxLineReader.cpp index c42f8705fa0..9e29f7744fa 100644 --- a/base/base/ReplxxLineReader.cpp +++ b/base/base/ReplxxLineReader.cpp @@ -402,7 +402,15 @@ ReplxxLineReader::ReplxxLineReader( words.push_back(hs.get().text()); } - std::string new_query(skim(words)); + std::string new_query; + try + { + new_query = std::string(skim(words)); + } + catch (const std::exception & e) + { + rx.print("skim failed: %s (consider using Ctrl-T for a regular non-fuzzy reverse search)\n", e.what()); + } if (!new_query.empty()) rx.set_state(replxx::Replxx::State(new_query.c_str(), static_cast(new_query.size()))); diff --git a/rust/skim/Cargo.lock b/rust/skim/Cargo.lock index f61e8a084e1..badd22dad07 100644 --- a/rust/skim/Cargo.lock +++ b/rust/skim/Cargo.lock @@ -9,6 +9,7 @@ dependencies = [ "cxx", "cxx-build", "skim", + "term", ] [[package]] diff --git a/rust/skim/Cargo.toml b/rust/skim/Cargo.toml index f665f249d7c..ce137221065 100644 --- a/rust/skim/Cargo.toml +++ b/rust/skim/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] skim = "0.10.2" cxx = "1.0.83" +term = "0.7.0" [build-dependencies] cxx-build = "1.0.83" diff --git a/rust/skim/include/skim.h b/rust/skim/include/skim.h index 6a2d5806e3e..12cd257567b 100644 --- a/rust/skim/include/skim.h +++ b/rust/skim/include/skim.h @@ -87,4 +87,4 @@ private: } // namespace cxxbridge1 } // namespace rust -::rust::String skim(::std::vector<::std::string> const &words) noexcept; +::rust::String skim(::std::vector<::std::string> const &words); diff --git a/rust/skim/src/lib.rs b/rust/skim/src/lib.rs index 520aaae34c8..29160329287 100644 --- a/rust/skim/src/lib.rs +++ b/rust/skim/src/lib.rs @@ -1,10 +1,11 @@ use skim::prelude::*; +use term::terminfo::TermInfo; use cxx::{CxxString, CxxVector}; #[cxx::bridge] mod ffi { extern "Rust" { - fn skim(words: &CxxVector) -> String; + fn skim(words: &CxxVector) -> Result; } } @@ -17,8 +18,12 @@ impl SkimItem for Item { } } -fn skim(words: &CxxVector) -> String { - // TODO: configure colors +fn skim(words: &CxxVector) -> Result { + // Let's check is terminal available. To avoid panic. + if let Err(err) = TermInfo::from_env() { + return Err(format!("{}", err)); + } + let options = SkimOptionsBuilder::default() .height(Some("30%")) .tac(true) @@ -35,15 +40,15 @@ fn skim(words: &CxxVector) -> String { let output = Skim::run_with(&options, Some(rx)); if output.is_none() { - return "".to_string(); + return Err("skim return nothing".to_string()); } let output = output.unwrap(); if output.is_abort { - return "".to_string(); + return Ok("".to_string()); } if output.selected_items.is_empty() { - return "".to_string(); + return Err("No items had been selected".to_string()); } - return output.selected_items[0].output().to_string(); + return Ok(output.selected_items[0].output().to_string()); }