Skip to content

Commit

Permalink
Fix warnings and run fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorik committed Oct 13, 2024
1 parent c9b76cc commit fbbc35c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 49 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
cargo
rustc
rust-analyzer
rustfmt
];
};
packages.default = lsd;
Expand Down
32 changes: 19 additions & 13 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,26 @@ impl Core {
fn display(&self, metas: &[Meta]) {
let output = match self.flags.layout {
Layout::Tree => display::tree(
metas,
&self.flags,
&self.colors,
&self.icons,
&self.git_theme,
),
Layout::Json => display::json(metas, &self.flags, &self.colors, &self.icons, &self.git_theme),
metas,
&self.flags,
&self.colors,
&self.icons,
&self.git_theme,
),
Layout::Json => display::json(
metas,
&self.flags,
&self.colors,
&self.icons,
&self.git_theme,
),
_ => display::grid(
metas,
&self.flags,
&self.colors,
&self.icons,
&self.git_theme,
)
metas,
&self.flags,
&self.colors,
&self.icons,
&self.git_theme,
),
};

print_output!("{}", output);
Expand Down
76 changes: 42 additions & 34 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
use crate::color::{Colors, Elem, ThemeOption};
use crate::color::{Colors, Elem};
use crate::flags::blocks::Block;
use crate::flags::{Display, Flags, HyperlinkOption, IconOption, IconTheme, Layout};
use crate::flags::{Display, Flags, HyperlinkOption, Layout};
use crate::git_theme::GitTheme;
use crate::icon::Icons;
use crate::meta::name::DisplayOption;
use crate::meta::{Date, FileType, Meta, OwnerCache};
use std::collections::HashMap;
use chrono::{DateTime, Local};
use serde::Serialize;
use std::collections::HashMap;
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::terminal_size;
use unicode_width::UnicodeWidthStr;
use url::Url;
use users::UsersCache;

const EDGE: &str = "\u{251c}\u{2500}\u{2500}"; // "├──"
const LINE: &str = "\u{2502} "; // "│ "
const CORNER: &str = "\u{2514}\u{2500}\u{2500}"; // "└──"
const BLANK: &str = " ";

fn make_clickable_link(
full_path: String,
link_name: String,
) -> String {
// uri's based on this https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda

format!(
"\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\",
match Url::from_file_path(full_path.clone()) {
Ok(url) => url.to_string(),
Err(_) => full_path.clone(),
},
link_name
)
}

#[derive(Serialize)]
struct JsonMeta {
name: String,
Expand All @@ -49,22 +31,46 @@ struct JsonMeta {
size: Option<u64>,
user: Option<String>,
group: Option<String>,
path: String
path: String,
}

impl JsonMeta {
fn from_meta(value: &Meta, icons: &Icons, colors: &Colors, flags: &Flags) -> JsonMeta {
let name = &value.name;
let icon = icons.get(&name);
let display = name.render(colors, icons, &DisplayOption::FileName, HyperlinkOption::Auto, false);
let permissions = value.permissions_or_attributes.as_ref().unwrap().render(colors, flags).to_string();
let display = name.render(
colors,
icons,
&DisplayOption::FileName,
HyperlinkOption::Auto,
false,
);
let permissions = value
.permissions_or_attributes
.as_ref()
.unwrap()
.render(colors, flags)
.to_string();
let size = value.size.as_ref().map(|size| size.get_bytes());
let user = value.owner.as_ref().map(|owner| owner.render_user(colors, &OwnerCache::default(), flags).to_string());
let group = value.owner.as_ref().map(|owner| owner.render_group(colors, &OwnerCache::default(), flags).to_string());
let user = value.owner.as_ref().map(|owner| {
owner
.render_user(colors, &OwnerCache::default(), flags)
.to_string()
});
let group = value.owner.as_ref().map(|owner| {
owner
.render_group(colors, &OwnerCache::default(), flags)
.to_string()
});
let path = value.path.to_str().unwrap().to_string();

JsonMeta {
content: value.content.as_ref().map(|content| content.iter().map(|meta| JsonMeta::from_meta(meta, icons, colors, flags)).collect()),
content: value.content.as_ref().map(|content| {
content
.iter()
.map(|meta| JsonMeta::from_meta(meta, icons, colors, flags))
.collect()
}),
name: name.name.clone(),
display: display.to_string(),
r#type: name.file_type().render(colors).to_string(),
Expand All @@ -79,7 +85,7 @@ impl JsonMeta {
size,
user,
group,
path
path,
}
}
}
Expand All @@ -89,13 +95,15 @@ pub fn json(
flags: &Flags,
colors: &Colors,
icons: &Icons,
git_theme: &GitTheme,
_git_theme: &GitTheme,
) -> String {
serde_json::to_string(&metas
.into_iter()
.map(|meta| JsonMeta::from_meta(meta, icons, colors, flags))
.collect::<Vec<JsonMeta>>()
).unwrap()
serde_json::to_string(
&metas
.into_iter()
.map(|meta| JsonMeta::from_meta(meta, icons, colors, flags))
.collect::<Vec<JsonMeta>>(),
)
.unwrap()
}

pub fn grid(
Expand Down
2 changes: 1 addition & 1 deletion src/flags/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Layout {
Grid,
Tree,
OneLine,
Json
Json,
}

impl Configurable<Layout> for Layout {
Expand Down
2 changes: 1 addition & 1 deletion src/meta/owner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::color::{ColoredString, Colors, Elem};
use crate::Flags;
use serde::Serialize;
#[cfg(unix)]
use std::fs::Metadata;
use serde::Serialize;
#[cfg(unix)]
use users::{Groups, Users, UsersCache};

Expand Down

0 comments on commit fbbc35c

Please sign in to comment.