stdin.rs 871 B

1234567891011121314151617181920212223242526272829
  1. use crate::options::parser::MatchedFlags;
  2. use crate::options::vars::EZA_STDIN_SEPARATOR;
  3. use crate::options::{flags, OptionsError, Vars};
  4. use std::ffi::OsString;
  5. use std::io;
  6. use std::io::IsTerminal;
  7. #[derive(Debug, PartialEq)]
  8. pub enum FilesInput {
  9. Stdin(OsString),
  10. Args,
  11. }
  12. impl FilesInput {
  13. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  14. Ok(
  15. if io::stdin().is_terminal() || !matches.has(&flags::STDIN)? {
  16. FilesInput::Args
  17. } else if matches.has(&flags::STDIN)? && !io::stdin().is_terminal() {
  18. let separator = vars
  19. .get(EZA_STDIN_SEPARATOR)
  20. .unwrap_or(OsString::from("\n"));
  21. FilesInput::Stdin(separator)
  22. } else {
  23. FilesInput::Args
  24. },
  25. )
  26. }
  27. }