1
0

git.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use ansiterm::{ANSIString, Style, Color};
  2. use crate::output::cell::{TextCell, DisplayWidth};
  3. use crate::fs::fields as f;
  4. impl f::Git {
  5. pub fn render(self, colours: &dyn Colours) -> TextCell {
  6. TextCell {
  7. width: DisplayWidth::from(2),
  8. contents: vec![
  9. self.staged.render(colours),
  10. self.unstaged.render(colours),
  11. ].into(),
  12. }
  13. }
  14. }
  15. impl f::SubdirGitRepo {
  16. pub fn render(self) -> TextCell {
  17. let style = Style::new();
  18. let branch_style = match self.branch.as_deref(){
  19. Some("master") => style.fg(Color::Green),
  20. Some("main") => style.fg(Color::Green),
  21. Some(_) => style.fg(Color::Fixed(208)),
  22. _ => style,
  23. };
  24. let branch = branch_style.paint(self.branch.unwrap_or(String::from("-")));
  25. let s = match self.status {
  26. f::SubdirGitRepoStatus::NoRepo => style.paint("- "),
  27. f::SubdirGitRepoStatus::GitClean => style.fg(Color::Green).paint("| "),
  28. f::SubdirGitRepoStatus::GitDirty => style.bold().fg(Color::Red).paint("+ "),
  29. f::SubdirGitRepoStatus::GitUnknown => style.fg(Color::Green).bold().paint("~ "),
  30. };
  31. TextCell {
  32. width: DisplayWidth::from(2 + branch.len()),
  33. contents: vec![s,branch].into(),
  34. }
  35. }
  36. }
  37. impl f::GitStatus {
  38. fn render(self, colours: &dyn Colours) -> ANSIString<'static> {
  39. match self {
  40. Self::NotModified => colours.not_modified().paint("-"),
  41. Self::New => colours.new().paint("N"),
  42. Self::Modified => colours.modified().paint("M"),
  43. Self::Deleted => colours.deleted().paint("D"),
  44. Self::Renamed => colours.renamed().paint("R"),
  45. Self::TypeChange => colours.type_change().paint("T"),
  46. Self::Ignored => colours.ignored().paint("I"),
  47. Self::Conflicted => colours.conflicted().paint("U"),
  48. }
  49. }
  50. }
  51. pub trait Colours {
  52. fn not_modified(&self) -> Style;
  53. // FIXME: this amount of allows needed to keep clippy happy should be enough
  54. // of an argument that new needs to be renamed.
  55. #[allow(clippy::new_ret_no_self,clippy::wrong_self_convention)]
  56. fn new(&self) -> Style;
  57. fn modified(&self) -> Style;
  58. fn deleted(&self) -> Style;
  59. fn renamed(&self) -> Style;
  60. fn type_change(&self) -> Style;
  61. fn ignored(&self) -> Style;
  62. fn conflicted(&self) -> Style;
  63. }
  64. #[cfg(test)]
  65. pub mod test {
  66. use super::Colours;
  67. use crate::output::cell::{TextCell, DisplayWidth};
  68. use crate::fs::fields as f;
  69. use ansiterm::Colour::*;
  70. use ansiterm::Style;
  71. struct TestColours;
  72. impl Colours for TestColours {
  73. fn not_modified(&self) -> Style { Fixed(90).normal() }
  74. fn new(&self) -> Style { Fixed(91).normal() }
  75. fn modified(&self) -> Style { Fixed(92).normal() }
  76. fn deleted(&self) -> Style { Fixed(93).normal() }
  77. fn renamed(&self) -> Style { Fixed(94).normal() }
  78. fn type_change(&self) -> Style { Fixed(95).normal() }
  79. fn ignored(&self) -> Style { Fixed(96).normal() }
  80. fn conflicted(&self) -> Style { Fixed(97).normal() }
  81. }
  82. #[test]
  83. fn git_blank() {
  84. let stati = f::Git {
  85. staged: f::GitStatus::NotModified,
  86. unstaged: f::GitStatus::NotModified,
  87. };
  88. let expected = TextCell {
  89. width: DisplayWidth::from(2),
  90. contents: vec![
  91. Fixed(90).paint("-"),
  92. Fixed(90).paint("-"),
  93. ].into(),
  94. };
  95. assert_eq!(expected, stati.render(&TestColours))
  96. }
  97. #[test]
  98. fn git_new_changed() {
  99. let stati = f::Git {
  100. staged: f::GitStatus::New,
  101. unstaged: f::GitStatus::Modified,
  102. };
  103. let expected = TextCell {
  104. width: DisplayWidth::from(2),
  105. contents: vec![
  106. Fixed(91).paint("N"),
  107. Fixed(92).paint("M"),
  108. ].into(),
  109. };
  110. assert_eq!(expected, stati.render(&TestColours))
  111. }
  112. }