Browse Source

Give block and character devices different colours

There are now two device colours instead of one. Even though they’re both set to the same style for the default colour set, LS_COLORS allows the two to look different, so exa has to support it too.

It’s probably a good idea to support it anyway.
Benjamin Sago 8 years ago
parent
commit
276d18cf7c
4 changed files with 101 additions and 97 deletions
  1. 20 20
      src/options/colours.rs
  2. 75 72
      src/output/colours.rs
  3. 2 2
      src/output/file_name.rs
  4. 4 3
      src/output/render/filetype.rs

+ 20 - 20
src/options/colours.rs

@@ -77,16 +77,16 @@ impl Colours {
             let lsc = lsc.to_string_lossy();
             let lsc = LSColors::parse(lsc.as_ref());
 
-            if let Some(c) = lsc.get("di") { colours.filekinds.directory  = c; }
-            if let Some(c) = lsc.get("ex") { colours.filekinds.executable = c; }
-            if let Some(c) = lsc.get("fi") { colours.filekinds.normal     = c; }
-            if let Some(c) = lsc.get("pi") { colours.filekinds.pipe       = c; }
-            if let Some(c) = lsc.get("so") { colours.filekinds.socket     = c; }
-            if let Some(c) = lsc.get("bd") { colours.filekinds.device     = c; }
-            if let Some(c) = lsc.get("cd") { colours.filekinds.device     = c; }
-            if let Some(c) = lsc.get("ln") { colours.filekinds.symlink    = c; }
-            if let Some(c) = lsc.get("or") { colours.broken_arrow         = c; }
-            if let Some(c) = lsc.get("mi") { colours.broken_filename      = c; }
+            if let Some(c) = lsc.get("di") { colours.filekinds.directory    = c; }
+            if let Some(c) = lsc.get("ex") { colours.filekinds.executable   = c; }
+            if let Some(c) = lsc.get("fi") { colours.filekinds.normal       = c; }
+            if let Some(c) = lsc.get("pi") { colours.filekinds.pipe         = c; }
+            if let Some(c) = lsc.get("so") { colours.filekinds.socket       = c; }
+            if let Some(c) = lsc.get("bd") { colours.filekinds.block_device = c; }
+            if let Some(c) = lsc.get("cd") { colours.filekinds.char_device  = c; }
+            if let Some(c) = lsc.get("ln") { colours.filekinds.symlink      = c; }
+            if let Some(c) = lsc.get("or") { colours.broken_arrow           = c; }
+            if let Some(c) = lsc.get("mi") { colours.broken_filename        = c; }
         }
 
         Ok(colours)
@@ -277,14 +277,14 @@ mod customs_test {
         }
     }
 
