Skip to content

Commit

Permalink
feat: add anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
taybart committed Mar 6, 2024
1 parent c0eb7fb commit de1bbd9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 57 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crossterm = "0.26"
tui = { package = "ratatui", version = "0.22.0", features = ["all-widgets"]}
shellexpand = "3.0.0"
cfg-if = "1.0.0"
anyhow = "1.0.76"
2 changes: 1 addition & 1 deletion src/fm/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Command {
}

if let Err(e) = tree.cwd().refresh() {
log::error(e);
log::error(e.to_string())
}
}
_ => {
Expand Down
74 changes: 34 additions & 40 deletions src/fm/dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::finder::match_and_score;
use crate::log;

use anyhow::{Context, Result};

use super::file::File;

use std::fs::{self, canonicalize, read_dir, read_link};
Expand All @@ -24,57 +26,49 @@ pub struct Dir {
}

impl Dir {
pub fn new(dir_name: PathBuf) -> Result<Dir, String> {
pub fn new(dir_name: PathBuf) -> Result<Dir> {
let mut files = Vec::new();
match read_dir(&dir_name) {
Ok(dir) => {
for entry in dir {
match File::new(entry.unwrap()) {
Ok(file) => {
files.push(file);
}
Err(e) => log::error(e),
}
let dir = read_dir(&dir_name).context(format!("could not read dir {dir_name:?}"))?;
for entry in dir {
match File::new(entry.unwrap()) {
Ok(file) => {
files.push(file);
}
Err(e) => log::error(e),
}
}

files.sort_by(|a, b| a.name.cmp(&b.name));
files.sort_by(|a, b| a.name.cmp(&b.name));

let path = canonicalize(&dir_name).expect("could not canonicalize directory");
let path = canonicalize(&dir_name).expect("could not canonicalize directory");

let mut state = ListState::default();
state.select(Some(0));
Ok(Dir {
state,
visible_files: files.clone(),
last_selection: 0,
files,
path,
})
}
Err(e) => Err(format!("could not read dir {dir_name:?}: {e}")),
}
let mut state = ListState::default();
state.select(Some(0));
Ok(Dir {
state,
visible_files: files.clone(),
last_selection: 0,
files,
path,
})
}

pub fn refresh(&mut self) -> Result<(), String> {
pub fn refresh(&mut self) -> Result<()> {
let mut files = Vec::new();
match read_dir(&self.path) {
Ok(dir) => {
for entry in dir {
match File::new(entry.unwrap()) {
Ok(file) => {
files.push(file);
}
Err(e) => log::error(e),
}
let dir = read_dir(&self.path).context("could not refresh dir")?;
for entry in dir {
match File::new(entry.unwrap()) {
Ok(file) => {
files.push(file);
}

files.sort_by(|a, b| a.name.cmp(&b.name));

self.files = files;
Ok(())
Err(e) => log::error(e),
}
Err(e) => Err(format!("could not refresh dir: {e}")),
}

files.sort_by(|a, b| a.name.cmp(&b.name));

self.files = files;
Ok(())
}

pub fn rename_selected(&mut self, new_name: &str, show_hidden: bool) {
Expand Down
3 changes: 2 additions & 1 deletion src/fm/fm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use crossterm::event::{self, Event, KeyEvent};
use std::io;

Expand All @@ -22,7 +23,7 @@ pub struct FM {
}

impl FM {
pub fn new() -> Result<FM, String> {
pub fn new() -> Result<FM> {
Ok(FM {
tree: Tree::new()?,
state: State::default(),
Expand Down
4 changes: 3 additions & 1 deletion src/fm/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use tui::{
Frame, Terminal,
};

use anyhow::Result;

use super::{dir::Dir, fm::FM, state::Mode, tree::Tree};

pub fn setup_tui() -> io::Result<Terminal<CrosstermBackend<Stdout>>> {
Expand All @@ -34,7 +36,7 @@ pub fn teardown_tui(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> io::Re
Ok(())
}

pub fn render<B: Backend>(fm: &mut FM, f: &mut Frame<B>) -> Result<(), String> {
pub fn render<B: Backend>(fm: &mut FM, f: &mut Frame<B>) -> Result<()> {
let hide_parent = fm.state.hide_parent;
// TODO: add to config
if f.size().width < 80 {
Expand Down
12 changes: 7 additions & 5 deletions src/fm/tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::{Context, Result};

use super::dir::Dir;
use super::state::State;
use std::{
Expand All @@ -11,7 +13,7 @@ pub struct Tree {
}

impl Tree {
pub fn new() -> Result<Tree, String> {
pub fn new() -> Result<Tree> {
let parent = Dir::new(Tree::parent_path()?)?;
let cwd = Dir::new(Tree::cwd_path()?)?;

Expand All @@ -22,11 +24,11 @@ impl Tree {
Ok(Tree { fs })
}

pub fn parent_path() -> Result<PathBuf, String> {
fs::canonicalize(Path::new("..")).map_err(|e| format!("could not get parent path {e}"))
pub fn parent_path() -> Result<PathBuf> {
fs::canonicalize(Path::new("..")).context("could not get parent path")
}
pub fn cwd_path() -> Result<PathBuf, String> {
fs::canonicalize(Path::new(".")).map_err(|e| format!("could not get cwd path {e}"))
pub fn cwd_path() -> Result<PathBuf> {
fs::canonicalize(Path::new(".")).context("could not get cwd path")
}

pub fn parent(&mut self) -> &mut Dir {
Expand Down
12 changes: 3 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use fm::FM;

use std::error::Error;
use anyhow::Result;

mod finder;
mod fm;
mod log;

fn main() -> Result<(), Box<dyn Error>> {
let mut fm = FM::new()?;

let res = fm.run();

if let Err(err) = res {
println!("{:?}", err)
}
fn main() -> Result<()> {
FM::new()?.run()?;
Ok(())
}

0 comments on commit de1bbd9

Please sign in to comment.