view.rs 27 KB

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