-    test!(ls_di:  ls "di=31", exa ""  =>  |c: &mut Colours| { c.filekinds.directory  = Red.normal();    });  // Directory
-    test!(ls_ex:  ls "ex=32", exa ""  =>  |c: &mut Colours| { c.filekinds.executable = Green.normal();  });  // Executable file
-    test!(ls_fi:  ls "fi=33", exa ""  =>  |c: &mut Colours| { c.filekinds.normal     = Yellow.normal(); });  // Regular file
-    test!(ls_pi:  ls "pi=34", exa ""  =>  |c: &mut Colours| { c.filekinds.pipe       = Blue.normal();   });  // FIFO
-    test!(ls_so:  ls "so=35", exa ""  =>  |c: &mut Colours| { c.filekinds.socket     = Purple.normal(); });  // Socket
-    test!(ls_bd:  ls "bd=36", exa ""  =>  |c: &mut Colours| { c.filekinds.device     = Cyan.normal();   });  // Block device
-    test!(ls_cd:  ls "cd=35", exa ""  =>  |c: &mut Colours| { c.filekinds.device     = Purple.normal(); });  // Character device
-    test!(ls_ln:  ls "ln=34", exa ""  =>  |c: &mut Colours| { c.filekinds.symlink    = Blue.normal();   });  // Symlink
-    test!(ls_or:  ls "or=33", exa ""  =>  |c: &mut Colours| { c.broken_arrow         = Yellow.normal(); });  // Broken link
-    test!(ls_mi:  ls "mi=32", exa ""  =>  |c: &mut Colours| { c.broken_filename      = Green.normal();  });  // Broken link target
+    test!(ls_di:  ls "di=31", exa ""  =>  |c: &mut Colours| { c.filekinds.directory    = Red.normal();    });  // Directory
+    test!(ls_ex:  ls "ex=32", exa ""  =>  |c: &mut Colours| { c.filekinds.executable   = Green.normal();  });  // Executable file
+    test!(ls_fi:  ls "fi=33", exa ""  =>  |c: &mut Colours| { c.filekinds.normal       = Yellow.normal(); });  // Regular file
+    test!(ls_pi:  ls "pi=34", exa ""  =>  |c: &mut Colours| { c.filekinds.pipe         = Blue.normal();   });  // FIFO
+    test!(ls_so:  ls "so=35", exa ""  =>  |c: &mut Colours| { c.filekinds.socket       = Purple.normal(); });  // Socket
+    test!(ls_bd:  ls "bd=36", exa ""  =>  |c: &mut Colours| { c.filekinds.block_device = Cyan.normal();   });  // Block device
+    test!(ls_cd:  ls "cd=35", exa ""  =>  |c: &mut Colours| { c.filekinds.char_device  = Purple.normal(); });  // Character device
+    test!(ls_ln:  ls "ln=34", exa ""  =>  |c: &mut Colours| { c.filekinds.symlink      = Blue.normal();   });  // Symlink
+    test!(ls_or:  ls "or=33", exa ""  =>  |c: &mut Colours| { c.broken_arrow           = Yellow.normal(); });  // Broken link
+    test!(ls_mi:  ls "mi=32", exa ""  =>  |c: &mut Colours| { c.broken_filename        = Green.normal();  });  // Broken link target
 }

+ 75 - 72
src/output/colours.rs

