file_name.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use crate::options::{flags, OptionsError, NumberSource};
  2. use crate::options::parser::MatchedFlags;
  3. use crate::options::vars::{self, Vars};
  4. use crate::output::file_name::{Options, Classify, ShowIcons};
  5. impl Options {
  6. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  7. let classify = Classify::deduce(matches)?;
  8. let show_icons = ShowIcons::deduce(matches, vars)?;
  9. Ok(Self { classify, show_icons })
  10. }
  11. }
  12. impl Classify {
  13. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  14. let flagged = matches.has(&flags::CLASSIFY)?;
  15. if flagged { Ok(Self::AddFileIndicators) }
  16. else { Ok(Self::JustFilenames) }
  17. }
  18. }
  19. impl ShowIcons {
  20. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  21. if matches.has(&flags::NO_ICONS)? || !matches.has(&flags::ICONS)? {
  22. Ok(Self::Off)
  23. }
  24. else if let Some(columns) = vars.get(vars::EXA_ICON_SPACING).and_then(|s| s.into_string().ok()) {
  25. match columns.parse() {
  26. Ok(width) => {
  27. Ok(Self::On(width))
  28. }
  29. Err(e) => {
  30. let source = NumberSource::Env(vars::EXA_ICON_SPACING);
  31. Err(OptionsError::FailedParse(columns, source, e))
  32. }
  33. }
  34. }
  35. else {
  36. Ok(Self::On(1))
  37. }
  38. }
  39. }