git.rs 2.7 KB

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