@@ -35,7 +35,8 @@ pub struct FileKinds {
     pub directory: Style,
     pub symlink: Style,
     pub pipe: Style,
-    pub device: Style,
+    pub block_device: Style,
+    pub char_device: Style,
     pub socket: Style,
     pub special: Style,
     pub executable: Style,
@@ -125,14 +126,15 @@ impl Colours {
             scale: scale,
 
             filekinds: FileKinds {
-                normal:      Style::default(),
-                directory:   Blue.bold(),
-                symlink:     Cyan.normal(),
-                pipe:        Yellow.normal(),
-                device:      Yellow.bold(),
-                socket:      Red.bold(),
-                special:     Yellow.normal(),
-                executable:  Green.bold(),
+                normal:       Style::default(),
+                directory:    Blue.bold(),
+                symlink:      Cyan.normal(),
+                pipe:         Yellow.normal(),
+                block_device: Yellow.bold(),
+                char_device:  Yellow.bold(),
+                socket:       Red.bold(),
+                special:      Yellow.normal(),
+                executable:   Green.bold(),
             },
 
             filetypes: FileTypes {
@@ -218,89 +220,90 @@ impl Colours {
 
 
 impl render::BlocksColours for Colours {
-	fn block_count(&self)  -> Style { self.blocks }
-	fn no_blocks(&self)    -> Style { self.punctuation }
+    fn block_count(&self)  -> Style { self.blocks }
+    fn no_blocks(&self)    -> Style { self.punctuation }
 }
 
 impl render::FiletypeColours for Colours {
-	fn normal(&self)     -> Style { self.filekinds.normal }
-	fn directory(&self)  -> Style { self.filekinds.directory }
-	fn pipe(&self)       -> Style { self.filekinds.pipe }
-	fn symlink(&self)    -> Style { self.filekinds.symlink }
-	fn device(&self)     -> Style { self.filekinds.device }
-	fn socket(&self)     -> Style { self.filekinds.socket }
-	fn special(&self)    -> Style { self.filekinds.special }
+    fn normal(&self)       -> Style { self.filekinds.normal }
+    fn directory(&self)    -> Style { self.filekinds.directory }
+    fn pipe(&self)         -> Style { self.filekinds.pipe }
+    fn symlink(&self)      -> Style { self.filekinds.symlink }
+    fn block_device(&self) -> Style { self.filekinds.block_device }
+    fn char_device(&self)  -> Style { self.filekinds.char_device }
+    fn socket(&self)       -> Style { self.filekinds.socket }
+    fn special(&self)      -> Style { self.filekinds.special }
 }
 
 impl render::GitColours for Colours {
-	fn not_modified(&self)  -> Style { self.punctuation }
-	fn new(&self)           -> Style { self.git.new }
-	fn modified(&self)      -> Style { self.git.modified }
-	fn deleted(&self)       -> Style { self.git.deleted }
-	fn renamed(&self)       -> Style { self.git.renamed }
-	fn type_change(&self)   -> Style { self.git.typechange }
+    fn not_modified(&self)  -> Style { self.punctuation }
+    fn new(&self)           -> Style { self.git.new }
+    fn modified(&self)      -> Style { self.git.modified }
+    fn deleted(&self)       -> Style { self.git.deleted }
+    fn renamed(&self)       -> Style { self.git.renamed }
+    fn type_change(&self)   -> Style { self.git.typechange }
 }
 
 impl render::GroupColours for Colours {
-	fn yours(&self)      -> Style { self.users.group_yours }
-	fn not_yours(&self)  -> Style { self.users.group_not_yours }
+    fn yours(&self)      -> Style { self.users.group_yours }
+    fn not_yours(&self)  -> Style { self.users.group_not_yours }
 }
 
 impl render::LinksColours for Colours {
-	fn normal(&self)           -> Style { self.links.normal }
-	fn multi_link_file(&self)  -> Style { self.links.multi_link_file }
+    fn normal(&self)           -> Style { self.links.normal }
+    fn multi_link_file(&self)  -> Style { self.links.multi_link_file }
 }
 
 impl render::PermissionsColours for Colours {
-	fn dash(&self)               -> Style { self.punctuation }
-	fn user_read(&self)          -> Style { self.perms.user_read }
-	fn user_write(&self)         -> Style { self.perms.user_write }
-	fn user_execute_file(&self)  -> Style { self.perms.user_execute_file }
-	fn user_execute_other(&self) -> Style { self.perms.user_execute_other }
-	fn group_read(&self)         -> Style { self.perms.group_read }
-	fn group_write(&self)        -> Style { self.perms.group_write }
-	fn group_execute(&self)      -> Style { self.perms.group_execute }
-	fn other_read(&self)         -> Style { self.perms.other_read }
-	fn other_write(&self)        -> Style { self.perms.other_write }
-	fn other_execute(&self)      -> Style { self.perms.other_execute }
-	fn special_user_file(&self)  -> Style { self.perms.special_user_file }
-	fn special_other(&self)      -> Style { self.perms.special_other }
-	fn attribute(&self)          -> Style { self.perms.attribute }
+    fn dash(&self)               -> Style { self.punctuation }
+    fn user_read(&self)          -> Style { self.perms.user_read }
+    fn user_write(&self)         -> Style { self.perms.user_write }
+    fn user_execute_file(&self)  -> Style { self.perms.user_execute_file }
+    fn user_execute_other(&self) -> Style { self.perms.user_execute_other }
+    fn group_read(&self)         -> Style { self.perms.group_read }
+    fn group_write(&self)        -> Style { self.perms.group_write }
+    fn group_execute(&self)      -> Style { self.perms.group_execute }
+    fn other_read(&self)         -> Style { self.perms.other_read }
+    fn other_write(&self)        -> Style { self.perms.other_write }
+    fn other_execute(&self)      -> Style { self.perms.other_execute }
+    fn special_user_file(&self)  -> Style { self.perms.special_user_file }
+    fn special_other(&self)      -> Style { self.perms.special_other }
+    fn attribute(&self)          -> Style { self.perms.attribute }
 }
 
 impl render::SizeColours for Colours {
-	fn size(&self, size: u64)  -> Style {
-		if self.scale {
-			if size < 1024 {
-				self.size.scale_byte
-			}
-			else if size < 1024 * 1024 {
-				self.size.scale_kilo
-			}
-			else if size < 1024 * 1024 * 1024 {
-				self.size.scale_mega
-			}
-			else if size < 1024 * 1024 * 1024 * 1024 {
-				self.size.scale_giga
-			}
-			else {
-				self.size.scale_huge
-			}
-		}
-		else {
-			self.size.numbers
-		}
-	}
-
-	fn unit(&self)    -> Style { self.size.unit }
-	fn no_size(&self) -> Style { self.punctuation }
-	fn major(&self)   -> Style { self.size.major }
-	fn comma(&self)   -> Style { self.punctuation }
-	fn minor(&self)   -> Style { self.size.minor }
+    fn size(&self, size: u64)  -> Style {
+        if self.scale {
+            if size < 1024 {
+                self.size.scale_byte
+            }
+            else if size < 1024 * 1024 {
+                self.size.scale_kilo
+            }
+            else if size < 1024 * 1024 * 1024 {
+                self.size.scale_mega
+            }
+            else if size < 1024 * 1024 * 1024 * 1024 {
+                self.size.scale_giga
+            }
+            else {
+                self.size.scale_huge
+            }
+        }
+        else {
+            self.size.numbers
+        }
+    }
+
+    fn unit(&self)    -> Style { self.size.unit }
+    fn no_size(&self) -> Style { self.punctuation }
+    fn major(&self)   -> Style { self.size.major }
+    fn comma(&self)   -> Style { self.punctuation }
+    fn minor(&self)   -> Style { self.size.minor }
 }
 
 impl render::UserColours for Colours {
-	fn you(&self)           -> Style { self.users.user_you }
-	fn someone_else(&self)  -> Style { self.users.user_someone_else }
+    fn you(&self)           -> Style { self.users.user_you }
+    fn someone_else(&self)  -> Style { self.users.user_someone_else }
 }
 

+ 2 - 2
src/output/file_name.rs

@@ -251,8 +251,8 @@ impl<'a, 'dir> FileName<'a, 'dir> {
             f if f.is_executable_file()  => self.colours.filekinds.executable,
             f if f.is_link()             => self.colours.filekinds.symlink,
             f if f.is_pipe()             => self.colours.filekinds.pipe,
-            f if f.is_char_device()
-               | f.is_block_device()     => self.colours.filekinds.device,
+            f if f.is_block_device()     => self.colours.filekinds.block_device,
+            f if f.is_char_device()      => self.colours.filekinds.char_device,
             f if f.is_socket()           => self.colours.filekinds.socket,
             f if !f.is_file()            => self.colours.filekinds.special,
 

+ 4 - 3
src/output/render/filetype.rs

@@ -10,8 +10,8 @@ impl f::Type {
             f::Type::Directory   => colours.directory().paint("d"),
             f::Type::Pipe        => colours.pipe().paint("|"),
             f::Type::Link        => colours.symlink().paint("l"),
-            f::Type::CharDevice  => colours.device().paint("c"),
-            f::Type::BlockDevice => colours.device().paint("b"),
+            f::Type::BlockDevice => colours.block_device().paint("b"),
+            f::Type::CharDevice  => colours.char_device().paint("c"),
             f::Type::Socket      => colours.socket().paint("s"),
             f::Type::Special     => colours.special().paint("?"),
         }
@@ -24,7 +24,8 @@ pub trait Colours {
     fn directory(&self) -> Style;
     fn pipe(&self) -> Style;
     fn symlink(&self) -> Style;
-    fn device(&self) -> Style;
+    fn block_device(&self) -> Style;
+    fn char_device(&self) -> Style;
     fn socket(&self) -> Style;
     fn special(&self) -> Style;
 }