theme.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use crate::options::{flags, vars, Vars, OptionsError};
  2. use crate::options::parser::MatchedFlags;
  3. use crate::theme::{Options, UseColours, ColourScale, Definitions};
  4. impl Options {
  5. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  6. let use_colours = UseColours::deduce(matches, vars)?;
  7. let colour_scale = ColourScale::deduce(matches)?;
  8. let definitions = if use_colours == UseColours::Never {
  9. Definitions::default()
  10. }
  11. else {
  12. Definitions::deduce(vars)
  13. };
  14. Ok(Self { use_colours, colour_scale, definitions })
  15. }
  16. }
  17. impl UseColours {
  18. fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  19. let default_value = match vars.get(vars::NO_COLOR) {
  20. Some(_) => Self::Never,
  21. None => Self::Automatic,
  22. };
  23. let word = match matches.get_where(|f| f.matches(&flags::COLOR) || f.matches(&flags::COLOUR))? {
  24. Some(w) => w,
  25. None => return Ok(default_value),
  26. };
  27. if word == "always" {
  28. Ok(Self::Always)
  29. }
  30. else if word == "auto" || word == "automatic" {
  31. Ok(Self::Automatic)
  32. }
  33. else if word == "never" {
  34. Ok(Self::Never)
  35. }
  36. else {
  37. Err(OptionsError::BadArgument(&flags::COLOR, word.into()))
  38. }
  39. }
  40. }
  41. impl ColourScale {
  42. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  43. if matches.has_where(|f| f.matches(&flags::COLOR_SCALE) || f.matches(&flags::COLOUR_SCALE))?.is_some() {
  44. Ok(Self::Gradient)
  45. }
  46. else {
  47. Ok(Self::Fixed)
  48. }
  49. }
  50. }
  51. impl Definitions {
  52. fn deduce<V: Vars>(vars: &V) -> Self {
  53. let ls = vars.get(vars::LS_COLORS) .map(|e| e.to_string_lossy().to_string());
  54. let exa = vars.get(vars::EXA_COLORS).map(|e| e.to_string_lossy().to_string());
  55. Self { ls, exa }
  56. }
  57. }
  58. #[cfg(test)]
  59. mod terminal_test {
  60. use super::*;
  61. use std::ffi::OsString;
  62. use crate::options::flags;
  63. use crate::options::parser::{Flag, Arg};
  64. use crate::options::test::parse_for_test;
  65. use crate::options::test::Strictnesses::*;
  66. static TEST_ARGS: &[&Arg] = &[ &flags::COLOR, &flags::COLOUR,
  67. &flags::COLOR_SCALE, &flags::COLOUR_SCALE, ];
  68. macro_rules! test {
  69. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => $result:expr) => {
  70. #[test]
  71. fn $name() {
  72. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  73. assert_eq!(result, $result);
  74. }
  75. }
  76. };
  77. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => err $result:expr) => {
  78. #[test]
  79. fn $name() {
  80. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  81. assert_eq!(result.unwrap_err(), $result);
  82. }
  83. }
  84. };
  85. }
  86. struct MockVars {
  87. ls: &'static str,
  88. exa: &'static str,
  89. }
  90. // Test impl that just returns the value it has.
  91. impl Vars for MockVars {
  92. fn get(&self, name: &'static str) -> Option<OsString> {
  93. if name == vars::LS_COLORS && ! self.ls.is_empty() {
  94. Some(OsString::from(self.ls.clone()))
  95. }
  96. else if name == vars::EXA_COLORS && ! self.exa.is_empty() {
  97. Some(OsString::from(self.exa.clone()))
  98. }
  99. else {
  100. None
  101. }
  102. }
  103. }
  104. // Default
  105. test!(empty: UseColours <- []; Both => Ok(UseColours::Automatic));
  106. // --colour
  107. test!(u_always: UseColours <- ["--colour=always"]; Both => Ok(UseColours::Always));
  108. test!(u_auto: UseColours <- ["--colour", "auto"]; Both => Ok(UseColours::Automatic));
  109. test!(u_never: UseColours <- ["--colour=never"]; Both => Ok(UseColours::Never));
  110. // --color
  111. test!(no_u_always: UseColours <- ["--color", "always"]; Both => Ok(UseColours::Always));
  112. test!(no_u_auto: UseColours <- ["--color=auto"]; Both => Ok(UseColours::Automatic));
  113. test!(no_u_never: UseColours <- ["--color", "never"]; Both => Ok(UseColours::Never));
  114. // Errors
  115. test!(no_u_error: UseColours <- ["--color=upstream"]; Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("upstream"))); // the error is for --color
  116. test!(u_error: UseColours <- ["--colour=lovers"]; Both => err OptionsError::BadArgument(&flags::COLOR, OsString::from("lovers"))); // and so is this one!
  117. // Overriding
  118. test!(overridden_1: UseColours <- ["--colour=auto", "--colour=never"]; Last => Ok(UseColours::Never));
  119. test!(overridden_2: UseColours <- ["--color=auto", "--colour=never"]; Last => Ok(UseColours::Never));
  120. test!(overridden_3: UseColours <- ["--colour=auto", "--color=never"]; Last => Ok(UseColours::Never));
  121. test!(overridden_4: UseColours <- ["--color=auto", "--color=never"]; Last => Ok(UseColours::Never));
  122. test!(overridden_5: UseColours <- ["--colour=auto", "--colour=never"]; Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("colour")));
  123. test!(overridden_6: UseColours <- ["--color=auto", "--colour=never"]; Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("colour")));
  124. test!(overridden_7: UseColours <- ["--colour=auto", "--color=never"]; Complain => err OptionsError::Duplicate(Flag::Long("colour"), Flag::Long("color")));
  125. test!(overridden_8: UseColours <- ["--color=auto", "--color=never"]; Complain => err OptionsError::Duplicate(Flag::Long("color"), Flag::Long("color")));
  126. test!(scale_1: ColourScale <- ["--color-scale", "--colour-scale"]; Last => Ok(ColourScale::Gradient));
  127. test!(scale_2: ColourScale <- ["--color-scale", ]; Last => Ok(ColourScale::Gradient));
  128. test!(scale_3: ColourScale <- [ "--colour-scale"]; Last => Ok(ColourScale::Gradient));
  129. test!(scale_4: ColourScale <- [ ]; Last => Ok(ColourScale::Fixed));
  130. test!(scale_5: ColourScale <- ["--color-scale", "--colour-scale"]; Complain => err OptionsError::Duplicate(Flag::Long("color-scale"), Flag::Long("colour-scale")));
  131. test!(scale_6: ColourScale <- ["--color-scale", ]; Complain => Ok(ColourScale::Gradient));
  132. test!(scale_7: ColourScale <- [ "--colour-scale"]; Complain => Ok(ColourScale::Gradient));
  133. test!(scale_8: ColourScale <- [ ]; Complain => Ok(ColourScale::Fixed));
  134. }