Jelajahi Sumber

Split FileTypes into types and kinds

This separates the colours to give to files with different filesystem types (directories, links, sockets) from files with different names or extensions (images, videos, archives).

I’m not 100% sure I’ve got the terms “kind” and “type” the right way round, but whatever.

This was done because colouring files based on their name is going to be handled differently and extensibly from colouring files based on what the filesystem thinks.
Benjamin Sago 8 tahun lalu
induk
melakukan
d517e9e12b
3 mengubah file dengan 31 tambahan dan 20 penghapusan
  1. 2 2
      src/options/colours.rs
  2. 20 10
      src/output/colours.rs
  3. 9 8
      src/output/file_name.rs

+ 2 - 2
src/options/colours.rs

@@ -78,7 +78,7 @@ impl Colours {
             let lsc = LSColors::parse(lsc.as_ref());
 
             if let Some(dir) = lsc.get("di") {
-                c.filetypes.directory = dir;
+                c.filekinds.directory = dir;
             }
         }
 
@@ -270,5 +270,5 @@ mod customs_test {
         }
     }
 
-    test!(override_1:  ls "di=34", exa ""  =>  |c: &mut Colours| { c.filetypes.directory = Blue.normal(); });
+    test!(override_1:  ls "di=34", exa ""  =>  |c: &mut Colours| { c.filekinds.directory = Blue.normal(); });
 }

+ 20 - 10
src/output/colours.rs

@@ -8,6 +8,7 @@ use output::render;
 pub struct Colours {
     pub scale: bool,
 
+    pub filekinds:  FileKinds,
     pub filetypes:  FileTypes,
     pub perms:      Permissions,
     pub size:       Size,
@@ -27,8 +28,9 @@ pub struct Colours {
     pub control_char:     Style,
 }
 
+// Colours for files depending on their filesystem type.
 #[derive(Clone, Copy, Debug, Default, PartialEq)]
-pub struct FileTypes {
+pub struct FileKinds {
     pub normal: Style,
     pub directory: Style,
     pub symlink: Style,
@@ -37,6 +39,11 @@ pub struct FileTypes {
     pub socket: Style,
     pub special: Style,
     pub executable: Style,
+}
+
+// Colours for files depending on their name or extension.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub struct FileTypes {
     pub image: Style,
     pub video: Style,
     pub music: Style,
@@ -117,7 +124,7 @@ impl Colours {
         Colours {
             scale: scale,
 
-            filetypes: FileTypes {
+            filekinds: FileKinds {
                 normal:      Style::default(),
                 directory:   Blue.bold(),
                 symlink:     Cyan.normal(),
@@ -126,6 +133,9 @@ impl Colours {
                 socket:      Red.bold(),
                 special:     Yellow.normal(),
                 executable:  Green.bold(),
+            },
+
+            filetypes: FileTypes {
                 image:       Fixed(133).normal(),
                 video:       Fixed(135).normal(),
                 music:       Fixed(92).normal(),
@@ -213,13 +223,13 @@ impl render::BlocksColours for Colours {
 }
 
 impl render::FiletypeColours for Colours {
-	fn normal(&self)     -> Style { self.filetypes.normal }
-	fn directory(&self)  -> Style { self.filetypes.directory }
-	fn pipe(&self)       -> Style { self.filetypes.pipe }
-	fn symlink(&self)    -> Style { self.filetypes.symlink }
-	fn device(&self)     -> Style { self.filetypes.device }
-	fn socket(&self)     -> Style { self.filetypes.socket }
-	fn special(&self)    -> Style { self.filetypes.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 device(&self)     -> Style { self.filekinds.device }
+	fn socket(&self)     -> Style { self.filekinds.socket }
+	fn special(&self)    -> Style { self.filekinds.special }
 }
 
 impl render::GitColours for Colours {
@@ -281,7 +291,7 @@ impl render::SizeColours for Colours {
 			self.size.numbers
 		}
 	}
-	
+
 	fn unit(&self)    -> Style { self.size.unit }
 	fn no_size(&self) -> Style { self.punctuation }
 	fn major(&self)   -> Style { self.size.major }

+ 9 - 8
src/output/file_name.rs

@@ -247,14 +247,14 @@ impl<'a, 'dir> FileName<'a, 'dir> {
         // Otherwise, just apply a bunch of rules in order. For example,
         // executable image files should be executable rather than images.
         match self.file {
-            f if f.is_directory()        => self.colours.filetypes.directory,
-            f if f.is_executable_file()  => self.colours.filetypes.executable,
-            f if f.is_link()             => self.colours.filetypes.symlink,
-            f if f.is_pipe()             => self.colours.filetypes.pipe,
+            f if f.is_directory()        => self.colours.filekinds.directory,
+            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.filetypes.device,
-            f if f.is_socket()           => self.colours.filetypes.socket,
-            f if !f.is_file()            => self.colours.filetypes.special,
+               | f.is_block_device()     => self.colours.filekinds.device,
+            f if f.is_socket()           => self.colours.filekinds.socket,
+            f if !f.is_file()            => self.colours.filekinds.special,
 
             f if self.exts.is_immediate(f)   => self.colours.filetypes.immediate,
             f if self.exts.is_image(f)       => self.colours.filetypes.image,
@@ -266,7 +266,8 @@ impl<'a, 'dir> FileName<'a, 'dir> {
             f if self.exts.is_compressed(f)  => self.colours.filetypes.compressed,
             f if self.exts.is_temp(f)        => self.colours.filetypes.temp,
             f if self.exts.is_compiled(f)    => self.colours.filetypes.compiled,
-            _                                => self.colours.filetypes.normal,
+
+            _                                => self.colours.filekinds.normal,
         }
     }
 }