aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlukeflo2024-11-23 00:49:00 +0100
committerlukeflo2024-11-23 00:49:00 +0100
commite354e97facdf61abb20a06f5bae2bac5e86c2d62 (patch)
treece3f15e72289f25f8c4a06fb5f5b891d3a643f60
parent53679da34cd7fe84aaac2a15f936e1450de7c125 (diff)
downloadbibiman-e354e97facdf61abb20a06f5bae2bac5e86c2d62.tar.gz
bibiman-e354e97facdf61abb20a06f5bae2bac5e86c2d62.zip
implement needed dep injection of cli args
-rw-r--r--src/app.rs14
-rw-r--r--src/bibiman.rs21
-rw-r--r--src/cliargs.rs6
-rw-r--r--src/main.rs4
-rw-r--r--src/tui.rs6
-rw-r--r--src/tui/ui.rs29
6 files changed, 30 insertions, 50 deletions
diff --git a/src/app.rs b/src/app.rs
index 7b6a30d..cd27996 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -44,7 +44,7 @@ pub struct App {
impl App {
// Constructs a new instance of [`App`].
- pub fn new(args: CLIArgs) -> Result<Self> {
+ pub fn new(args: &CLIArgs) -> Result<Self> {
// Self::default()
let running = true;
let input = Input::default();
@@ -57,14 +57,14 @@ impl App {
})
}
- pub async fn run(&mut self) -> Result<()> {
+ pub async fn run(&mut self, args: &CLIArgs) -> Result<()> {
let mut tui = tui::Tui::new()?;
tui.enter()?;
// Start the main loop.
while self.running {
// Render the user interface.
- tui.draw(self)?;
+ tui.draw(self, args)?;
// Handle events.
match tui.next().await? {
Event::Tick => self.tick(),
@@ -83,10 +83,10 @@ impl App {
} else {
CmdAction::from(key_event)
};
- self.run_command(command, &mut tui)?
+ self.run_command(command, args, &mut tui)?
}
Event::Mouse(mouse_event) => {
- self.run_command(CmdAction::from(mouse_event), &mut tui)?
+ self.run_command(CmdAction::from(mouse_event), args, &mut tui)?
}
Event::Resize(_, _) => {}
@@ -108,7 +108,7 @@ impl App {
self.running = false;
}
- pub fn run_command(&mut self, cmd: CmdAction, tui: &mut Tui) -> Result<()> {
+ pub fn run_command(&mut self, cmd: CmdAction, args: &CLIArgs, tui: &mut Tui) -> Result<()> {
match cmd {
CmdAction::Input(cmd) => match cmd {
InputCmdAction::Nothing => {}
@@ -278,7 +278,7 @@ impl App {
}
CmdAction::EditFile => {
if let CurrentArea::EntryArea = self.bibiman.current_area {
- self.bibiman.run_editor(tui)?;
+ self.bibiman.run_editor(args, tui)?;
}
}
CmdAction::Open => {
diff --git a/src/bibiman.rs b/src/bibiman.rs
index 3ae0520..3da0a24 100644
--- a/src/bibiman.rs
+++ b/src/bibiman.rs
@@ -25,7 +25,6 @@ use arboard::Clipboard;
use color_eyre::eyre::{Ok, Result};
use editor_command::EditorBuilder;
use ratatui::widgets::ScrollbarState;
-use std::path::PathBuf;
use std::process::Command;
use tui_input::Input;
@@ -55,7 +54,7 @@ pub enum FormerArea {
#[derive(Debug)]
pub struct Bibiman {
// main bib file
- pub main_bibfiles: Vec<PathBuf>,
+ // pub main_bibfiles: Vec<PathBuf>,
// main bibliography
pub main_biblio: BibiSetup,
// search struct:
@@ -76,15 +75,15 @@ pub struct Bibiman {
impl Bibiman {
// Constructs a new instance of [`App`].
- pub fn new(args: CLIArgs) -> Result<Self> {
- let main_bibfiles = args.pos_args;
- let main_biblio = BibiSetup::new(&main_bibfiles);
+ pub fn new(args: &CLIArgs) -> Result<Self> {
+ // let main_bibfiles = args.pos_args.clone();
+ let main_biblio = BibiSetup::new(&args.pos_args);
let tag_list = TagList::new(main_biblio.keyword_list.clone());
let search_struct = BibiSearch::default();
let entry_table = EntryTable::new(&main_biblio.entry_list);
let current_area = CurrentArea::EntryArea;
Ok(Self {
- main_bibfiles,
+ // main_bibfiles,
main_biblio,
tag_list,
search_struct,
@@ -122,8 +121,8 @@ impl Bibiman {
self.former_area = None;
}
- pub fn update_lists(&mut self) {
- self.main_biblio = BibiSetup::new(&self.main_bibfiles);
+ pub fn update_lists(&mut self, args: &CLIArgs) {
+ self.main_biblio = BibiSetup::new(&args.pos_args);
self.tag_list = TagList::new(self.main_biblio.keyword_list.clone());
self.entry_table = EntryTable::new(&self.main_biblio.entry_list);
}
@@ -299,7 +298,7 @@ impl Bibiman {
}
}
- pub fn run_editor(&mut self, tui: &mut Tui) -> Result<()> {
+ pub fn run_editor(&mut self, args: &CLIArgs, tui: &mut Tui) -> Result<()> {
// get filecontent and citekey for calculating line number
let citekey: &str = &self.entry_table.entry_table_items
[self.entry_table.entry_table_state.selected().unwrap()]
@@ -308,7 +307,7 @@ impl Bibiman {
let saved_key = citekey.to_owned();
// TODO: Only for testing purposes, needs better logic to find correct file
// when using multi file approach
- let filepath = self.main_bibfiles[0].as_os_str();
+ let filepath = args.pos_args[0].clone().into_os_string();
let filecontent: &str = &self.main_biblio.bibfilestring;
let mut line_count = 0;
@@ -348,7 +347,7 @@ impl Bibiman {
tui.terminal.clear()?;
// Update the database and the lists to show changes
- self.update_lists();
+ self.update_lists(args);
// Search for entry, selected before editing, by matching citekeys
// Use earlier saved copy of citekey to match
diff --git a/src/cliargs.rs b/src/cliargs.rs
index 1b03019..0a1c0b5 100644
--- a/src/cliargs.rs
+++ b/src/cliargs.rs
@@ -24,7 +24,7 @@ use std::path::PathBuf;
use walkdir::WalkDir;
// struct for CLIArgs
-#[derive(Debug, Default)]
+#[derive(Debug, Default, Clone)]
pub struct CLIArgs {
pub helparg: bool,
pub versionarg: bool,
@@ -64,8 +64,8 @@ pub fn parse_files(args: &mut CLIArgs, pos_arg: OsString) {
}
} else {
println!(
- "{} {}",
- "The positional argument is neither a valid file, nor a path:"
+ "{}\n{}",
+ "The positional argument is neither a valid file, nor a directory:"
.red()
.bold(),
path.as_os_str().to_string_lossy().bright_red().italic()
diff --git a/src/main.rs b/src/main.rs
index 747a8f1..79041fe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -55,8 +55,8 @@ async fn main() -> Result<()> {
init_error_hooks()?;
// Create an application.
- let mut app = App::new(parsed_args)?;
+ let mut app = App::new(&parsed_args)?;
- app.run().await?;
+ app.run(&parsed_args).await?;
Ok(())
}
diff --git a/src/tui.rs b/src/tui.rs
index ba43b2e..113d33d 100644
--- a/src/tui.rs
+++ b/src/tui.rs
@@ -19,7 +19,7 @@ pub mod commands;
pub mod popup;
pub mod ui;
-use crate::App;
+use crate::{cliargs::CLIArgs, App};
use crossterm::{
cursor,
event::{
@@ -194,11 +194,11 @@ impl Tui {
//
// [`Draw`]: ratatui::Terminal::draw
// [`rendering`]: crate::ui::render
- pub fn draw(&mut self, app: &mut App) -> Result<()> {
+ pub fn draw(&mut self, app: &mut App, args: &CLIArgs) -> Result<()> {
// self.terminal.draw(|frame| ui::render(app, frame))?;
self.terminal
// .draw(|frame| frame.render_widget(app, frame.area()))?;
- .draw(|frame| ui::render_ui(app, frame))?;
+ .draw(|frame| ui::render_ui(app, args, frame))?;
Ok(())
}
diff --git a/src/tui/ui.rs b/src/tui/ui.rs
index fd0fc6f..41a18d8 100644
--- a/src/tui/ui.rs
+++ b/src/tui/ui.rs
@@ -18,6 +18,7 @@
use super::popup::PopupArea;
use crate::bibiman::entries::EntryTableColumn;
use crate::bibiman::{CurrentArea, FormerArea};
+use crate::cliargs::CLIArgs;
use crate::tui::popup::PopupKind;
use crate::App;
use crate::{
@@ -123,7 +124,7 @@ pub const fn color_list(list_item: i32, sel_item: i32, highlight: u8, max_diff:
}
}
-pub fn render_ui(app: &mut App, frame: &mut Frame) {
+pub fn render_ui(app: &mut App, args: &CLIArgs, frame: &mut Frame) {
let [header_area, main_area, footer_area] = Layout::new(
Direction::Vertical,
[
@@ -155,7 +156,7 @@ pub fn render_ui(app: &mut App, frame: &mut Frame) {
render_entrytable(app, frame, entry_area);
render_selected_item(app, frame, info_area);
render_taglist(app, frame, tag_area);
- render_file_info(app, frame, entry_info_area);
+ render_file_info(app, args, frame, entry_info_area);
if app.bibiman.popup_area.is_popup {
render_popup(app, frame);
}
@@ -270,23 +271,6 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) {
frame.render_widget(Clear, popup_area);
frame.render_stateful_widget(list, popup_area, &mut app.bibiman.popup_area.popup_state)
- // let sized_list = SizedWrapper {
- // inner: list.clone(),
- // width: (frame.area().width / 2) as usize,
- // height: list.len(),
- // };
-
- // let popup = Popup::new(sized_list)
- // .title(" Select ".bold().into_centered_line().fg(KEYWORD_COLOR))
- // .border_set(symbols::border::THICK)
- // // .border_style(Style::new().fg(CONFIRM_COLOR))
- // .style(POPUP_HELP_BOX);
-
- // frame.render_stateful_widget(
- // &popup,
- // frame.area(),
- // &mut app.bibiman.popup_area.popup_state,
- // )
}
None => {}
}
@@ -338,7 +322,7 @@ pub fn render_footer(app: &mut App, frame: &mut Frame, rect: Rect) {
// 1. Basename of the currently loaded file
// 2. Keyword by which the entries are filtered at the moment
// 3. Currently selected entry and total count of entries
-pub fn render_file_info(app: &mut App, frame: &mut Frame, rect: Rect) {
+pub fn render_file_info(app: &mut App, args: &CLIArgs, frame: &mut Frame, rect: Rect) {
let block = Block::new() // can also be Block::new
// Leave Top empty to simulate one large box with borders of entry list
.borders(Borders::LEFT | Borders::RIGHT | Borders::BOTTOM)
@@ -367,10 +351,7 @@ pub fn render_file_info(app: &mut App, frame: &mut Frame, rect: Rect) {
Span::raw("File: ").bold(),
Span::raw(
// TODO: Only for testing! Need to replace with dir or files vec
- app.bibiman.main_bibfiles[0]
- .file_name()
- .unwrap()
- .to_string_lossy(),
+ args.pos_args[0].file_name().unwrap().to_string_lossy(),
)
.bold(),
])