view.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. use std::env::var_os;
  2. use output::Colours;
  3. use output::{View, Mode, grid, details};
  4. use output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions};
  5. use output::file_name::{Classify, FileStyle};
  6. use output::time::TimeFormat;
  7. use options::{flags, Misfire};
  8. use options::parser::MatchedFlags;
  9. use fs::feature::xattr;
  10. use info::filetype::FileExtensions;
  11. impl View {
  12. /// Determine which view to use and all of that view’s arguments.
  13. pub fn deduce(matches: &MatchedFlags) -> Result<View, Misfire> {
  14. let mode = Mode::deduce(matches)?;
  15. let colours = Colours::deduce(matches)?;
  16. let style = FileStyle::deduce(matches)?;
  17. Ok(View { mode, colours, style })
  18. }
  19. }
  20. impl Mode {
  21. /// Determine the mode from the command-line arguments.
  22. pub fn deduce(matches: &MatchedFlags) -> Result<Mode, Misfire> {
  23. use options::misfire::Misfire::*;
  24. let long = || {
  25. if matches.has(&flags::ACROSS)? && !matches.has(&flags::GRID)? {
  26. Err(Useless(&flags::ACROSS, true, &flags::LONG))
  27. }
  28. else if matches.has(&flags::ONE_LINE)? {
  29. Err(Useless(&flags::ONE_LINE, true, &flags::LONG))
  30. }
  31. else {
  32. Ok(details::Options {
  33. table: Some(TableOptions::deduce(matches)?),
  34. header: matches.has(&flags::HEADER)?,
  35. xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
  36. })
  37. }
  38. };
  39. let long_options_scan = || {
  40. for option in &[ &flags::BINARY, &flags::BYTES, &flags::INODE, &flags::LINKS,
  41. &flags::HEADER, &flags::BLOCKS, &flags::TIME, &flags::GROUP ] {
  42. if matches.is_strict() && matches.has(option)? {
  43. return Err(Useless(*option, false, &flags::LONG));
  44. }
  45. }
  46. if cfg!(feature="git") && matches.has(&flags::GIT)? {
  47. Err(Useless(&flags::GIT, false, &flags::LONG))
  48. }
  49. else if matches.has(&flags::LEVEL)? && !matches.has(&flags::RECURSE)? && !matches.has(&flags::TREE)? {
  50. Err(Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE))
  51. }
  52. else {
  53. Ok(())
  54. }
  55. };
  56. let other_options_scan = || {
  57. if let Some(width) = TerminalWidth::deduce()?.width() {
  58. if matches.has(&flags::ONE_LINE)? {
  59. if matches.has(&flags::ACROSS)? {
  60. Err(Useless(&flags::ACROSS, true, &flags::ONE_LINE))
  61. }
  62. else {
  63. Ok(Mode::Lines)
  64. }
  65. }
  66. else if matches.has(&flags::TREE)? {
  67. let details = details::Options {
  68. table: None,
  69. header: false,
  70. xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
  71. };
  72. Ok(Mode::Details(details))
  73. }
  74. else {
  75. let grid = grid::Options {
  76. across: matches.has(&flags::ACROSS)?,
  77. console_width: width,
  78. };
  79. Ok(Mode::Grid(grid))
  80. }
  81. }
  82. else {
  83. // If the terminal width couldn’t be matched for some reason, such
  84. // as the program’s stdout being connected to a file, then
  85. // fallback to the lines view.
  86. if matches.has(&flags::TREE)? {
  87. let details = details::Options {
  88. table: None,
  89. header: false,
  90. xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
  91. };
  92. Ok(Mode::Details(details))
  93. }
  94. else {
  95. Ok(Mode::Lines)
  96. }
  97. }
  98. };
  99. if matches.has(&flags::LONG)? {
  100. let details = long()?;
  101. if matches.has(&flags::GRID)? {
  102. match other_options_scan()? {
  103. Mode::Grid(grid) => return Ok(Mode::GridDetails(grid, details)),
  104. others => return Ok(others),
  105. };
  106. }
  107. else {
  108. return Ok(Mode::Details(details));
  109. }
  110. }
  111. long_options_scan()?;
  112. other_options_scan()
  113. }
  114. }
  115. /// The width of the terminal requested by the user.
  116. #[derive(PartialEq, Debug)]
  117. enum TerminalWidth {
  118. /// The user requested this specific number of columns.
  119. Set(usize),
  120. /// The terminal was found to have this number of columns.
  121. Terminal(usize),
  122. /// The user didn’t request any particular terminal width.
  123. Unset,
  124. }
  125. impl TerminalWidth {
  126. /// Determine a requested terminal width from the command-line arguments.
  127. ///
  128. /// Returns an error if a requested width doesn’t parse to an integer.
  129. fn deduce() -> Result<TerminalWidth, Misfire> {
  130. if let Some(columns) = var_os("COLUMNS").and_then(|s| s.into_string().ok()) {
  131. match columns.parse() {
  132. Ok(width) => Ok(TerminalWidth::Set(width)),
  133. Err(e) => Err(Misfire::FailedParse(e)),
  134. }
  135. }
  136. else if let Some(width) = *TERM_WIDTH {
  137. Ok(TerminalWidth::Terminal(width))
  138. }
  139. else {
  140. Ok(TerminalWidth::Unset)
  141. }
  142. }
  143. fn width(&self) -> Option<usize> {
  144. match *self {
  145. TerminalWidth::Set(width) |
  146. TerminalWidth::Terminal(width) => Some(width),
  147. TerminalWidth::Unset => None,
  148. }
  149. }
  150. }
  151. impl TableOptions {
  152. fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
  153. let env = Environment::load_all();
  154. let time_format = TimeFormat::deduce(matches)?;
  155. let size_format = SizeFormat::deduce(matches)?;
  156. let extra_columns = Columns::deduce(matches)?;
  157. Ok(TableOptions { env, time_format, size_format, extra_columns })
  158. }
  159. }
  160. impl Columns {
  161. fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
  162. let time_types = TimeTypes::deduce(matches)?;
  163. let git = cfg!(feature="git") && matches.has(&flags::GIT)?;
  164. let blocks = matches.has(&flags::BLOCKS)?;
  165. let group = matches.has(&flags::GROUP)?;
  166. let inode = matches.has(&flags::INODE)?;
  167. let links = matches.has(&flags::LINKS)?;
  168. Ok(Columns { time_types, git, blocks, group, inode, links })
  169. }
  170. }
  171. impl SizeFormat {
  172. /// Determine which file size to use in the file size column based on
  173. /// the user’s options.
  174. ///
  175. /// The default mode is to use the decimal prefixes, as they are the
  176. /// most commonly-understood, and don’t involve trying to parse large
  177. /// strings of digits in your head. Changing the format to anything else
  178. /// involves the `--binary` or `--bytes` flags, and these conflict with
  179. /// each other.
  180. fn deduce(matches: &MatchedFlags) -> Result<SizeFormat, Misfire> {
  181. let flag = matches.has_where(|f| f.matches(&flags::BINARY) || f.matches(&flags::BYTES))?;
  182. Ok(match flag {
  183. Some(f) if f.matches(&flags::BINARY) => SizeFormat::BinaryBytes,
  184. Some(f) if f.matches(&flags::BYTES) => SizeFormat::JustBytes,
  185. _ => SizeFormat::DecimalBytes,
  186. })
  187. }
  188. }
  189. const TIME_STYLES: &[&str] = &["default", "long-iso", "full-iso", "iso"];
  190. impl TimeFormat {
  191. /// Determine how time should be formatted in timestamp columns.
  192. fn deduce(matches: &MatchedFlags) -> Result<TimeFormat, Misfire> {
  193. pub use output::time::{DefaultFormat, ISOFormat};
  194. let word = match matches.get(&flags::TIME_STYLE)? {
  195. Some(w) => w,
  196. None => return Ok(TimeFormat::DefaultFormat(DefaultFormat::new())),
  197. };
  198. if word == "default" {
  199. Ok(TimeFormat::DefaultFormat(DefaultFormat::new()))
  200. }
  201. else if word == "iso" {
  202. Ok(TimeFormat::ISOFormat(ISOFormat::new()))
  203. }
  204. else if word == "long-iso" {
  205. Ok(TimeFormat::LongISO)
  206. }
  207. else if word == "full-iso" {
  208. Ok(TimeFormat::FullISO)
  209. }
  210. else {
  211. Err(Misfire::bad_argument(&flags::TIME_STYLE, word, TIME_STYLES))
  212. }
  213. }
  214. }
  215. static TIMES: &[&str] = &["modified", "accessed", "created"];
  216. impl TimeTypes {
  217. /// Determine which of a file’s time fields should be displayed for it
  218. /// based on the user’s options.
  219. ///
  220. /// There are two separate ways to pick which fields to show: with a
  221. /// flag (such as `--modified`) or with a parameter (such as
  222. /// `--time=modified`). An error is signaled if both ways are used.
  223. ///
  224. /// It’s valid to show more than one column by passing in more than one
  225. /// option, but passing *no* options means that the user just wants to
  226. /// see the default set.
  227. fn deduce(matches: &MatchedFlags) -> Result<TimeTypes, Misfire> {
  228. let possible_word = matches.get(&flags::TIME)?;
  229. let modified = matches.has(&flags::MODIFIED)?;
  230. let created = matches.has(&flags::CREATED)?;
  231. let accessed = matches.has(&flags::ACCESSED)?;
  232. if let Some(word) = possible_word {
  233. if modified {
  234. Err(Misfire::Useless(&flags::MODIFIED, true, &flags::TIME))
  235. }
  236. else if created {
  237. Err(Misfire::Useless(&flags::CREATED, true, &flags::TIME))
  238. }
  239. else if accessed {
  240. Err(Misfire::Useless(&flags::ACCESSED, true, &flags::TIME))
  241. }
  242. else if word == "mod" || word == "modified" {
  243. Ok(TimeTypes { accessed: false, modified: true, created: false })
  244. }
  245. else if word == "acc" || word == "accessed" {
  246. Ok(TimeTypes { accessed: true, modified: false, created: false })
  247. }
  248. else if word == "cr" || word == "created" {
  249. Ok(TimeTypes { accessed: false, modified: false, created: true })
  250. }
  251. else {
  252. Err(Misfire::bad_argument(&flags::TIME, word, TIMES))
  253. }
  254. }
  255. else if modified || created || accessed {
  256. Ok(TimeTypes { accessed, modified, created })
  257. }
  258. else {
  259. Ok(TimeTypes::default())
  260. }
  261. }
  262. }
  263. /// Under what circumstances we should display coloured, rather than plain,
  264. /// output to the terminal.
  265. ///
  266. /// By default, we want to display the colours when stdout can display them.
  267. /// Turning them on when output is going to, say, a pipe, would make programs
  268. /// such as `grep` or `more` not work properly. So the `Automatic` mode does
  269. /// this check and only displays colours when they can be truly appreciated.
  270. #[derive(PartialEq, Debug)]
  271. enum TerminalColours {
  272. /// Display them even when output isn’t going to a terminal.
  273. Always,
  274. /// Display them when output is going to a terminal, but not otherwise.
  275. Automatic,
  276. /// Never display them, even when output is going to a terminal.
  277. Never,
  278. }
  279. impl Default for TerminalColours {
  280. fn default() -> TerminalColours {
  281. TerminalColours::Automatic
  282. }
  283. }
  284. const COLOURS: &[&str] = &["always", "auto", "never"];
  285. impl TerminalColours {
  286. /// Determine which terminal colour conditions to use.
  287. fn deduce(matches: &MatchedFlags) -> Result<TerminalColours, Misfire> {
  288. let word = match matches.get_where(|f| f.matches(&flags::COLOR) || f.matches(&flags::COLOUR))? {
  289. Some(w) => w,
  290. None => return Ok(TerminalColours::default()),
  291. };
  292. if word == "always" {
  293. Ok(TerminalColours::Always)
  294. }
  295. else if word == "auto" || word == "automatic" {
  296. Ok(TerminalColours::Automatic)
  297. }
  298. else if word == "never" {
  299. Ok(TerminalColours::Never)
  300. }
  301. else {
  302. Err(Misfire::bad_argument(&flags::COLOR, word, COLOURS))
  303. }
  304. }
  305. }
  306. impl Colours {
  307. fn deduce(matches: &MatchedFlags) -> Result<Colours, Misfire> {
  308. use self::TerminalColours::*;
  309. let tc = TerminalColours::deduce(matches)?;
  310. if tc == Always || (tc == Automatic && TERM_WIDTH.is_some()) {
  311. let scale = matches.has(&flags::COLOR_SCALE)? || matches.has(&flags::COLOUR_SCALE)?;
  312. Ok(Colours::colourful(scale))
  313. }
  314. else {
  315. Ok(Colours::plain())
  316. }
  317. }
  318. }
  319. impl FileStyle {
  320. fn deduce(matches: &MatchedFlags) -> Result<FileStyle, Misfire> {
  321. let classify = Classify::deduce(matches)?;
  322. let exts = FileExtensions;
  323. Ok(FileStyle { classify, exts })
  324. }
  325. }
  326. impl Classify {
  327. fn deduce(matches: &MatchedFlags) -> Result<Classify, Misfire> {
  328. let flagged = matches.has(&flags::CLASSIFY)?;
  329. Ok(if flagged { Classify::AddFileIndicators }
  330. else { Classify::JustFilenames })
  331. }
  332. }
  333. // Gets, then caches, the width of the terminal that exa is running in.
  334. // This gets used multiple times above, with no real guarantee of order,
  335. // so it’s easier to just cache it the first time it runs.
  336. lazy_static! {
  337. static ref TERM_WIDTH: Option<usize> = {
  338. // All of stdin, stdout, and stderr could not be connected to a
  339. // terminal, but we’re only interested in stdout because it’s
  340. // where the output goes.
  341. use term_size::dimensions_stdout;
  342. dimensions_stdout().map(|t| t.0)
  343. };
  344. }
  345. #[cfg(test)]
  346. mod test {
  347. use super::*;
  348. use std::ffi::OsString;
  349. use options::flags;
  350. use options::parser::{Flag, Arg};
  351. use options::test::parse_for_test;
  352. use options::test::Strictnesses::*;
  353. pub fn os(input: &'static str) -> OsString {
  354. let mut os = OsString::new();
  355. os.push(input);
  356. os
  357. }
  358. static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES, &flags::TIME_STYLE,
  359. &flags::TIME, &flags::MODIFIED, &flags::CREATED, &flags::ACCESSED,
  360. &flags::COLOR, &flags::COLOUR,
  361. &flags::HEADER, &flags::GROUP, &flags::INODE,
  362. &flags::LINKS, &flags::BLOCKS, &flags::LONG ];
  363. macro_rules! test {
  364. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => $result:expr) => {
  365. /// Macro that writes a test.
  366. /// If testing both strictnesses, they’ll both be done in the same function.
  367. #[test]
  368. fn $name() {
  369. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  370. assert_eq!(result, $result);
  371. }
  372. }
  373. };
  374. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => err $result:expr) => {
  375. /// Special macro for testing Err results.
  376. /// This is needed because sometimes the Ok type doesn’t implement PartialEq.
  377. #[test]
  378. fn $name() {
  379. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  380. assert_eq!(result.unwrap_err(), $result);
  381. }
  382. }
  383. };
  384. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => like $pat:pat) => {
  385. /// More general macro for testing against a pattern.
  386. /// Instead of using PartialEq, this just tests if it matches a pat.
  387. #[test]
  388. fn $name() {
  389. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
  390. println!("Testing {:?}", result);
  391. match result {
  392. $pat => assert!(true),
  393. _ => assert!(false),
  394. }
  395. }
  396. }
  397. };
  398. }
  399. mod size_formats {
  400. use super::*;
  401. // Default behaviour
  402. test!(empty: SizeFormat <- []; Both => Ok(SizeFormat::DecimalBytes));
  403. // Individual flags
  404. test!(binary: SizeFormat <- ["--binary"]; Both => Ok(SizeFormat::BinaryBytes));
  405. test!(bytes: SizeFormat <- ["--bytes"]; Both => Ok(SizeFormat::JustBytes));
  406. // Overriding
  407. test!(both_1: SizeFormat <- ["--binary", "--binary"]; Last => Ok(SizeFormat::BinaryBytes));
  408. test!(both_2: SizeFormat <- ["--bytes", "--binary"]; Last => Ok(SizeFormat::BinaryBytes));
  409. test!(both_3: SizeFormat <- ["--binary", "--bytes"]; Last => Ok(SizeFormat::JustBytes));
  410. test!(both_4: SizeFormat <- ["--bytes", "--bytes"]; Last => Ok(SizeFormat::JustBytes));
  411. test!(both_5: SizeFormat <- ["--binary", "--binary"]; Complain => err Misfire::Duplicate(Flag::Long("binary"), Flag::Long("binary")));
  412. test!(both_6: SizeFormat <- ["--bytes", "--binary"]; Complain => err Misfire::Duplicate(Flag::Long("bytes"), Flag::Long("binary")));
  413. test!(both_7: SizeFormat <- ["--binary", "--bytes"]; Complain => err Misfire::Duplicate(Flag::Long("binary"), Flag::Long("bytes")));
  414. test!(both_8: SizeFormat <- ["--bytes", "--bytes"]; Complain => err Misfire::Duplicate(Flag::Long("bytes"), Flag::Long("bytes")));
  415. }
  416. mod time_formats {
  417. use super::*;
  418. use std::ffi::OsStr;
  419. // These tests use pattern matching because TimeFormat doesn’t
  420. // implement PartialEq.
  421. // Default behaviour
  422. test!(empty: TimeFormat <- []; Both => like Ok(TimeFormat::DefaultFormat(_)));
  423. // Individual settings
  424. test!(default: TimeFormat <- ["--time-style=default"]; Both => like Ok(TimeFormat::DefaultFormat(_)));
  425. test!(iso: TimeFormat <- ["--time-style", "iso"]; Both => like Ok(TimeFormat::ISOFormat(_)));
  426. test!(long_iso: TimeFormat <- ["--time-style=long-iso"]; Both => like Ok(TimeFormat::LongISO));
  427. test!(full_iso: TimeFormat <- ["--time-style", "full-iso"]; Both => like Ok(TimeFormat::FullISO));
  428. // Overriding
  429. test!(actually: TimeFormat <- ["--time-style=default", "--time-style", "iso"]; Last => like Ok(TimeFormat::ISOFormat(_)));
  430. test!(actual_2: TimeFormat <- ["--time-style=default", "--time-style", "iso"]; Complain => err Misfire::Duplicate(Flag::Long("time-style"), Flag::Long("time-style")));
  431. test!(nevermind: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"]; Last => like Ok(TimeFormat::FullISO));
  432. test!(nevermore: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"]; Complain => err Misfire::Duplicate(Flag::Long("time-style"), Flag::Long("time-style")));
  433. // Errors
  434. test!(daily: TimeFormat <- ["--time-style=24-hour"]; Both => err Misfire::bad_argument(&flags::TIME_STYLE, OsStr::new("24-hour"), TIME_STYLES));
  435. }
  436. mod time_types {
  437. use super::*;
  438. // Default behaviour
  439. test!(empty: TimeTypes <- []; Both => Ok(TimeTypes::default()));
  440. // Modified
  441. test!(modified: TimeTypes <- ["--modified"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
  442. test!(m: TimeTypes <- ["-m"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
  443. test!(time_mod: TimeTypes <- ["--time=modified"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
  444. test!(time_m: TimeTypes <- ["-tmod"]; Both => Ok(TimeTypes { accessed: false, modified: true, created: false }));
  445. // Accessed
  446. test!(acc: TimeTypes <- ["--accessed"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
  447. test!(a: TimeTypes <- ["-u"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
  448. test!(time_acc: TimeTypes <- ["--time", "accessed"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
  449. test!(time_a: TimeTypes <- ["-t", "acc"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: false }));
  450. // Created
  451. test!(cr: TimeTypes <- ["--created"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
  452. test!(c: TimeTypes <- ["-U"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
  453. test!(time_cr: TimeTypes <- ["--time=created"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
  454. test!(time_c: TimeTypes <- ["-tcr"]; Both => Ok(TimeTypes { accessed: false, modified: false, created: true }));
  455. // Multiples
  456. test!(time_uu: TimeTypes <- ["-uU"]; Both => Ok(TimeTypes { accessed: true, modified: false, created: true }));
  457. // Errors
  458. test!(time_tea: TimeTypes <- ["--time=tea"]; Both => err Misfire::bad_argument(&flags::TIME, &os("tea"), super::TIMES));
  459. test!(time_ea: TimeTypes <- ["-tea"]; Both => err Misfire::bad_argument(&flags::TIME, &os("ea"), super::TIMES));
  460. // Overriding
  461. test!(overridden: TimeTypes <- ["-tcr", "-tmod"]; Last => Ok(TimeTypes { accessed: false, modified: true, created: false }));
  462. test!(overridden_2: TimeTypes <- ["-tcr", "-tmod"]; Complain => err Misfire::Duplicate(Flag::Short(b't'), Flag::Short(b't')));
  463. }
  464. mod colourses {
  465. use super::*;
  466. // Default
  467. test!(empty: TerminalColours <- []; Both => Ok(TerminalColours::default()));
  468. // --colour
  469. test!(u_always: TerminalColours <- ["--colour=always"]; Both => Ok(TerminalColours::Always));
  470. test!(u_auto: TerminalColours <- ["--colour", "auto"]; Both => Ok(TerminalColours::Automatic));
  471. test!(u_never: TerminalColours <- ["--colour=never"]; Both => Ok(TerminalColours::Never));
  472. // --color
  473. test!(no_u_always: TerminalColours <- ["--color", "always"]; Both => Ok(TerminalColours::Always));
  474. test!(no_u_auto: TerminalColours <- ["--color=auto"]; Both => Ok(TerminalColours::Automatic));
  475. test!(no_u_never: TerminalColours <- ["--color", "never"]; Both => Ok(TerminalColours::Never));
  476. // Errors
  477. test!(no_u_error: TerminalColours <- ["--color=upstream"]; Both => err Misfire::bad_argument(&flags::COLOR, &os("upstream"), super::COLOURS)); // the error is for --color
  478. test!(u_error: TerminalColours <- ["--colour=lovers"]; Both => err Misfire::bad_argument(&flags::COLOR, &os("lovers"), super::COLOURS)); // and so is this one!
  479. // Overriding
  480. test!(overridden_1: TerminalColours <- ["--colour=auto", "--colour=never"]; Last => Ok(TerminalColours::Never));
  481. test!(overridden_2: TerminalColours <- ["--color=auto", "--colour=never"]; Last => Ok(TerminalColours::Never));
  482. test!(overridden_3: TerminalColours <- ["--colour=auto", "--color=never"]; Last => Ok(TerminalColours::Never));
  483. test!(overridden_4: TerminalColours <- ["--color=auto", "--color=never"]; Last => Ok(TerminalColours::Never));
  484. test!(overridden_5: TerminalColours <- ["--colour=auto", "--colour=never"]; Complain => err Misfire::Duplicate(Flag::Long("colour"), Flag::Long("colour")));
  485. test!(overridden_6: TerminalColours <- ["--color=auto", "--colour=never"]; Complain => err Misfire::Duplicate(Flag::Long("color"), Flag::Long("colour")));
  486. test!(overridden_7: TerminalColours <- ["--colour=auto", "--color=never"]; Complain => err Misfire::Duplicate(Flag::Long("colour"), Flag::Long("color")));
  487. test!(overridden_8: TerminalColours <- ["--color=auto", "--color=never"]; Complain => err Misfire::Duplicate(Flag::Long("color"), Flag::Long("color")));
  488. }
  489. mod views {
  490. use super::*;
  491. test!(just_header: Mode <- ["--header"]; Last => like Ok(Mode::Grid(_)));
  492. test!(just_header_2: Mode <- ["--header"]; Complain => err Misfire::Useless(&flags::HEADER, false, &flags::LONG));
  493. test!(just_group: Mode <- ["--group"]; Last => like Ok(Mode::Grid(_)));
  494. test!(just_group_2: Mode <- ["--group"]; Complain => err Misfire::Useless(&flags::GROUP, false, &flags::LONG));
  495. test!(just_inode: Mode <- ["--inode"]; Last => like Ok(Mode::Grid(_)));
  496. test!(just_inode_2: Mode <- ["--inode"]; Complain => err Misfire::Useless(&flags::INODE, false, &flags::LONG));
  497. test!(just_links: Mode <- ["--links"]; Last => like Ok(Mode::Grid(_)));
  498. test!(just_links_2: Mode <- ["--links"]; Complain => err Misfire::Useless(&flags::LINKS, false, &flags::LONG));
  499. test!(just_blocks: Mode <- ["--blocks"]; Last => like Ok(Mode::Grid(_)));
  500. test!(just_blocks_2: Mode <- ["--blocks"]; Complain => err Misfire::Useless(&flags::BLOCKS, false, &flags::LONG));
  501. test!(just_binary: Mode <- ["--binary"]; Last => like Ok(Mode::Grid(_)));
  502. test!(just_binary_2: Mode <- ["--binary"]; Complain => err Misfire::Useless(&flags::BINARY, false, &flags::LONG));
  503. test!(just_bytes: Mode <- ["--bytes"]; Last => like Ok(Mode::Grid(_)));
  504. test!(just_bytes_2: Mode <- ["--bytes"]; Complain => err Misfire::Useless(&flags::BYTES, false, &flags::LONG));
  505. }
  506. }