Skip to content

Extract a symlink's target type if possible, so it can have proper colors. #563

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate
### Added
- Added support for coloring symlink targets.
- Added support for the MISSING / mi= dircolors variable for broken symlink targets.
- Add support for theme from [zwpaper](https://github.com/zwpaper) [#452](https://github.com/Peltoche/lsd/pull/452)
- Update minimal rust version to 1.42.0 from [zwpaper](https://github.com/zwpaper) [#534](https://github.com/Peltoche/lsd/issues/534)
Expand Down
34 changes: 33 additions & 1 deletion src/meta/symlink.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::Flags;
use crate::meta::{FileType, Permissions};
use std::fs::read_link;
use std::path::Path;

#[derive(Clone, Debug)]
pub struct SymLink {
target: Option<String>,
target_type: Option<FileType>,
valid: bool,
}

impl<'a> From<&'a Path> for SymLink {
fn from(path: &'a Path) -> Self {
if let Ok(target) = read_link(path) {
// Extract the symlink's target type if possible, so it can have proper colors.
let target_type = match target.metadata() {
Ok(metadata) => Some(FileType::new(
&metadata,
None,
&Permissions::from(&metadata),
)),
Err(_) => None,
};

if target.is_absolute() || path.parent() == None {
return Self {
valid: target.exists(),
Expand All @@ -21,6 +33,7 @@ impl<'a> From<&'a Path> for SymLink {
.expect("failed to convert symlink to str")
.to_string(),
),
target_type,
};
}

Expand All @@ -31,12 +44,14 @@ impl<'a> From<&'a Path> for SymLink {
.expect("failed to convert symlink to str")
.to_string(),
),
target_type,
valid: path.parent().unwrap().join(target).exists(),
};
}

Self {
target: None,
target_type: None,
valid: false,
}
}
Expand All @@ -50,7 +65,20 @@ impl SymLink {
pub fn render(&self, colors: &Colors, flag: &Flags) -> ColoredString {
if let Some(target_string) = self.symlink_string() {
let elem = if self.valid {
&Elem::SymLink
// Proper colors for symlink target file types.
match self.target_type {
Some(FileType::BlockDevice) => &Elem::BlockDevice,
Some(FileType::CharDevice) => &Elem::CharDevice,
Some(FileType::Directory { uid: _ }) => &Elem::Dir { uid: false },
Some(FileType::File { uid: _, exec: _ }) => &Elem::File {
uid: false,
exec: false,
},
Some(FileType::Pipe) => &Elem::Pipe,
Some(FileType::Socket) => &Elem::Socket,
Some(FileType::Special) => &Elem::Special,
_ => &Elem::SymLink,
}
} else {
&Elem::MissingSymLinkTarget
};
Expand Down Expand Up @@ -79,11 +107,13 @@ mod tests {
use crate::color::{Colors, ThemeOption};
use crate::config_file::Config;
use crate::flags::Flags;
use crate::meta::FileType;

#[test]
fn test_symlink_render_default_valid_target_nocolor() {
let link = SymLink {
target: Some("/target".to_string()),
target_type: None,
valid: true,
};
let argv = vec!["lsd"];
Expand All @@ -102,6 +132,7 @@ mod tests {
fn test_symlink_render_default_invalid_target_nocolor() {
let link = SymLink {
target: Some("/target".to_string()),
target_type: None,
valid: false,
};
let argv = vec!["lsd"];
Expand All @@ -120,6 +151,7 @@ mod tests {
fn test_symlink_render_default_invalid_target_withcolor() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like the test here, it checks the invalid target color, IMO, you can add some tests like this testing some of newly added cases

Copy link
Member

@zwpaper zwpaper Oct 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe It looks a little dumb by adding tests for pattern matching, but this is how we kept the functionalities working as expected during the code changes.

let link = SymLink {
target: Some("/target".to_string()),
target_type: Some(FileType::SymLink { is_dir: false }),
valid: false,
};
let argv = vec!["lsd"];
Expand Down