summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2022-10-26 22:29:08 +0200
committerqkzk <qu3nt1n@gmail.com>2022-10-26 22:29:08 +0200
commitd841f73bb1d85b294708a1dced77ba5361df51bd (patch)
tree477fb6bd6ec437f0bdf095c5069beeed0d80161c
parent6f193dc517bbc43489983ddae308f8884630e6a9 (diff)
fix broken link not displayed & newdir when already exists crashlink-destination
-rw-r--r--readme.md2
-rw-r--r--src/display.rs4
-rw-r--r--src/fileinfo.rs54
-rw-r--r--src/tab.rs8
4 files changed, 42 insertions, 26 deletions
diff --git a/readme.md b/readme.md
index 6449c2e..0a30d0b 100644
--- a/readme.md
+++ b/readme.md
@@ -82,6 +82,7 @@
- [x] user defined marks ; saved and read from a file.
- [x] refactor: main should return result, have everything raise errors
- [x] stable colors per extension with caching
+- [x] BUGFIX creating an already existing dir / file crashes
## TODO
@@ -103,6 +104,7 @@
## BUGS
- [ ] when opening a file with rifle opener into nvim and closing, the terminal hangs
+- [ ] broken links aren't shown
## Sources
diff --git a/src/display.rs b/src/display.rs
index dc28066..5b9857d 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -136,11 +136,11 @@ impl Display {
.skip(tab.window.top)
{
let row = i + ContentWindow::WINDOW_MARGIN_TOP - tab.window.top;
- let mut attr = fileinfo_attr(status, &file, &self.colors);
+ let mut attr = fileinfo_attr(status, file, &self.colors);
if status.flagged.contains(&file.path) {
attr.effect |= Effect::UNDERLINE;
}
- self.term.print_with_attr(row, 0, &string, attr)?;
+ self.term.print_with_attr(row, 0, string, attr)?;
}
Ok(())
}
diff --git a/src/fileinfo.rs b/src/fileinfo.rs
index 000064d..6f2f40e 100644
--- a/src/fileinfo.rs
+++ b/src/fileinfo.rs
@@ -130,7 +130,7 @@ pub struct FileInfo {
impl FileInfo {
/// Reads every information about a file from its metadata and returs
/// a new `FileInfo` object if we can create one.
- pub fn new(direntry: &DirEntry) -> Result<FileInfo, FmError> {
+ pub fn new(direntry: &DirEntry) -> FmResult<FileInfo> {
let path = direntry.path();
let filename = extract_filename(direntry)?;
let size = extract_file_size(direntry)?;
@@ -174,18 +174,16 @@ impl FileInfo {
filename = self.filename,
);
if let FileKind::SymbolicLink = self.file_kind {
- repr = self.add_pointed_file_to_symlink(repr);
+ repr.push_str(" -> ");
+ repr.push_str(&self.read_dest().unwrap());
}
Ok(repr)
}
- fn add_pointed_file_to_symlink(&self, mut repr: String) -> String {
- if let Ok(dest_path) = &std::fs::read_link(&self.path) {
- let dest = dest_path.to_str().ok_or("Unreadable path");
- repr.push_str(&format!(" -> {}", dest.unwrap()));
- repr
- } else {
- "Broken link".to_owned()
+ fn read_dest(&self) -> Option<String> {
+ match metadata(&self.path) {
+ Ok(_) => Some(std::fs::read_link(&self.path).ok()?.to_str()?.to_owned()),
+ Err(_) => Some("Broken link".to_owned()),
}
}
@@ -393,12 +391,16 @@ fn extract_filename(direntry: &DirEntry) -> FmResult<String> {
/// Reads the permission and converts them into a string.
fn extract_permissions_string(direntry: &DirEntry) -> FmResult<String> {
- let metadata = metadata(direntry.path())?;
- let mode = metadata.mode() & 511;
- let s_o = convert_octal_mode(mode >> 6);
- let s_g = convert_octal_mode((mode >> 3) & 7);
- let s_a = convert_octal_mode(mode & 7);
- Ok([s_o, s_g, s_a].join(""))
+ match metadata(direntry.path()) {
+ Ok(metadata) => {
+ let mode = metadata.mode() & 511;
+ let s_o = convert_octal_mode(mode >> 6);
+ let s_g = convert_octal_mode((mode >> 3) & 7);
+ let s_a = convert_octal_mode(mode & 7);
+ Ok([s_o, s_g, s_a].join(""))
+ }
+ Err(_) => Ok("??????".to_owned()),
+ }
}
/// Convert an integer like `Oo777` into its string representation like `"rwx"`
@@ -409,18 +411,24 @@ fn convert_octal_mode(mode: u32) -> String {
/// Reads the owner name and returns it as a string.
fn extract_owner(direntry: &DirEntry) -> FmResult<String> {
- Ok(String::from(
- get_user_by_uid(metadata(direntry.path())?.uid())
- .ok_or_else(|| FmError::new("Couldn't read uid"))?
- .name()
- .to_str()
- .ok_or_else(|| FmError::new("Couldn't read owner name"))?,
- ))
+ match metadata(direntry.path()) {
+ Ok(metadata) => Ok(String::from(
+ get_user_by_uid(metadata.uid())
+ .ok_or_else(|| FmError::new("Couldn't read uid"))?
+ .name()
+ .to_str()
+ .ok_or_else(|| FmError::new("Couldn't read owner name"))?,
+ )),
+ Err(_) => Ok("".to_owned()),
+ }
}
/// Returns the file size.
fn extract_file_size(direntry: &DirEntry) -> Result<u64, FmError> {
- Ok(direntry.path().metadata()?.len())
+ match direntry.path().metadata() {
+ Ok(metadata) => Ok(metadata.len()),
+ Err(_) => Ok(0),
+ }
}
/// Convert a file size from bytes to human readable string.
diff --git a/src/tab.rs b/src/tab.rs
index 599729e..8eb913e 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -486,7 +486,13 @@ impl Tab {
}
pub fn exec_newdir(&mut self) -> FmResult<()> {
- fs::create_dir(self.path_content.path.join(self.input.string.clone()))?;
+ match fs::create_dir(self.path_content.path.join(self.input.string.clone())) {
+ Ok(()) => (),
+ Err(e) => match e.kind() {
+ std::io::ErrorKind::AlreadyExists => (),
+ _ => return Err(FmError::from(e)),
+ },
+ }
self.refresh_view()
}