Skip to content

Commit 6522337

Browse files
authored
Merge pull request ogham#114 from Lindenk/unix_types
Exa now recognizes pipes, devices, and sockets on unix systems
2 parents b819167 + a9bb275 commit 6522337

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

src/fs/fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub type uid_t = u32;
4141
/// file’s contents. So “link” is a type, but “image” is just a type of
4242
/// regular file. (See the `filetype` module for those checks.)
4343
pub enum Type {
44-
File, Directory, Pipe, Link, Special,
44+
File, Directory, Pipe, Link, Socket, CharDevice, BlockDevice, Special,
4545
}
4646

4747
impl Type {

src/fs/file.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::path::{Path, PathBuf};
1010
use fs::dir::Dir;
1111
use fs::fields as f;
1212

13+
#[cfg(any(target_os = "macos", target_os = "linux"))]
14+
use std::os::unix::fs::FileTypeExt;
1315

1416
/// Constant table copied from https://doc.rust-lang.org/src/std/sys/unix/ext/fs.rs.html#11-259
1517
/// which is currently unstable and lacks vision for stabilization,
@@ -145,12 +147,7 @@ impl<'dir> File<'dir> {
145147
/// Whether this file is a symlink on the filesystem.
146148
pub fn is_link(&self) -> bool {
147149
self.metadata.file_type().is_symlink()
148-
}
149-
150-
/// Whether this file is a named pipe on the filesystem.
151-
pub fn is_pipe(&self) -> bool {
152-
false // TODO: Still waiting on this one...
153-
}
150+
}
154151

155152
/// Whether this file is a dotfile, based on its name. In Unix, file names
156153
/// beginning with a dot represent system or configuration files, and
@@ -283,6 +280,15 @@ impl<'dir> File<'dir> {
283280
else if self.is_link() {
284281
f::Type::Link
285282
}
283+
else if self.is_char_device() {
284+
f::Type::CharDevice
285+
}
286+
else if self.is_block_device() {
287+
f::Type::BlockDevice
288+
}
289+
else if self.is_socket() {
290+
f::Type::Socket
291+
}
286292
else {
287293
f::Type::Special
288294
}
@@ -347,6 +353,53 @@ impl<'dir> File<'dir> {
347353
}
348354
}
349355

356+
#[cfg(any(target_os = "macos", target_os = "linux"))]
357+
impl<'dir> File<'dir> {
358+
/// Whether this file is a named pipe on the filesystem.
359+
pub fn is_pipe(&self) -> bool {
360+
self.metadata.file_type().is_fifo()
361+
}
362+
363+
/// Whether this file is a char device on the filesystem.
364+
pub fn is_char_device(&self) -> bool {
365+
self.metadata.file_type().is_char_device()
366+
}
367+
368+
/// Whether this file is a block device on the filesystem.
369+
pub fn is_block_device(&self) -> bool {
370+
self.metadata.file_type().is_block_device()
371+
}
372+
373+
/// Whether this file is a socket on the filesystem.
374+
pub fn is_socket(&self) -> bool {
375+
self.metadata.file_type().is_socket()
376+
}
377+
}
378+
379+
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
380+
impl<'dir> File<'dir> {
381+
/// Whether this file is a named pipe on the filesystem.
382+
pub fn is_pipe(&self) -> bool {
383+
false
384+
}
385+
386+
/// Whether this file is a char device on the filesystem.
387+
pub fn is_char_device(&self) -> bool {
388+
false
389+
}
390+
391+
/// Whether this file is a block device on the filesystem.
392+
pub fn is_block_device(&self) -> bool {
393+
false
394+
}
395+
396+
/// Whether this file is a socket on the filesystem.
397+
pub fn is_socket(&self) -> bool {
398+
false
399+
}
400+
}
401+
402+
350403
impl<'a> AsRef<File<'a>> for File<'a> {
351404
fn as_ref(&self) -> &File<'a> {
352405
&self

src/output/colours.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub struct FileTypes {
2727
pub normal: Style,
2828
pub directory: Style,
2929
pub symlink: Style,
30+
pub pipe: Style,
31+
pub device: Style,
32+
pub socket: Style,
3033
pub special: Style,
3134
pub executable: Style,
3235
pub image: Style,
@@ -99,6 +102,9 @@ impl Colours {
99102
normal: Style::default(),
100103
directory: Blue.bold(),
101104
symlink: Cyan.normal(),
105+
pipe: Yellow.normal(),
106+
device: Yellow.bold(),
107+
socket: Red.bold(),
102108
special: Yellow.normal(),
103109
executable: Green.bold(),
104110
image: Fixed(133).normal(),

src/output/details.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,11 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
515515
let type_char = match file_type {
516516
f::Type::File => types.normal.paint("."),
517517
f::Type::Directory => types.directory.paint("d"),
518-
f::Type::Pipe => types.special.paint("|"),
518+
f::Type::Pipe => types.pipe.paint("|"),
519519
f::Type::Link => types.symlink.paint("l"),
520+
f::Type::CharDevice => types.device.paint("c"),
521+
f::Type::BlockDevice => types.device.paint("b"),
522+
f::Type::Socket => types.socket.paint("s"),
520523
f::Type::Special => types.special.paint("?"),
521524
};
522525

src/output/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ pub fn file_colour(colours: &Colours, file: &File) -> Style {
8484
f if f.is_directory() => colours.filetypes.directory,
8585
f if f.is_executable_file() => colours.filetypes.executable,
8686
f if f.is_link() => colours.filetypes.symlink,
87-
f if !f.is_file() => colours.filetypes.special,
87+
f if f.is_pipe() => colours.filetypes.pipe,
88+
f if f.is_char_device()
89+
| f.is_block_device() => colours.filetypes.device,
90+
f if f.is_socket() => colours.filetypes.socket,
91+
f if !f.is_file() => colours.filetypes.special,
8892
f if f.is_immediate() => colours.filetypes.immediate,
8993
f if f.is_image() => colours.filetypes.image,
9094
f if f.is_video() => colours.filetypes.video,

0 commit comments

Comments
 (0)