Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some modifications to path handling and directory display #19

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
version = "0.1.2"
version = "0.1.3"
name = "bcat"
authors = ["bootjp / Yoshiaki Ueda <contact@bootjp.me>"]
edition = "2018"
Expand Down
31 changes: 25 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use filesize::PathExt;
use humansize::{file_size_opts as options, FileSize};
use prettytable::format;
use prettytable::Table;
use std::path::PathBuf;
use std::process;
use std::{fs, os::unix::prelude::CommandExt};
use std::{io::Read, os::linux::fs::MetadataExt};
Expand All @@ -20,7 +21,12 @@ const SIZE_LESS: u64 = 1024 * 10;
fn main() -> Result<()> {
let args = Cli::from_args();

let mut file = fs::File::open(&args.path)?;
let path = fs::canonicalize(&args.path).unwrap_or_else(|error| {
println!("{}", &error);
std::process::exit(1)
});

let mut file = fs::File::open(&path)?;
let metadata = file.metadata()?;

return if metadata.is_file() {
Expand All @@ -32,7 +38,7 @@ fn main() -> Result<()> {

read_file(&mut file)
} else {
list_dir(&args.path)
list_dir(&path)
};
}

Expand All @@ -45,7 +51,7 @@ fn read_file(file: &mut fs::File) -> Result<()> {

#[macro_use]
extern crate prettytable;
fn list_dir(path: &str) -> Result<()> {
fn list_dir(path: &PathBuf) -> Result<()> {
let mut table = Table::new();
table.set_titles(row![
"permission",
Expand All @@ -56,9 +62,17 @@ fn list_dir(path: &str) -> Result<()> {
"size"
]);
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
let mut paths: Vec<_> = fs::read_dir(path)?.map(|r| r.unwrap()).collect();

paths.sort_by_key(|f| {
f.file_name()
.to_os_string()
.to_string_lossy()
.to_lowercase()
});

for entry in fs::read_dir(path)? {
let path = entry?.path();
for entry in paths {
let path = entry.path();
let meta = fs::metadata(&path)?;
let uid = meta.st_uid();
let user = get_user_by_uid(uid)
Expand All @@ -75,11 +89,16 @@ fn list_dir(path: &str) -> Result<()> {
.unwrap_or_default();
let lmtime = Local.timestamp(meta.st_mtime(), 0);

let file_name = match path.file_name() {
Some(result) => result.to_string_lossy(),
None => continue,
};

table.add_row(row![
&unix_mode::to_string(stat),
&user,
&group,
&path.display().to_string(),
&file_name,
&lmtime.to_string(),
&size,
]);
Expand Down