Просмотр исходного кода

Add git branch info to Repo column

	modified:   src/fs/feature/git.rs
	modified:   src/fs/feature/mod.rs
	modified:   src/fs/fields.rs
	modified:   src/output/render/git.rs
	modified:   src/output/table.rs
alpn 5 лет назад
Родитель
Сommit
f17cf3db8a
5 измененных файлов с 60 добавлено и 20 удалено
  1. 23 4
      src/fs/feature/git.rs
  2. 1 1
      src/fs/feature/mod.rs
  3. 15 5
      src/fs/fields.rs
  4. 20 9
      src/output/render/git.rs
  5. 1 1
      src/output/table.rs

+ 23 - 4
src/fs/feature/git.rs

@@ -312,6 +312,25 @@ fn index_status(status: git2::Status) -> f::GitStatus {
     }
 }
 
+fn current_branch(repo: &git2::Repository) -> Option<String>{
+    let head = match repo.head() {
+        Ok(head) => Some(head),
+        Err(ref e) if e.code() == git2::ErrorCode::UnbornBranch || e.code() == git2::ErrorCode::NotFound => return None,
+        Err(e) => {
+            error!("Error looking up Git branch: {:?}", e);
+            return None
+        }
+    };
+
+    if let Some(h) = head{
+        if let Some(s) = h.shorthand(){
+            return Some(s.to_owned());
+        }
+    }
+
+    None
+}
+
 impl f::SubdirGitRepo{
     pub fn from_path(dir : &Path) -> Self{
 
@@ -319,19 +338,19 @@ impl f::SubdirGitRepo{
         let g = git2::Repository::open(path);
         if let Ok(repo) = g{
 
+            let branch = current_branch(&repo);
             match repo.statuses(None) {
                 Ok(es) => {
                     if es.iter().filter(|s| s.status() != git2::Status::IGNORED).count() > 0{
-                        return Self{status : f::SubdirGitRepoStatus::GitDirty};
+                        return Self{status : f::SubdirGitRepoStatus::GitDirty, branch};
                     }
-                    return Self{status : f::SubdirGitRepoStatus::GitClean};
+                    return Self{status : f::SubdirGitRepoStatus::GitClean, branch};
                 }
                 Err(e) => {
                     error!("Error looking up Git statuses: {:?}", e)
                 }
             }
         }
-
-        Self{status : f::SubdirGitRepoStatus::NotRepo}
+        Self::default()
     }
 }

+ 1 - 1
src/fs/feature/mod.rs

@@ -33,7 +33,7 @@ pub mod git {
 
     impl f::SubdirGitRepo{
         pub fn from_path(_dir : &Path) -> Self{
-            Self{status : f::SubdirGitRepoStatus::NotRepo}
+            panic!("Tried to get subdir Git status, but Git support is disabled")
         }
     }
 }

+ 15 - 5
src/fs/fields.rs

@@ -246,15 +246,25 @@ impl Default for Git {
     }
 }
 
-#[derive(PartialEq, Copy, Clone)]
 #[allow(dead_code)]
+#[derive(PartialEq, Copy, Clone)]
 pub enum SubdirGitRepoStatus{
-    NotDir,
-    NotRepo,
+    NoRepo,
     GitClean,
     GitDirty
 }
-#[derive(Copy, Clone)]
+
+#[derive(Clone)]
 pub struct SubdirGitRepo{
-    pub status : SubdirGitRepoStatus
+    pub status : SubdirGitRepoStatus,
+    pub branch : Option<String>
 }
+
+impl Default for SubdirGitRepo{
+    fn default() -> Self {
+        Self{
+            status : SubdirGitRepoStatus::NoRepo,
+            branch : None
+        }
+    }
+}

+ 20 - 9
src/output/render/git.rs

@@ -1,4 +1,4 @@
-use ansi_term::{ANSIString, Style};
+use ansi_term::{ANSIString, Style, Color};
 
 use crate::output::cell::{TextCell, DisplayWidth};
 use crate::fs::fields as f;
@@ -18,15 +18,26 @@ impl f::Git {
 
 impl f::SubdirGitRepo {
     pub fn render(self) -> TextCell {
-        let style = ansi_term::Style::new();
-            match self.status{
-                f::SubdirGitRepoStatus::NotDir => (),
-                f::SubdirGitRepoStatus::NotRepo => (),
-                f::SubdirGitRepoStatus::GitClean => return TextCell::paint_str(style.bold().fg(ansi_term::Color::Green), "V"),
-                f::SubdirGitRepoStatus::GitDirty => return TextCell::paint_str(style.bold().fg(ansi_term::Color::Red), "X"),
-            }
-        TextCell::blank(style)
+        let style = Style::new();
+        let branch_style = match self.branch.as_deref(){
+            Some("master") => style.fg(Color::Green),
+            Some(_) => style.fg(Color::Fixed(208)),
+            _ => style,
+        };
+        
+        let branch = branch_style.paint(self.branch.unwrap_or(String::from("-")));
+
+        let s = match self.status {
+            f::SubdirGitRepoStatus::NoRepo => style.paint("- "),
+            f::SubdirGitRepoStatus::GitClean => style.bold().fg(Color::Green).paint("V "),
+            f::SubdirGitRepoStatus::GitDirty => style.bold().fg(Color::Red).paint("X "),
+        };
+
+        TextCell {
+            width: DisplayWidth::from(2 + branch.len()),
+            contents: vec![s,branch].into(),
         }
+    }
 }
 
 impl f::GitStatus {

+ 1 - 1
src/output/table.rs

@@ -443,7 +443,7 @@ impl<'a, 'f> Table<'a> {
         if file.is_directory(){
             return f::SubdirGitRepo::from_path(&file.path);
         }
-        f::SubdirGitRepo {status : f::SubdirGitRepoStatus::NotDir}
+        f::SubdirGitRepo::default()
     }
 
     pub fn render(&self, row: Row) -> TextCell {