Ver código fonte

Highlight the current user

Ben S 11 anos atrás
pai
commit
6edf2c0d4d
4 arquivos alterados com 13 adições e 3 exclusões
  1. 1 1
      column.rs
  2. 5 1
      file.rs
  3. 2 1
      options.rs
  4. 5 0
      unix.rs

+ 1 - 1
column.rs

@@ -2,6 +2,6 @@ pub enum Column {
     Permissions,
     FileName,
     FileSize(bool),
-    User,
+    User(u64),
     Group,
 }

+ 5 - 1
file.rs

@@ -71,7 +71,11 @@ impl<'a> File<'a> {
 
             // Display the ID if the user/group doesn't exist, which
             // usually means it was deleted but its files weren't.
-            User => get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str()),
+            User(uid) => {
+                let style = if uid == self.stat.unstable.uid { Yellow.bold() } else { Plain };
+                let string = get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str());
+                return style.paint(string.as_slice());
+            },
             Group => get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()),
         }
     }

+ 2 - 1
options.rs

@@ -3,6 +3,7 @@ extern crate getopts;
 use file::File;
 use std::cmp::lexical_ordering;
 use column::{Column, Permissions, FileName, FileSize, User, Group};
+use unix::get_current_user_id;
 
 pub enum SortField {
     Name, Extension, Size
@@ -53,7 +54,7 @@ impl Options {
         let mut columns = vec![
             Permissions,
             FileSize(matches.opt_present("binary")),
-            User,
+            User(get_current_user_id()),
         ];
 
         if matches.opt_present("group") {

+ 5 - 0
unix.rs

@@ -31,6 +31,7 @@ mod c {
     extern {
         pub fn getpwuid(uid: c_int) -> *c_passwd;
         pub fn getgrgid(gid: uid_t) -> *c_group;
+        pub fn getuid() -> libc::c_int;
     }
 }
 
@@ -53,3 +54,7 @@ pub fn get_group_name(gid: u32) -> Option<String> {
         return None;
     }
 }
+
+pub fn get_current_user_id() -> u64 {
+    unsafe { c::getuid() as u64 }
+}