view.rs 33 KB

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