Skip to content
Myr

dir_entry.rs

This file

Path

src/dir_entry.rs

155 lines · 4.0 KiB · Rust

Last changed by

Merge pull request #1510 from garlic-hub/clippyTavian Barnesbefore adoption4efc05e

History of this file

Take it

The walk stopped after 200 commits: lines older than 5376676 point at it.

Merge pull request #1333 from tmccombs/rust-170Thayne McCombsbefore adoptiona0370aa

line 1why, one step back
1use std::cell::OnceCell;

dir_entry: Implement lscolors::ColorableTavian Barnesbefore adoption7917c00

lines 2–4why, one step back
2use std::ffi::OsString;
3use std::fs::{FileType, Metadata};4use std::path::{Path, PathBuf};

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

line 5why, one step back
5

Compute colors in parallelTavian Barnesbefore adoption17d849d

line 6why, one step back
6use lscolors::{Colorable, LsColors, Style};

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

line 7why, one step back
7

Make --strip-cwd-prefix apply to -x/-XTavian Barnesbefore adoption4ffc349

lines 8–10why, one step back
8use crate::config::Config;
9use crate::filesystem::strip_current_dir;10

Merge pull request #1422 from tavianator/batchTavian Barnesbefore adoption84f032e

line 11why, one step back
11#[derive(Debug)]

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 12–16why, one step back
12enum DirEntryInner {
13 Normal(ignore::DirEntry),14 BrokenSymlink(PathBuf),15}16

Merge pull request #1422 from tavianator/batchTavian Barnesbefore adoption84f032e

line 17why, one step back
17#[derive(Debug)]

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 18–20why, one step back
18pub struct DirEntry {
19 inner: DirEntryInner,20 metadata: OnceCell<Option<Metadata>>,

Compute colors in parallelTavian Barnesbefore adoption17d849d

line 21why, one step back
21 style: OnceCell<Option<Style>>,

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 22–29why, one step back
22}
2324impl DirEntry {25 #[inline]26 pub fn normal(e: ignore::DirEntry) -> Self {27 Self {28 inner: DirEntryInner::Normal(e),29 metadata: OnceCell::new(),

Compute colors in parallelTavian Barnesbefore adoption17d849d

line 30why, one step back
30 style: OnceCell::new(),

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 31–37why, one step back
31 }
32 }3334 pub fn broken_symlink(path: PathBuf) -> Self {35 Self {36 inner: DirEntryInner::BrokenSymlink(path),37 metadata: OnceCell::new(),

Compute colors in parallelTavian Barnesbefore adoption17d849d

line 38why, one step back
38 style: OnceCell::new(),

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 39–52why, one step back
39 }
40 }4142 pub fn path(&self) -> &Path {43 match &self.inner {44 DirEntryInner::Normal(e) => e.path(),45 DirEntryInner::BrokenSymlink(pathbuf) => pathbuf.as_path(),46 }47 }4849 pub fn into_path(self) -> PathBuf {50 match self.inner {51 DirEntryInner::Normal(e) => e.into_path(),52 DirEntryInner::BrokenSymlink(p) => p,

Make --strip-cwd-prefix apply to -x/-XTavian Barnesbefore adoption4ffc349

lines 53–70why, one step back
53 }
54 }5556 /// Returns the path as it should be presented to the user.57 pub fn stripped_path(&self, config: &Config) -> &Path {58 if config.strip_cwd_prefix {59 strip_current_dir(self.path())60 } else {61 self.path()62 }63 }6465 /// Returns the path as it should be presented to the user.66 pub fn into_stripped_path(self, config: &Config) -> PathBuf {67 if config.strip_cwd_prefix {68 self.stripped_path(config).to_path_buf()69 } else {70 self.into_path()

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 71–94why, one step back
71 }
72 }7374 pub fn file_type(&self) -> Option<FileType> {75 match &self.inner {76 DirEntryInner::Normal(e) => e.file_type(),77 DirEntryInner::BrokenSymlink(_) => self.metadata().map(|m| m.file_type()),78 }79 }8081 pub fn metadata(&self) -> Option<&Metadata> {82 self.metadata83 .get_or_init(|| match &self.inner {84 DirEntryInner::Normal(e) => e.metadata().ok(),85 DirEntryInner::BrokenSymlink(path) => path.symlink_metadata().ok(),86 })87 .as_ref()88 }8990 pub fn depth(&self) -> Option<usize> {91 match &self.inner {92 DirEntryInner::Normal(e) => Some(e.depth()),93 DirEntryInner::BrokenSymlink(_) => None,94 }

Compute colors in parallelTavian Barnesbefore adoption17d849d

lines 95–100why, one step back
95 }
9697 pub fn style(&self, ls_colors: &LsColors) -> Option<&Style> {98 self.style99 .get_or_init(|| ls_colors.style_for(self).cloned())100 .as_ref()

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 101–109why, one step back
101 }
102}103104impl PartialEq for DirEntry {105 #[inline]106 fn eq(&self, other: &Self) -> bool {107 self.path() == other.path()108 }109}

dir_entry: Implement lscolors::ColorableTavian Barnesbefore adoption7917c00

line 110why, one step back
110

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 111–115why, one step back
111impl Eq for DirEntry {}
112113impl PartialOrd for DirEntry {114 #[inline]115 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {

Merge pull request #1510 from garlic-hub/clippyTavian Barnesbefore adoption4efc05e

line 116why, one step back
116 Some(self.cmp(other))

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 117–123why, one step back
117 }
118}119120impl Ord for DirEntry {121 #[inline]122 fn cmp(&self, other: &Self) -> std::cmp::Ordering {123 self.path().cmp(other.path())

dir_entry: Implement lscolors::ColorableTavian Barnesbefore adoption7917c00

lines 124–134why, one step back
124 }
125}126127impl Colorable for DirEntry {128 fn path(&self) -> PathBuf {129 self.path().to_owned()130 }131132 fn file_name(&self) -> OsString {133 let name = match &self.inner {134 DirEntryInner::Normal(e) => e.file_name(),

dir_entry: Implement file_name() for broken symlinksTavian Barnesbefore adoptionb2c8888

lines 135–143why, one step back
135 DirEntryInner::BrokenSymlink(path) => {
136 // Path::file_name() only works if the last component is Normal,137 // but we want it for all component types, so we open code it.138 // Copied from LsColors::style_for_path_with_metadata().139 path.components()140 .last()141 .map(|c| c.as_os_str())142 .unwrap_or_else(|| path.as_os_str())143 }

dir_entry: Implement lscolors::ColorableTavian Barnesbefore adoption7917c00

lines 144–153why, one step back
144 };
145 name.to_owned()146 }147148 fn file_type(&self) -> Option<FileType> {149 self.file_type()150 }151152 fn metadata(&self) -> Option<Metadata> {153 self.metadata().cloned()

Merge pull request #1125 from sharkdp/dependabot/cargo/anyhow-1.0.65Thayne McCombsbefore adoption5376676

lines 154–155why, one step back
154 }
155}