view.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. use crate::fs::feature::xattr;
  2. use crate::options::{flags, OptionsError, NumberSource, Vars};
  3. use crate::options::parser::MatchedFlags;
  4. use crate::output::{View, Mode, TerminalWidth, grid, details};
  5. use crate::output::grid_details::{self, RowThreshold};
  6. use crate::output::file_name::Options as FileStyle;
  7. use crate::output::table::{TimeTypes, SizeFormat, UserFormat, Columns, Options as TableOptions};
  8. use crate::output::time::TimeFormat;
  9. impl View {
  10. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  11. let mode = Mode::deduce(matches, vars)?;
  12. let width = TerminalWidth::deduce(vars)?;
  13. let file_style = FileStyle::deduce(matches, vars)?;
  14. Ok(Self { mode, width, file_style })
  15. }
  16. }
  17. impl Mode {
  18. /// Determine which viewing mode to use based on the user’s options.
  19. ///
  20. /// As with the other options, arguments are scanned right-to-left and the
  21. /// first flag found is matched, so `exa --oneline --long` will pick a
  22. /// details view, and `exa --long --oneline` will pick the lines view.
  23. ///
  24. /// This is complicated a little by the fact that `--grid` and `--tree`
  25. /// can also combine with `--long`, so care has to be taken to use the
  26. pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  27. let flag = matches.has_where_any(|f| f.matches(&flags::LONG) || f.matches(&flags::ONE_LINE)
  28. || f.matches(&flags::GRID) || f.matches(&flags::TREE));
  29. let flag = if let Some(f) = flag { f } else {
  30. Self::strict_check_long_flags(matches)?;
  31. let grid = grid::Options::deduce(matches)?;
  32. return Ok(Self::Grid(grid));
  33. };
  34. if flag.matches(&flags::LONG)
  35. || (flag.matches(&flags::TREE) && matches.has(&flags::LONG)?)
  36. || (flag.matches(&flags::GRID) && matches.has(&flags::LONG)?)
  37. {
  38. let _ = matches.has(&flags::LONG)?;
  39. let details = details::Options::deduce_long(matches, vars)?;
  40. let flag = matches.has_where_any(|f| f.matches(&flags::GRID) || f.matches(&flags::TREE));
  41. if flag.is_some() && flag.unwrap().matches(&flags::GRID) {
  42. let _ = matches.has(&flags::GRID)?;
  43. let grid = grid::Options::deduce(matches)?;
  44. let row_threshold = RowThreshold::deduce(vars)?;
  45. let grid_details = grid_details::Options { grid, details, row_threshold };
  46. return Ok(Self::GridDetails(grid_details));
  47. }
  48. // the --tree case is handled by the DirAction parser later
  49. return Ok(Self::Details(details));
  50. }
  51. Self::strict_check_long_flags(matches)?;
  52. if flag.matches(&flags::TREE) {
  53. let _ = matches.has(&flags::TREE)?;
  54. let details = details::Options::deduce_tree(matches)?;
  55. return Ok(Self::Details(details));
  56. }
  57. if flag.matches(&flags::ONE_LINE) {
  58. let _ = matches.has(&flags::ONE_LINE)?;
  59. return Ok(Self::Lines);
  60. }
  61. let grid = grid::Options::deduce(matches)?;
  62. Ok(Self::Grid(grid))
  63. }
  64. fn strict_check_long_flags(matches: &MatchedFlags<'_>) -> Result<(), OptionsError> {
  65. // If --long hasn’t been passed, then check if we need to warn the
  66. // user about flags that won’t have any effect.
  67. if matches.is_strict() {
  68. for option in &[ &flags::BINARY, &flags::BYTES, &flags::INODE, &flags::LINKS,
  69. &flags::HEADER, &flags::BLOCKS, &flags::TIME, &flags::GROUP, &flags::NUMERIC ] {
  70. if matches.has(option)? {
  71. return Err(OptionsError::Useless(*option, false, &flags::LONG));
  72. }
  73. }
  74. if matches.has(&flags::GIT)? {
  75. return Err(OptionsError::Useless(&flags::GIT, false, &flags::LONG));
  76. }
  77. else if matches.has(&flags::LEVEL)? && ! matches.has(&flags::RECURSE)? && ! matches.has(&flags::TREE)? {
  78. return Err(OptionsError::Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE));
  79. }
  80. }
  81. Ok(())
  82. }
  83. }
  84. impl grid::Options {
  85. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  86. let grid = grid::Options {
  87. across: matches.has(&flags::ACROSS)?,
  88. };
  89. Ok(grid)
  90. }
  91. }
  92. impl details::Options {
  93. fn deduce_tree(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  94. let details = details::Options {
  95. table: None,
  96. header: false,
  97. xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
  98. };
  99. Ok(details)
  100. }
  101. fn deduce_long<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  102. if matches.is_strict() {
  103. if matches.has(&flags::ACROSS)? && ! matches.has(&flags::GRID)? {
  104. return Err(OptionsError::Useless(&flags::ACROSS, true, &flags::LONG));
  105. }
  106. else if matches.has(&flags::ONE_LINE)? {
  107. return Err(OptionsError::Useless(&flags::ONE_LINE, true, &flags::LONG));
  108. }
  109. }
  110. Ok(details::Options {
  111. table: Some(TableOptions::deduce(matches, vars)?),
  112. header: matches.has(&flags::HEADER)?,
  113. xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
  114. })
  115. }
  116. }
  117. impl TerminalWidth {
  118. fn deduce<V: Vars>(vars: &V) -> Result<Self, OptionsError> {
  119. use crate::options::vars;
  120. if let Some(columns) = vars.get(vars::COLUMNS).and_then(|s| s.into_string().ok()) {
  121. match columns.parse() {
  122. Ok(width) => {
  123. Ok(Self::Set(width))
  124. }
  125. Err(e) => {
  126. let source = NumberSource::Env(vars::COLUMNS);
  127. Err(OptionsError::FailedParse(columns, source, e))
  128. }
  129. }
  130. }
  131. else {
  132. Ok(Self::Automatic)
  133. }
  134. }
  135. }
  136. impl RowThreshold {
  137. fn deduce<V: Vars>(vars: &V) -> Result<Self, OptionsError> {
  138. use crate::options::vars;
  139. if let Some(columns) = vars.get(vars::EXA_GRID_ROWS).and_then(|s| s.into_string().ok()) {
  140. match columns.parse() {
  141. Ok(rows) => {
  142. Ok(Self::MinimumRows(rows))
  143. }
  144. Err(e) => {
  145. let source = NumberSource::Env(vars::EXA_GRID_ROWS);
  146. Err(OptionsError::FailedParse(columns, source, e))
  147. }
  148. }
  149. }
  150. else {
  151. Ok(Self::AlwaysGrid)
  152. }
  153. }
  154. }
  155. impl TableOptions {
  156. fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  157. let time_format = TimeFormat::deduce(matches, vars)?;
  158. let size_format = SizeFormat::deduce(matches)?;
  159. let user_format = UserFormat::deduce(matches)?;
  160. let columns = Columns::deduce(matches)?;
  161. Ok(Self { size_format, time_format, user_format, columns })
  162. }
  163. }
  164. impl Columns {
  165. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  166. let time_types = TimeTypes::deduce(matches)?;
  167. let git = matches.has(&flags::GIT)?;
  168. let blocks = matches.has(&flags::BLOCKS)?;
  169. let group = matches.has(&flags::GROUP)?;
  170. let inode = matches.has(&flags::INODE)?;
  171. let links = matches.has(&flags::LINKS)?;
  172. let octal = matches.has(&flags::OCTAL)?;
  173. let permissions = ! matches.has(&flags::NO_PERMISSIONS)?;
  174. let filesize = ! matches.has(&flags::NO_FILESIZE)?;
  175. let user = ! matches.has(&flags::NO_USER)?;
  176. Ok(Self { time_types, inode, links, blocks, group, git, octal, permissions, filesize, user })
  177. }
  178. }
  179. impl SizeFormat {
  180. /// Determine which file size to use in the file size column based on
  181. /// the user’s options.
  182. ///
  183. /// The default mode is to use the decimal prefixes, as they are the
  184. /// most commonly-understood, and don’t involve trying to parse large
  185. /// strings of digits in your head. Changing the format to anything else
  186. /// involves the `--binary` or `--bytes` flags, and these conflict with
  187. /// each other.
  188. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  189. let flag = matches.has_where(|f| f.matches(&flags::BINARY) || f.matches(&flags::BYTES))?;
  190. Ok(match flag {
  191. Some(f) if f.matches(&flags::BINARY) => Self::BinaryBytes,
  192. Some(f) if f.matches(&flags::BYTES) => Self::JustBytes,
  193. _ => Self::DecimalBytes,
  194. })
  195. }
  196. }
  197. impl TimeFormat {
  198. /// Determine how time should be formatted in timestamp columns.
  199. fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
  200. let word =
  201. if let Some(w) = matches.get(&flags::TIME_STYLE)? {
  202. w.to_os_string()
  203. }
  204. else {
  205. use crate::options::vars;
  206. match vars.get(vars::TIME_STYLE) {
  207. Some(ref t) if ! t.is_empty() => t.clone(),
  208. _ => return Ok(Self::DefaultFormat)
  209. }
  210. };
  211. match word.to_string_lossy().as_ref() {
  212. "default" => Ok(Self::DefaultFormat),
  213. "relative" => Ok(Self::Relative),
  214. "iso" => Ok(Self::ISOFormat),
  215. "long-iso" => Ok(Self::LongISO),
  216. "full-iso" => Ok(Self::FullISO),
  217. _ => Err(OptionsError::BadArgument(&flags::TIME_STYLE, word))
  218. }
  219. }
  220. }
  221. impl UserFormat {
  222. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  223. let flag = matches.has(&flags::NUMERIC)?;
  224. Ok(if flag { Self::Numeric } else { Self::Name })
  225. }
  226. }
  227. impl TimeTypes {
  228. /// Determine which of a file’s time fields should be displayed for it
  229. /// based on the user’s options.
  230. ///
  231. /// There are two separate ways to pick which fields to show: with a
  232. /// flag (such as `--modified`) or with a parameter (such as
  233. /// `--time=modified`). An error is signaled if both ways are used.
  234. ///
  235. /// It’s valid to show more than one column by passing in more than one
  236. /// option, but passing *no* options means that the user just wants to
  237. /// see the default set.
  238. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  239. let possible_word = matches.get(&flags::TIME)?;
  240. let modified = matches.has(&flags::MODIFIED)?;
  241. let changed = matches.has(&flags::CHANGED)?;
  242. let accessed = matches.has(&flags::ACCESSED)?;
  243. let created = matches.has(&flags::CREATED)?;
  244. let no_time = matches.has(&flags::NO_TIME)?;
  245. let time_types = if no_time {
  246. Self { modified: false, changed: false, accessed: false, created: false }
  247. } else if let Some(word) = possible_word {
  248. if modified {
  249. return Err(OptionsError::Useless(&flags::MODIFIED, true, &flags::TIME));
  250. }
  251. else if changed {
  252. return Err(OptionsError::Useless(&flags::CHANGED, true, &flags::TIME));
  253. }
  254. else if accessed {
  255. return Err(OptionsError::Useless(&flags::ACCESSED, true, &flags::TIME));
  256. }
  257. else if created {
  258. return Err(OptionsError::Useless(&flags::CREATED, true, &flags::TIME));
  259. }
  260. else if word == "mod" || word == "modified" {
  261. Self { modified: true, changed: false, accessed: false, created: false }
  262. }
  263. else if word == "ch" || word == "changed" {
  264. Self { modified: false, changed: true, accessed: false, created: false }
  265. }
  266. else if word == "acc" || word == "accessed" {
  267. Self { modified: false, changed: false, accessed: true, created: false }
  268. }
  269. else if word == "cr" || word == "created" {
  270. Self { modified: false, changed: false, accessed: false, created: true }
  271. }
  272. else {
  273. return Err(OptionsError::BadArgument(&flags::TIME, word.into()));
  274. }
  275. }
  276. else if modified || changed || accessed || created {
  277. Self { modified, changed, accessed, created }
  278. }
  279. else {
  280. Self::default()
  281. };
  282. Ok(time_types)
  283. }
  284. }
  285. #[cfg(test)]
  286. mod test {
  287. use super::*;
  288. use std::ffi::OsString;
  289. use crate::options::flags;
  290. use crate::options::parser::{Flag, Arg};
  291. use crate::options::test::parse_for_test;
  292. use crate::options::test::Strictnesses::*;
  293. static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES, &flags::TIME_STYLE,
  294. &flags::TIME, &flags::MODIFIED, &flags::CHANGED,
  295. &flags::CREATED, &flags::ACCESSED,
  296. &flags::HEADER, &flags::GROUP, &flags::INODE, &flags::GIT,
  297. &flags::LINKS, &flags::BLOCKS, &flags::LONG, &flags::LEVEL,
  298. &flags::GRID, &flags::ACROSS, &flags::ONE_LINE, &flags::TREE,
  299. &flags::NUMERIC ];
  300. macro_rules! test {
  301. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => $result:expr) => {
  302. /// Macro that writes a test.
  303. /// If testing both strictnesses, they’ll both be done in the same function.
  304. #[test]
  305. fn $name() {
  306. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  307. assert_eq!(result, $result);
  308. }
  309. }
  310. };
  311. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => err $result:expr) => {
  312. /// Special macro for testing Err results.
  313. /// This is needed because sometimes the Ok type doesn’t implement `PartialEq`.
  314. #[test]
  315. fn $name() {
  316. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  317. assert_eq!(result.unwrap_err(), $result);
  318. }
  319. }
  320. };
  321. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => like $pat:pat) => {
  322. /// More general macro for testing against a pattern.
  323. /// Instead of using `PartialEq`, this just tests if it matches a pat.
  324. #[test]
  325. fn $name() {
  326. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  327. println!("Testing {:?}", result);
  328. match result {
  329. $pat => assert!(true),
  330. _ => assert!(false),
  331. }
  332. }
  333. }
  334. };
  335. ($name:ident: $type:ident <- $inputs:expr, $vars:expr; $stricts:expr => err $result:expr) => {
  336. /// Like above, but with $vars.
  337. #[test]
  338. fn $name() {
  339. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &$vars)) {
  340. assert_eq!(result.unwrap_err(), $result);
  341. }
  342. }
  343. };
  344. ($name:ident: $type:ident <- $inputs:expr, $vars:expr; $stricts:expr => like $pat:pat) => {
  345. /// Like further above, but with $vars.
  346. #[test]
  347. fn $name() {
  348. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf, &$vars)) {
  349. println!("Testing {:?}", result);
  350. match result {
  351. $pat => assert!(true),
  352. _ => assert!(false),
  353. }
  354. }
  355. }
  356. };
  357. }
  358. mod size_formats {
  359. use super::*;
  360. // Default behaviour
  361. test!(empty: SizeFormat <- []; Both => Ok(SizeFormat::DecimalBytes));
  362. // Individual flags
  363. test!(binary: SizeFormat <- ["--binary"]; Both => Ok(SizeFormat::BinaryBytes));
  364. test!(bytes: SizeFormat <- ["--bytes"]; Both => Ok(SizeFormat::JustBytes));
  365. // Overriding
  366. test!(both_1: SizeFormat <- ["--binary", "--binary"]; Last => Ok(SizeFormat::BinaryBytes));
  367. test!(both_2: SizeFormat <- ["--bytes", "--binary"]; Last => Ok(SizeFormat::BinaryBytes));
  368. test!(both_3: SizeFormat <- ["--binary", "--bytes"]; Last => Ok(SizeFormat::JustBytes));
  369. test!(both_4: SizeFormat <- ["--bytes", "--bytes"]; Last => Ok(SizeFormat::JustBytes));
  370. test!(both_5: SizeFormat <- ["--binary", "--binary"]; Complain => err OptionsError::Duplicate(Flag::Long("binary"), Flag::Long("binary")));
  371. test!(both_6: SizeFormat <- ["--bytes", "--binary"]; Complain => err OptionsError::Duplicate(Flag::Long("bytes"), Flag::Long("binary")));
  372. test!(both_7: SizeFormat <- ["--binary", "--bytes"]; Complain => err OptionsError::Duplicate(Flag::Long("binary"), Flag::Long("bytes")));
  373. test!(both_8: SizeFormat <- ["--bytes", "--bytes"]; Complain => err OptionsError::Duplicate(Flag::Long("bytes"), Flag::Long("bytes")));
  374. }
  375. mod time_formats {
  376. use super::*;
  377. // These tests use pattern matching because TimeFormat doesn’t
  378. // implement PartialEq.
  379. // Default behaviour
  380. test!(empty: TimeFormat <- [], None; Both => like Ok(TimeFormat::DefaultFormat));
  381. // Individual settings
  382. test!(default: TimeFormat <- ["--time-style=default"], None; Both => like Ok(TimeFormat::DefaultFormat));
  383. test!(iso: TimeFormat <- ["--time-style", "iso"], None; Both => like Ok(TimeFormat::ISOFormat));
  384. test!(relative: TimeFormat <- ["--time-style", "relative"], None; Both => like Ok(TimeFormat::Relative));
  385. test!(long_iso: TimeFormat <- ["--time-style=long-iso"], None; Both => like Ok(TimeFormat::LongISO));
  386. test!(full_iso: TimeFormat <- ["--time-style", "full-iso"], None; Both => like Ok(TimeFormat::FullISO));
  387. // Overriding
  388. test!(actually: TimeFormat <- ["--time-style=default", "--time-style", "iso"], None; Last => like Ok(TimeFormat::ISOFormat));
  389. test!(actual_2: TimeFormat <- ["--time-style=default", "--time-style", "iso"], None; Complain => err OptionsError::Duplicate(Flag::Long("time-style"), Flag::Long("time-style")));
  390. test!(nevermind: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"], None; Last => like Ok(TimeFormat::FullISO));
  391. test!(nevermore: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"], None; Complain => err OptionsError::Duplicate(Flag::Long("time-style"), Flag::Long("time-style")));
  392. // Errors
  393. test!(daily: TimeFormat <- ["--time-style=24-hour"], None; Both => err OptionsError::BadArgument(&flags::TIME_STYLE, OsString::from("24-hour")));
  394. // `TIME_STYLE` environment variable is defined.
  395. // If the time-style argument is not given, `TIME_STYLE` is used.
  396. test!(use_env: TimeFormat <- [], Some("long-iso".into()); Both => like Ok(TimeFormat::LongISO));
  397. // If the time-style argument is given, `TIME_STYLE` is overriding.
  398. test!(override_env: TimeFormat <- ["--time-style=full-iso"], Some("long-iso".into()); Both => like Ok(TimeFormat::FullISO));
  399. }
  400. mod time_types {
  401. use super::*;
  402. // Default behaviour
  403. test!(empty: TimeTypes <- []; Both => Ok(TimeTypes::default()));
  404. // Modified
  405. test!(modified: TimeTypes <- ["--modified"]; Both => Ok(TimeTypes { modified: true, changed: false, accessed: false, created: false }));
  406. test!(m: TimeTypes <- ["-m"]; Both => Ok(TimeTypes { modified: true, changed: false, accessed: false, created: false }));
  407. test!(time_mod: TimeTypes <- ["--time=modified"]; Both => Ok(TimeTypes { modified: true, changed: false, accessed: false, created: false }));
  408. test!(t_m: TimeTypes <- ["-tmod"]; Both => Ok(TimeTypes { modified: true, changed: false, accessed: false, created: false }));
  409. // Changed
  410. #[cfg(target_family = "unix")]
  411. test!(changed: TimeTypes <- ["--changed"]; Both => Ok(TimeTypes { modified: false, changed: true, accessed: false, created: false }));
  412. #[cfg(target_family = "unix")]
  413. test!(time_ch: TimeTypes <- ["--time=changed"]; Both => Ok(TimeTypes { modified: false, changed: true, accessed: false, created: false }));
  414. #[cfg(target_family = "unix")]
  415. test!(t_ch: TimeTypes <- ["-t", "ch"]; Both => Ok(TimeTypes { modified: false, changed: true, accessed: false, created: false }));
  416. // Accessed
  417. test!(acc: TimeTypes <- ["--accessed"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));
  418. test!(a: TimeTypes <- ["-u"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));
  419. test!(time_acc: TimeTypes <- ["--time", "accessed"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));
  420. test!(time_a: TimeTypes <- ["-t", "acc"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));
  421. // Created
  422. test!(cr: TimeTypes <- ["--created"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
  423. test!(c: TimeTypes <- ["-U"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
  424. test!(time_cr: TimeTypes <- ["--time=created"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
  425. test!(t_cr: TimeTypes <- ["-tcr"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
  426. // Multiples
  427. test!(time_uu: TimeTypes <- ["-u", "--modified"]; Both => Ok(TimeTypes { modified: true, changed: false, accessed: true, created: false }));
  428. // Errors
  429. test!(time_tea: TimeTypes <- ["--time=tea"]; Both => err OptionsError::BadArgument(&flags::TIME, OsString::from("tea")));
  430. test!(t_ea: TimeTypes <- ["-tea"]; Both => err OptionsError::BadArgument(&flags::TIME, OsString::from("ea")));
  431. // Overriding
  432. test!(overridden: TimeTypes <- ["-tcr", "-tmod"]; Last => Ok(TimeTypes { modified: true, changed: false, accessed: false, created: false }));
  433. test!(overridden_2: TimeTypes <- ["-tcr", "-tmod"]; Complain => err OptionsError::Duplicate(Flag::Short(b't'), Flag::Short(b't')));
  434. }
  435. mod views {
  436. use super::*;
  437. use crate::output::grid::Options as GridOptions;
  438. // Default
  439. test!(empty: Mode <- [], None; Both => like Ok(Mode::Grid(_)));
  440. // Grid views
  441. test!(original_g: Mode <- ["-G"], None; Both => like Ok(Mode::Grid(GridOptions { across: false, .. })));
  442. test!(grid: Mode <- ["--grid"], None; Both => like Ok(Mode::Grid(GridOptions { across: false, .. })));
  443. test!(across: Mode <- ["--across"], None; Both => like Ok(Mode::Grid(GridOptions { across: true, .. })));
  444. test!(gracross: Mode <- ["-xG"], None; Both => like Ok(Mode::Grid(GridOptions { across: true, .. })));
  445. // Lines views
  446. test!(lines: Mode <- ["--oneline"], None; Both => like Ok(Mode::Lines));
  447. test!(prima: Mode <- ["-1"], None; Both => like Ok(Mode::Lines));
  448. // Details views
  449. test!(long: Mode <- ["--long"], None; Both => like Ok(Mode::Details(_)));
  450. test!(ell: Mode <- ["-l"], None; Both => like Ok(Mode::Details(_)));
  451. // Grid-details views
  452. test!(lid: Mode <- ["--long", "--grid"], None; Both => like Ok(Mode::GridDetails(_)));
  453. test!(leg: Mode <- ["-lG"], None; Both => like Ok(Mode::GridDetails(_)));
  454. // Options that do nothing with --long
  455. test!(long_across: Mode <- ["--long", "--across"], None; Last => like Ok(Mode::Details(_)));
  456. // Options that do nothing without --long
  457. test!(just_header: Mode <- ["--header"], None; Last => like Ok(Mode::Grid(_)));
  458. test!(just_group: Mode <- ["--group"], None; Last => like Ok(Mode::Grid(_)));
  459. test!(just_inode: Mode <- ["--inode"], None; Last => like Ok(Mode::Grid(_)));
  460. test!(just_links: Mode <- ["--links"], None; Last => like Ok(Mode::Grid(_)));
  461. test!(just_blocks: Mode <- ["--blocks"], None; Last => like Ok(Mode::Grid(_)));
  462. test!(just_binary: Mode <- ["--binary"], None; Last => like Ok(Mode::Grid(_)));
  463. test!(just_bytes: Mode <- ["--bytes"], None; Last => like Ok(Mode::Grid(_)));
  464. test!(just_numeric: Mode <- ["--numeric"], None; Last => like Ok(Mode::Grid(_)));
  465. #[cfg(feature = "git")]
  466. test!(just_git: Mode <- ["--git"], None; Last => like Ok(Mode::Grid(_)));
  467. test!(just_header_2: Mode <- ["--header"], None; Complain => err OptionsError::Useless(&flags::HEADER, false, &flags::LONG));
  468. test!(just_group_2: Mode <- ["--group"], None; Complain => err OptionsError::Useless(&flags::GROUP, false, &flags::LONG));
  469. test!(just_inode_2: Mode <- ["--inode"], None; Complain => err OptionsError::Useless(&flags::INODE, false, &flags::LONG));
  470. test!(just_links_2: Mode <- ["--links"], None; Complain => err OptionsError::Useless(&flags::LINKS, false, &flags::LONG));
  471. test!(just_blocks_2: Mode <- ["--blocks"], None; Complain => err OptionsError::Useless(&flags::BLOCKS, false, &flags::LONG));
  472. test!(just_binary_2: Mode <- ["--binary"], None; Complain => err OptionsError::Useless(&flags::BINARY, false, &flags::LONG));
  473. test!(just_bytes_2: Mode <- ["--bytes"], None; Complain => err OptionsError::Useless(&flags::BYTES, false, &flags::LONG));
  474. test!(just_numeric2: Mode <- ["--numeric"], None; Complain => err OptionsError::Useless(&flags::NUMERIC, false, &flags::LONG));
  475. #[cfg(feature = "git")]
  476. test!(just_git_2: Mode <- ["--git"], None; Complain => err OptionsError::Useless(&flags::GIT, false, &flags::LONG));
  477. // Contradictions and combinations
  478. test!(lgo: Mode <- ["--long", "--grid", "--oneline"], None; Both => like Ok(Mode::Lines));
  479. test!(lgt: Mode <- ["--long", "--grid", "--tree"], None; Both => like Ok(Mode::Details(_)));
  480. test!(tgl: Mode <- ["--tree", "--grid", "--long"], None; Both => like Ok(Mode::GridDetails(_)));
  481. test!(tlg: Mode <- ["--tree", "--long", "--grid"], None; Both => like Ok(Mode::GridDetails(_)));
  482. test!(ot: Mode <- ["--oneline", "--tree"], None; Both => like Ok(Mode::Details(_)));
  483. test!(og: Mode <- ["--oneline", "--grid"], None; Both => like Ok(Mode::Grid(_)));
  484. test!(tg: Mode <- ["--tree", "--grid"], None; Both => like Ok(Mode::Grid(_)));
  485. }
  486. }