aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/backend/bib.rs29
-rw-r--r--src/frontend/tui.rs71
2 files changed, 33 insertions, 67 deletions
diff --git a/src/backend/bib.rs b/src/backend/bib.rs
index bfc959d..fecb548 100644
--- a/src/backend/bib.rs
+++ b/src/backend/bib.rs
@@ -19,11 +19,18 @@ use biblatex::{self, Bibliography};
use biblatex::{ChunksExt, Type};
use std::{fs, path::PathBuf};
+#[derive(Debug)]
+pub enum FileFormat {
+ BibLatex,
+ Hayagriva,
+}
+
// Set necessary fields
// TODO: can surely be made more efficient/simpler
#[derive(Debug)]
pub struct BibiMain {
pub bibfile: PathBuf, // path to bibfile
+ pub bibfile_format: FileFormat, // Format of passed file
pub bibfilestring: String, // content of bibfile as string
pub bibliography: Bibliography, // parsed bibliography
pub citekeys: Vec<String>, // list of all citekeys
@@ -33,6 +40,7 @@ pub struct BibiMain {
impl BibiMain {
pub fn new(main_bibfile: PathBuf) -> Self {
// TODO: Needs check for config file path as soon as config file is impl
+ let bibfile_format = Self::check_file_format(&main_bibfile);
let bibfile = main_bibfile;
let bibfilestring = fs::read_to_string(&bibfile).unwrap();
let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap();
@@ -40,6 +48,7 @@ impl BibiMain {
let keyword_list = Self::collect_tag_list(&citekeys, &bibliography);
Self {
bibfile,
+ bibfile_format,
bibfilestring,
bibliography,
citekeys,
@@ -47,13 +56,27 @@ impl BibiMain {
}
}
+ // Check which file format the passed file has
+ fn check_file_format(main_bibfile: &PathBuf) -> FileFormat {
+ let extension = main_bibfile.extension().unwrap().to_str();
+
+ match extension {
+ Some("yml") => FileFormat::Hayagriva,
+ Some("yaml") => FileFormat::Hayagriva,
+ Some("bib") => FileFormat::BibLatex,
+ Some(_) => panic!("The extension {:?} is no valid bibfile", extension.unwrap()),
+ None => panic!("The given path {:?} holds no valid file", main_bibfile),
+ }
+ }
+
// get list of citekeys from the given bibfile
// this list is the base for further operations on the bibentries
// since it is the entry point of the biblatex crate.
pub fn get_citekeys(bibstring: &Bibliography) -> Vec<String> {
- let mut citekeys: Vec<String> =
- bibstring.iter().map(|entry| entry.to_owned().key).collect();
- citekeys.sort_by_key(|name| name.to_lowercase());
+ let citekeys: Vec<String> = bibstring.keys().map(|k| k.to_owned()).collect();
+ // let mut citekeys: Vec<String> =
+ // bibstring.iter().map(|entry| entry.to_owned().key).collect();
+ // citekeys.sort_by_key(|name| name.to_lowercase());
citekeys
}
diff --git a/src/frontend/tui.rs b/src/frontend/tui.rs
index 698407d..e3c9c1a 100644
--- a/src/frontend/tui.rs
+++ b/src/frontend/tui.rs
@@ -24,6 +24,8 @@ use crossterm::{
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
};
// use ratatui::backend::{Backend, CrosstermBackend};
+use color_eyre::eyre::{OptionExt, Result};
+use futures::{FutureExt, StreamExt};
use ratatui::backend::CrosstermBackend as Backend;
use std::io::{stdout, Stdout};
use std::panic;
@@ -31,10 +33,6 @@ use std::{
ops::{Deref, DerefMut},
time::Duration,
};
-
-use color_eyre::config::HookBuilder;
-use color_eyre::eyre::{OptionExt, Result};
-use futures::{FutureExt, StreamExt};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
@@ -51,14 +49,6 @@ pub enum Event {
Resize(u16, u16),
}
-// pub type IO = std::io::{{crossterm_io | title_case}};
-// pub fn io() -> IO {
-// std::io::{{crossterm_io}}()
-// }
-/// Representation of a terminal user interface.
-///
-/// It is responsible for setting up the terminal,
-/// initializing the interface and handling the draw events.
#[derive(Debug)]
pub struct Tui {
/// Interface to the Terminal.
@@ -73,7 +63,7 @@ pub struct Tui {
}
impl Tui {
- /// Constructs a new instance of [`Tui`].
+ // Constructs a new instance of [`Tui`].
pub fn new() -> Result<Self> {
let terminal = ratatui::Terminal::new(Backend::new(stdout()))?;
let (sender, receiver) = mpsc::unbounded_channel();
@@ -150,26 +140,6 @@ impl Tui {
cancellation_token.cancel();
}
- /// Initializes the terminal interface.
- ///
- /// It enables the raw mode and sets terminal properties.
- // pub fn init(&mut self) -> Result<()> {
- // terminal::enable_raw_mode()?;
- // crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
-
- // // Define a custom panic hook to reset the terminal properties.
- // // This way, you won't have your terminal messed up if an unexpected error happens.
- // let panic_hook = panic::take_hook();
- // panic::set_hook(Box::new(move |panic| {
- // Self::reset().expect("failed to reset the terminal");
- // panic_hook(panic);
- // }));
-
- // self.terminal.hide_cursor()?;
- // self.terminal.clear()?;
- // Ok(())
- // }
-
pub fn enter(&mut self) -> Result<()> {
crossterm::terminal::enable_raw_mode()?;
crossterm::execute!(stdout(), EnterAlternateScreen, cursor::Hide)?;
@@ -216,10 +186,10 @@ impl Tui {
Ok(())
}
- /// [`Draw`] the terminal interface by [`rendering`] the widgets.
- ///
- /// [`Draw`]: ratatui::Terminal::draw
- /// [`rendering`]: crate::ui::render
+ // [`Draw`] the terminal interface by [`rendering`] the widgets.
+ //
+ // [`Draw`]: ratatui::Terminal::draw
+ // [`rendering`]: crate::ui::render
pub fn draw(&mut self, app: &mut App) -> Result<()> {
// self.terminal.draw(|frame| ui::render(app, frame))?;
self.terminal
@@ -230,33 +200,6 @@ impl Tui {
pub async fn next(&mut self) -> Result<Event> {
self.receiver.recv().await.ok_or_eyre("This is an IO error")
}
-
- pub fn init_error_hooks() -> Result<()> {
- let (panic, error) = HookBuilder::default().into_hooks();
- let panic = panic.into_panic_hook();
- let error = error.into_eyre_hook();
- color_eyre::eyre::set_hook(Box::new(move |e| {
- let _ = crossterm::execute!(
- stdout(),
- DisableMouseCapture,
- LeaveAlternateScreen,
- cursor::Show
- );
- let _ = crossterm::terminal::disable_raw_mode();
- error(e)
- }))?;
- std::panic::set_hook(Box::new(move |info| {
- let _ = crossterm::execute!(
- stdout(),
- DisableMouseCapture,
- LeaveAlternateScreen,
- cursor::Show
- );
- let _ = crossterm::terminal::disable_raw_mode();
- panic(info)
- }));
- Ok(())
- }
}
impl Deref for Tui {