git.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use ansi_term::{ANSIString, Style};
  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::GitStatus {
  16. fn render(self, colours: &dyn Colours) -> ANSIString<'static> {
  17. match self {
  18. Self::NotModified => colours.not_modified().paint("-"),
  19. Self::New => colours.new().paint("N"),
  20. Self::Modified => colours.modified().paint("M"),
  21. Self::Deleted => colours.deleted().paint("D"),
  22. Self::Renamed => colours.renamed().paint("R"),
  23. Self::TypeChange => colours.type_change().paint("T"),
  24. Self::Ignored => colours.ignored().paint("I"),
  25. Self::Conflicted => colours.conflicted().paint("U"),
  26. }
  27. }
  28. }
  29. pub trait Colours {
  30. fn not_modified(&self) -> Style;
  31. #[allow(clippy::new_ret_no_self)]
  32. fn new(&self) -> Style;
  33. fn modified(&self) -> Style;
  34. fn deleted(&self) -> Style;
  35. fn renamed(&self) -> Style;
  36. fn type_change(&self) -> Style;
  37. fn ignored(&self) -> Style;
  38. fn conflicted(&self) -> Style;
  39. }
  40. #[cfg(test)]
  41. pub mod test {
  42. use super::Colours;
  43. use crate::output::cell::{TextCell, DisplayWidth};
  44. use crate::fs::fields as f;
  45. use ansi_term::Colour::*;
  46. use ansi_term::Style;
  47. struct TestColours;
  48. impl Colours for TestColours {
  49. fn not_modified(&self) -> Style { Fixed(90).normal() }
  50. fn new(&self) -> Style { Fixed(91).normal() }
  51. fn modified(&self) -> Style { Fixed(92).normal() }
  52. fn deleted(&self) -> Style { Fixed(93).normal() }
  53. fn renamed(&self) -> Style { Fixed(94).normal() }
  54. fn type_change(&self) -> Style { Fixed(95).normal() }
  55. fn ignored(&self) -> Style { Fixed(96).normal() }
  56. fn conflicted(&self) -> Style { Fixed(97).normal() }
  57. }
  58. #[test]
  59. fn git_blank() {
  60. let stati = f::Git {
  61. staged: f::GitStatus::NotModified,
  62. unstaged: f::GitStatus::NotModified,
  63. };
  64. let expected = TextCell {
  65. width: DisplayWidth::from(2),
  66. contents: vec![
  67. Fixed(90).paint("-"),
  68. Fixed(90).paint("-"),
  69. ].into(),
  70. };
  71. assert_eq!(expected, stati.render(&TestColours).into())
  72. }
  73. #[test]
  74. fn git_new_changed() {
  75. let stati = f::Git {
  76. staged: f::GitStatus::New,
  77. unstaged: f::GitStatus::Modified,
  78. };
  79. let expected = TextCell {
  80. width: DisplayWidth::from(2),
  81. contents: vec![
  82. Fixed(91).paint("N"),
  83. Fixed(92).paint("M"),
  84. ].into(),
  85. };
  86. assert_eq!(expected, stati.render(&TestColours).into())
  87. }
  88. }