view.rs 31 KB

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