diff options
| author | lukeflo | 2024-11-16 15:56:23 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-16 15:56:23 +0100 |
| commit | 9c2fef5c1481d852b69ea342a38fec3eb6337524 (patch) | |
| tree | 5ef64bd7b72258f07eeac9f528294d3757818998 /src | |
| parent | c639cd2b7bd37e98e4469b8fb6f6d92660acaed0 (diff) | |
| download | bibiman-9c2fef5c1481d852b69ea342a38fec3eb6337524.tar.gz bibiman-9c2fef5c1481d852b69ea342a38fec3eb6337524.zip | |
replaced some Strings with &str, impl lifetimes for structs
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 20 | ||||
| -rw-r--r-- | src/bibiman.rs | 32 | ||||
| -rw-r--r-- | src/tui/popup.rs | 14 |
3 files changed, 35 insertions, 31 deletions
@@ -29,18 +29,18 @@ use tui_input::Input; // Application. #[derive(Debug)] -pub struct App { +pub struct App<'a> { // Is the application running? pub running: bool, // bibimain - pub bibiman: Bibiman, + pub bibiman: Bibiman<'a>, // Input mode pub input: Input, // Input mode bool pub input_mode: bool, } -impl App { +impl App<'_> { // Constructs a new instance of [`App`]. pub fn new(args: CLIArgs) -> Result<Self> { // Self::default() @@ -228,10 +228,18 @@ impl App { } CmdAction::YankItem => { if let CurrentArea::EntryArea = self.bibiman.current_area { - Bibiman::yank_text(self.bibiman.get_selected_citekey()); + let citekey: &str = &self.bibiman.entry_table.entry_table_items[self + .bibiman + .entry_table + .entry_table_state + .selected() + .unwrap()] + .citekey; + + Bibiman::yank_text(citekey); self.bibiman.popup_area.popup_message( - "Yanked citekey to clipboard:".to_owned(), - self.bibiman.get_selected_citekey().to_string(), + "Yanked citekey to clipboard: ", + citekey, // self.bibiman.get_selected_citekey(), ); } } diff --git a/src/bibiman.rs b/src/bibiman.rs index a677132..0993b76 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -54,7 +54,7 @@ pub enum FormerArea { // Application. #[derive(Debug)] -pub struct Bibiman { +pub struct Bibiman<'a> { // main bib file pub main_bibfile: PathBuf, // main bibliography @@ -72,10 +72,10 @@ pub struct Bibiman { // mode for popup window pub former_area: Option<FormerArea>, // active popup - pub popup_area: PopupArea, + pub popup_area: PopupArea<'a>, } -impl Bibiman { +impl Bibiman<'_> { // Constructs a new instance of [`App`]. pub fn new(args: CLIArgs) -> Result<Self> { let main_bibfile = args.bibfilearg; @@ -185,7 +185,7 @@ impl Bibiman { } } -impl Bibiman { +impl Bibiman<'_> { // Entry Table commands /// Select next entry in Table holding the bibliographic entries. @@ -300,19 +300,15 @@ impl Bibiman { } } - // Get the citekey of the selected entry - pub fn get_selected_citekey(&self) -> &str { - let idx = self.entry_table.entry_table_state.selected().unwrap(); - &self.entry_table.entry_table_items[idx].citekey - } - pub fn run_editor(&mut self, tui: &mut Tui) -> Result<()> { // get filecontent and citekey for calculating line number - let citekey = self.get_selected_citekey(); + let citekey: &str = &self.entry_table.entry_table_items + [self.entry_table.entry_table_state.selected().unwrap()] + .citekey; // create independent copy of citekey for finding entry after updating list let saved_key = citekey.to_owned(); - let filepath = self.main_bibfile.display().to_string(); - let filecontent = self.main_biblio.bibfilestring.clone(); + let filepath = self.main_bibfile.as_os_str(); + let filecontent: &str = &self.main_biblio.bibfilestring; let mut line_count = 0; for line in filecontent.lines() { @@ -324,7 +320,8 @@ impl Bibiman { } else if line_count == filecontent.len() { eprintln!( "Citekey {} not found, opening file {} at line 1", - &citekey, &filepath + citekey, + filepath.to_string_lossy() ); line_count = 0; break; @@ -340,8 +337,7 @@ impl Bibiman { .build() .unwrap(); // Prepare arguments to open file at specific line - let args: Vec<String> = vec![format!("+{}", line_count), filepath]; - let status = cmd.args(&args).status()?; + let status = cmd.arg(format!("+{}", line_count)).arg(filepath).status()?; if !status.success() { eprintln!("Spawning editor failed with status {}", status); } @@ -457,7 +453,7 @@ impl Bibiman { } } -impl Bibiman { +impl Bibiman<'_> { // Tag List commands // Movement @@ -560,7 +556,7 @@ impl Bibiman { } } -impl Bibiman { +impl Bibiman<'_> { // Search Area // Enter the search area diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 7c54b94..e15ed13 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -31,16 +31,16 @@ pub enum PopupKind { } #[derive(Debug, Default)] -pub struct PopupArea { +pub struct PopupArea<'a> { pub is_popup: bool, pub popup_kind: Option<PopupKind>, pub popup_message: String, pub popup_scroll_pos: u16, - pub popup_list: Vec<String>, + pub popup_list: Vec<&'a str>, pub popup_state: ListState, } -impl PopupArea { +impl PopupArea<'_> { pub fn popup_help<'a>() -> Text<'a> { let help = [ ("General", "first"), @@ -100,17 +100,17 @@ impl PopupArea { Text::from(helptext) } - pub fn popup_message(&mut self, message: String, object: String) { + pub fn popup_message(&mut self, message: &str, object: &str) { if object.is_empty() { - self.popup_message = message; + self.popup_message = message.to_owned(); } else { - self.popup_message = format!("{} \"{}\"", message, object); + self.popup_message = message.to_owned() + object; //format!("{} \"{}\"", message, object); } self.popup_kind = Some(PopupKind::Message); self.is_popup = true; } - pub fn popup_selection(&mut self, items: Vec<String>) { + pub fn popup_selection(&mut self, items: Vec<&'static str>) { self.popup_list = items; self.popup_kind = Some(PopupKind::Selection); self.is_popup = true |
