view.rs 21.8 KB
Newer Older
B
Benjamin Sago 已提交
1 2 3
use std::env::var_os;

use output::Colours;
4
use output::{View, Mode, grid, details};
5
use output::table::{TimeTypes, Environment, SizeFormat, Options as TableOptions};
6
use output::file_name::{Classify, FileStyle};
7
use output::time::TimeFormat;
B
Benjamin Sago 已提交
8 9

use options::{flags, Misfire};
10
use options::parser::MatchedFlags;
B
Benjamin Sago 已提交
11

B
Benjamin Sago 已提交
12
use fs::feature::xattr;
B
Benjamin Sago 已提交
13
use info::filetype::FileExtensions;
B
Benjamin Sago 已提交
14

15

16 17 18
impl View {

    /// Determine which view to use and all of that view’s arguments.
19
    pub fn deduce(matches: &MatchedFlags) -> Result<View, Misfire> {
20 21
        let mode = Mode::deduce(matches)?;
        let colours = Colours::deduce(matches)?;
B
Benjamin Sago 已提交
22
        let style = FileStyle::deduce(matches)?;
23
        Ok(View { mode, colours, style })
24 25 26 27 28
    }
}


impl Mode {
B
Benjamin Sago 已提交
29

30
    /// Determine the mode from the command-line arguments.
31
    pub fn deduce(matches: &MatchedFlags) -> Result<Mode, Misfire> {
B
Benjamin Sago 已提交
32 33 34
        use options::misfire::Misfire::*;

        let long = || {
B
Benjamin Sago 已提交
35
            if matches.has(&flags::ACROSS)? && !matches.has(&flags::GRID)? {
B
Benjamin Sago 已提交
36
                Err(Useless(&flags::ACROSS, true, &flags::LONG))
B
Benjamin Sago 已提交
37
            }
B
Benjamin Sago 已提交
38
            else if matches.has(&flags::ONE_LINE)? {
B
Benjamin Sago 已提交
39
                Err(Useless(&flags::ONE_LINE, true, &flags::LONG))
B
Benjamin Sago 已提交
40 41
            }
            else {
B
Benjamin Sago 已提交
42
                Ok(details::Options {
43
                    table: Some(TableOptions::deduce(matches)?),
B
Benjamin Sago 已提交
44 45
                    header: matches.has(&flags::HEADER)?,
                    xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
B
Benjamin Sago 已提交
46
                })
B
Benjamin Sago 已提交
47 48 49 50
            }
        };

        let long_options_scan = || {
B
Benjamin Sago 已提交
51 52
            for option in &[ &flags::BINARY, &flags::BYTES, &flags::INODE, &flags::LINKS,
                             &flags::HEADER, &flags::BLOCKS, &flags::TIME, &flags::GROUP ] {
B
Benjamin Sago 已提交
53
                if matches.has(option)? {
B
Benjamin Sago 已提交
54
                    return Err(Useless(*option, false, &flags::LONG));
B
Benjamin Sago 已提交
55 56 57
                }
            }

B
Benjamin Sago 已提交
58
            if cfg!(feature="git") && matches.has(&flags::GIT)? {
B
Benjamin Sago 已提交
59
                Err(Useless(&flags::GIT, false, &flags::LONG))
B
Benjamin Sago 已提交
60
            }
B
Benjamin Sago 已提交
61
            else if matches.has(&flags::LEVEL)? && !matches.has(&flags::RECURSE)? && !matches.has(&flags::TREE)? {
B
Benjamin Sago 已提交
62
                Err(Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE))
B
Benjamin Sago 已提交
63 64 65 66 67 68 69
            }
            else {
                Ok(())
            }
        };

        let other_options_scan = || {
70
            if let Some(width) = TerminalWidth::deduce()?.width() {
B
Benjamin Sago 已提交
71 72
                if matches.has(&flags::ONE_LINE)? {
                    if matches.has(&flags::ACROSS)? {
B
Benjamin Sago 已提交
73
                        Err(Useless(&flags::ACROSS, true, &flags::ONE_LINE))
B
Benjamin Sago 已提交
74 75
                    }
                    else {
76
                        Ok(Mode::Lines)
B
Benjamin Sago 已提交
77 78
                    }
                }
B
Benjamin Sago 已提交
79
                else if matches.has(&flags::TREE)? {
B
Benjamin Sago 已提交
80
                    let details = details::Options {
81
                        table: None,
B
Benjamin Sago 已提交
82
                        header: false,
B
Benjamin Sago 已提交
83
                        xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
B
Benjamin Sago 已提交
84 85
                    };

86
                    Ok(Mode::Details(details))
B
Benjamin Sago 已提交
87 88
                }
                else {
B
Benjamin Sago 已提交
89
                    let grid = grid::Options {
B
Benjamin Sago 已提交
90
                        across: matches.has(&flags::ACROSS)?,
B
Benjamin Sago 已提交
91 92 93
                        console_width: width,
                    };

94
                    Ok(Mode::Grid(grid))
B
Benjamin Sago 已提交
95 96 97 98 99 100 101
                }
            }
            else {
                // If the terminal width couldn’t be matched for some reason, such
                // as the program’s stdout being connected to a file, then
                // fallback to the lines view.

B
Benjamin Sago 已提交
102
                if matches.has(&flags::TREE)? {
B
Benjamin Sago 已提交
103
                    let details = details::Options {
104
                        table: None,
B
Benjamin Sago 已提交
105
                        header: false,
B
Benjamin Sago 已提交
106
                        xattr: xattr::ENABLED && matches.has(&flags::EXTENDED)?,
B
Benjamin Sago 已提交
107 108
                    };

109
                    Ok(Mode::Details(details))
B
Benjamin Sago 已提交
110 111
                }
                else {
112
                    Ok(Mode::Lines)
B
Benjamin Sago 已提交
113 114 115 116
                }
            }
        };

B
Benjamin Sago 已提交
117
        if matches.has(&flags::LONG)? {
B
Benjamin Sago 已提交
118
            let details = long()?;
B
Benjamin Sago 已提交
119
            if matches.has(&flags::GRID)? {
B
Benjamin Sago 已提交
120 121 122 123
                match other_options_scan()? {
                    Mode::Grid(grid)  => return Ok(Mode::GridDetails(grid, details)),
                    others            => return Ok(others),
                };
B
Benjamin Sago 已提交
124 125
            }
            else {
B
Benjamin Sago 已提交
126
                return Ok(Mode::Details(details));
B
Benjamin Sago 已提交
127 128 129
            }
        }

130
        long_options_scan()?;
B
Benjamin Sago 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

        other_options_scan()
    }
}


/// The width of the terminal requested by the user.
#[derive(PartialEq, Debug)]
enum TerminalWidth {

    /// The user requested this specific number of columns.
    Set(usize),

    /// The terminal was found to have this number of columns.
    Terminal(usize),

    /// The user didn’t request any particular terminal width.
    Unset,
}

impl TerminalWidth {

    /// Determine a requested terminal width from the command-line arguments.
    ///
    /// Returns an error if a requested width doesn’t parse to an integer.
B
Benjamin Sago 已提交
156
    fn deduce() -> Result<TerminalWidth, Misfire> {
B
Benjamin Sago 已提交
157 158 159 160 161 162
        if let Some(columns) = var_os("COLUMNS").and_then(|s| s.into_string().ok()) {
            match columns.parse() {
                Ok(width)  => Ok(TerminalWidth::Set(width)),
                Err(e)     => Err(Misfire::FailedParse(e)),
            }
        }
163
        else if let Some(width) = *TERM_WIDTH {
B
Benjamin Sago 已提交
164 165 166 167 168 169 170
            Ok(TerminalWidth::Terminal(width))
        }
        else {
            Ok(TerminalWidth::Unset)
        }
    }

B
Benjamin Sago 已提交
171
    fn width(&self) -> Option<usize> {
B
Benjamin Sago 已提交
172
        match *self {
B
Benjamin Sago 已提交
173 174 175
            TerminalWidth::Set(width)       |
            TerminalWidth::Terminal(width)  => Some(width),
            TerminalWidth::Unset            => None,
B
Benjamin Sago 已提交
176 177 178 179 180
        }
    }
}


B
Benjamin Sago 已提交
181
impl TableOptions {
182
    fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
B
Benjamin Sago 已提交
183
        Ok(TableOptions {
184
            env:         Environment::load_all(),
B
Benjamin Sago 已提交
185
            time_format: TimeFormat::deduce(matches)?,
186 187
            size_format: SizeFormat::deduce(matches)?,
            time_types:  TimeTypes::deduce(matches)?,
B
Benjamin Sago 已提交
188 189 190 191 192
            inode:  matches.has(&flags::INODE)?,
            links:  matches.has(&flags::LINKS)?,
            blocks: matches.has(&flags::BLOCKS)?,
            group:  matches.has(&flags::GROUP)?,
            git:    cfg!(feature="git") && matches.has(&flags::GIT)?,
B
Benjamin Sago 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        })
    }
}


impl SizeFormat {

    /// Determine which file size to use in the file size column based on
    /// the user’s options.
    ///
    /// The default mode is to use the decimal prefixes, as they are the
    /// most commonly-understood, and don’t involve trying to parse large
    /// strings of digits in your head. Changing the format to anything else
    /// involves the `--binary` or `--bytes` flags, and these conflict with
    /// each other.
208
    fn deduce(matches: &MatchedFlags) -> Result<SizeFormat, Misfire> {
B
Benjamin Sago 已提交
209 210
        let binary = matches.has(&flags::BINARY)?;
        let bytes  = matches.has(&flags::BYTES)?;
B
Benjamin Sago 已提交
211 212

        match (binary, bytes) {
B
Benjamin Sago 已提交
213
            (true,  true )  => Err(Misfire::Conflict(&flags::BINARY, &flags::BYTES)),
B
Benjamin Sago 已提交
214 215 216 217 218 219 220 221
            (true,  false)  => Ok(SizeFormat::BinaryBytes),
            (false, true )  => Ok(SizeFormat::JustBytes),
            (false, false)  => Ok(SizeFormat::DecimalBytes),
        }
    }
}


222 223
const TIME_STYLES: &[&str] = &["default", "long-iso", "full-iso", "iso"];

B
Benjamin Sago 已提交
224 225 226
impl TimeFormat {

    /// Determine how time should be formatted in timestamp columns.
227
    fn deduce(matches: &MatchedFlags) -> Result<TimeFormat, Misfire> {
B
Benjamin Sago 已提交
228
        pub use output::time::{DefaultFormat, ISOFormat};
229

B
Benjamin Sago 已提交
230
        let word = match matches.get(&flags::TIME_STYLE)? {
B
Benjamin Sago 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
            Some(w) => w,
            None    => return Ok(TimeFormat::DefaultFormat(DefaultFormat::new())),
        };

        if word == "default" {
            Ok(TimeFormat::DefaultFormat(DefaultFormat::new()))
        }
        else if word == "iso" {
            Ok(TimeFormat::ISOFormat(ISOFormat::new()))
        }
        else if word == "long-iso" {
            Ok(TimeFormat::LongISO)
        }
        else if word == "full-iso" {
            Ok(TimeFormat::FullISO)
246 247
        }
        else {
248
            Err(Misfire::bad_argument(&flags::TIME_STYLE, word, TIME_STYLES))
249
        }
B
Benjamin Sago 已提交
250 251 252 253
    }
}


254 255
static TIMES: &[&str] = &["modified", "accessed", "created"];

B
Benjamin Sago 已提交
256 257 258 259 260 261 262 263 264 265 266 267
impl TimeTypes {

    /// Determine which of a file’s time fields should be displayed for it
    /// based on the user’s options.
    ///
    /// There are two separate ways to pick which fields to show: with a
    /// flag (such as `--modified`) or with a parameter (such as
    /// `--time=modified`). An error is signaled if both ways are used.
    ///
    /// It’s valid to show more than one column by passing in more than one
    /// option, but passing *no* options means that the user just wants to
    /// see the default set.
268
    fn deduce(matches: &MatchedFlags) -> Result<TimeTypes, Misfire> {
B
Benjamin Sago 已提交
269 270 271 272
        let possible_word = matches.get(&flags::TIME)?;
        let modified = matches.has(&flags::MODIFIED)?;
        let created  = matches.has(&flags::CREATED)?;
        let accessed = matches.has(&flags::ACCESSED)?;
B
Benjamin Sago 已提交
273 274 275

        if let Some(word) = possible_word {
            if modified {
B
Benjamin Sago 已提交
276
                Err(Misfire::Useless(&flags::MODIFIED, true, &flags::TIME))
B
Benjamin Sago 已提交
277 278
            }
            else if created {
B
Benjamin Sago 已提交
279
                Err(Misfire::Useless(&flags::CREATED, true, &flags::TIME))
B
Benjamin Sago 已提交
280 281
            }
            else if accessed {
B
Benjamin Sago 已提交
282
                Err(Misfire::Useless(&flags::ACCESSED, true, &flags::TIME))
B
Benjamin Sago 已提交
283
            }
B
Benjamin Sago 已提交
284
            else if word == "mod" || word == "modified" {
B
Benjamin Sago 已提交
285 286 287 288 289 290 291 292 293 294
                Ok(TimeTypes { accessed: false, modified: true,  created: false })
            }
            else if word == "acc" || word == "accessed" {
                Ok(TimeTypes { accessed: true,  modified: false, created: false })
            }
            else if word == "cr" || word == "created" {
                Ok(TimeTypes { accessed: false, modified: false, created: true  })
            }
            else {
                Err(Misfire::bad_argument(&flags::TIME, word, TIMES))
B
Benjamin Sago 已提交
295 296 297
            }
        }
        else if modified || created || accessed {
298
            Ok(TimeTypes { accessed, modified, created })
B
Benjamin Sago 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        }
        else {
            Ok(TimeTypes::default())
        }
    }
}


/// Under what circumstances we should display coloured, rather than plain,
/// output to the terminal.
///
/// By default, we want to display the colours when stdout can display them.
/// Turning them on when output is going to, say, a pipe, would make programs
/// such as `grep` or `more` not work properly. So the `Automatic` mode does
/// this check and only displays colours when they can be truly appreciated.
#[derive(PartialEq, Debug)]
enum TerminalColours {

    /// Display them even when output isn’t going to a terminal.
    Always,

    /// Display them when output is going to a terminal, but not otherwise.
    Automatic,

    /// Never display them, even when output is going to a terminal.
    Never,
}

impl Default for TerminalColours {
    fn default() -> TerminalColours {
        TerminalColours::Automatic
    }
}

B
Benjamin Sago 已提交
333 334
const COLOURS: &[&str] = &["always", "auto", "never"];

B
Benjamin Sago 已提交
335 336 337
impl TerminalColours {

    /// Determine which terminal colour conditions to use.
338
    fn deduce(matches: &MatchedFlags) -> Result<TerminalColours, Misfire> {
339

B
Benjamin Sago 已提交
340
        let word = match matches.get_where(|f| f.matches(&flags::COLOR) || f.matches(&flags::COLOUR))? {
B
Benjamin Sago 已提交
341 342 343 344 345 346 347 348 349 350 351 352
            Some(w) => w,
            None    => return Ok(TerminalColours::default()),
        };

        if word == "always" {
            Ok(TerminalColours::Always)
        }
        else if word == "auto" || word == "automatic" {
            Ok(TerminalColours::Automatic)
        }
        else if word == "never" {
            Ok(TerminalColours::Never)
B
Benjamin Sago 已提交
353 354
        }
        else {
B
Benjamin Sago 已提交
355
            Err(Misfire::bad_argument(&flags::COLOR, word, COLOURS))
B
Benjamin Sago 已提交
356 357 358
        }
    }
}
359 360


361
impl Colours {
362
    fn deduce(matches: &MatchedFlags) -> Result<Colours, Misfire> {
363 364 365 366
        use self::TerminalColours::*;

        let tc = TerminalColours::deduce(matches)?;
        if tc == Always || (tc == Automatic && TERM_WIDTH.is_some()) {
B
Benjamin Sago 已提交
367
            let scale = matches.has(&flags::COLOR_SCALE)? || matches.has(&flags::COLOUR_SCALE)?;
368 369 370 371 372 373 374 375 376
            Ok(Colours::colourful(scale))
        }
        else {
            Ok(Colours::plain())
        }
    }
}


377

378
impl FileStyle {
B
Benjamin Sago 已提交
379 380
    fn deduce(matches: &MatchedFlags) -> Result<FileStyle, Misfire> {
        let classify = Classify::deduce(matches)?;
381
        let exts = FileExtensions;
B
Benjamin Sago 已提交
382
        Ok(FileStyle { classify, exts })
383 384 385
    }
}

386
impl Classify {
B
Benjamin Sago 已提交
387 388 389 390 391
    fn deduce(matches: &MatchedFlags) -> Result<Classify, Misfire> {
        let flagged = matches.has(&flags::CLASSIFY)?;

        Ok(if flagged { Classify::AddFileIndicators }
                 else { Classify::JustFilenames })
392 393
    }
}
394 395 396 397 398 399 400


// Gets, then caches, the width of the terminal that exa is running in.
// This gets used multiple times above, with no real guarantee of order,
// so it’s easier to just cache it the first time it runs.
lazy_static! {
    static ref TERM_WIDTH: Option<usize> = {
401 402 403 404 405
        // All of stdin, stdout, and stderr could not be connected to a
        // terminal, but we’re only interested in stdout because it’s
        // where the output goes.
        use term_size::dimensions_stdout;
        dimensions_stdout().map(|t| t.0)
406 407
    };
}
B
Benjamin Sago 已提交
408 409 410 411 412 413



#[cfg(test)]
mod test {
    use super::*;
B
Benjamin Sago 已提交
414
    use std::ffi::OsString;
B
Benjamin Sago 已提交
415
    use options::flags;
B
Benjamin Sago 已提交
416 417 418 419
    use options::parser::{Flag, Arg};

    use options::test::parse_for_test;
    use options::test::Strictnesses::*;
B
Benjamin Sago 已提交
420 421 422 423 424 425

    pub fn os(input: &'static str) -> OsString {
        let mut os = OsString::new();
        os.push(input);
        os
    }
B
Benjamin Sago 已提交
426

B
Benjamin Sago 已提交
427 428 429 430
    static TEST_ARGS: &[&Arg] = &[ &flags::BINARY, &flags::BYTES,    &flags::TIME_STYLE,
                                   &flags::TIME,   &flags::MODIFIED, &flags::CREATED, &flags::ACCESSED,
                                   &flags::COLOR,  &flags::COLOUR ];

B
Benjamin Sago 已提交
431
    macro_rules! test {
B
Benjamin Sago 已提交
432
        ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => $result:expr) => {
B
Benjamin Sago 已提交
433 434
            #[test]
            fn $name() {
435 436 437
                for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
                    assert_eq!(result, $result);
                }
B
Benjamin Sago 已提交
438 439
            }
        };
B
Benjamin Sago 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452

        ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => like $pat:pat) => {
            #[test]
            fn $name() {
                for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
                    println!("Testing {:?}", result);
                    match result {
                        $pat => assert!(true),
                        _    => assert!(false),
                    }
                }
            }
        };
B
Benjamin Sago 已提交
453 454
    }

455

B
Benjamin Sago 已提交
456 457 458
    mod size_formats {
        use super::*;

B
Benjamin Sago 已提交
459
        // Default behaviour
B
Benjamin Sago 已提交
460
        test!(empty:   SizeFormat <- [];                       Both => Ok(SizeFormat::DecimalBytes));
B
Benjamin Sago 已提交
461 462

        // Individual flags
B
Benjamin Sago 已提交
463 464
        test!(binary:  SizeFormat <- ["--binary"];             Both => Ok(SizeFormat::BinaryBytes));
        test!(bytes:   SizeFormat <- ["--bytes"];              Both => Ok(SizeFormat::JustBytes));
B
Benjamin Sago 已提交
465 466

        // Errors
B
Benjamin Sago 已提交
467
        test!(both:    SizeFormat <- ["--binary", "--bytes"];  Both => Err(Misfire::Conflict(&flags::BINARY, &flags::BYTES)));
B
Benjamin Sago 已提交
468
    }
469 470


B
Benjamin Sago 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    mod time_formats {
        use super::*;

        // These tests use pattern matching because TimeFormat doesn’t
        // implement PartialEq.

        // Default behaviour
        test!(empty:     TimeFormat <- [];                            Both => like Ok(TimeFormat::DefaultFormat(_)));

        // Individual settings
        test!(default:   TimeFormat <- ["--time-style=default"];      Both => like Ok(TimeFormat::DefaultFormat(_)));
        test!(iso:       TimeFormat <- ["--time-style", "iso"];       Both => like Ok(TimeFormat::ISOFormat(_)));
        test!(long_iso:  TimeFormat <- ["--time-style=long-iso"];     Both => like Ok(TimeFormat::LongISO));
        test!(full_iso:  TimeFormat <- ["--time-style", "full-iso"];  Both => like Ok(TimeFormat::FullISO));

        // Overriding
        test!(actually:  TimeFormat <- ["--time-style=default",     "--time-style", "iso"];    Last => like Ok(TimeFormat::ISOFormat(_)));
        test!(actual_2:  TimeFormat <- ["--time-style=default",     "--time-style", "iso"];    Complain => like Err(Misfire::Duplicate(Flag::Long("time-style"), Flag::Long("time-style"))));

        test!(nevermind: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"];  Last => like Ok(TimeFormat::FullISO));
        test!(nevermore: TimeFormat <- ["--time-style", "long-iso", "--time-style=full-iso"];  Complain => like Err(Misfire::Duplicate(Flag::Long("time-style"), Flag::Long("time-style"))));

        // Errors
        test!(daily:     TimeFormat <- ["--time-style=24-hour"];      Both => like Err(Misfire::BadArgument(_, _, _)));
    }


498 499 500 501
    mod time_types {
        use super::*;

        // Default behaviour
B
Benjamin Sago 已提交
502
        test!(empty:     TimeTypes <- [];                      Both => Ok(TimeTypes::default()));
503 504

        // Modified
B
Benjamin Sago 已提交
505 506 507 508 509
        test!(modified:  TimeTypes <- ["--modified"];          Both => Ok(TimeTypes { accessed: false,  modified: true,   created: false }));
        test!(m:         TimeTypes <- ["-m"];                  Both => Ok(TimeTypes { accessed: false,  modified: true,   created: false }));
        test!(time_mod:  TimeTypes <- ["--time=modified"];     Both => Ok(TimeTypes { accessed: false,  modified: true,   created: false }));
        test!(time_m:    TimeTypes <- ["-tmod"];               Both => Ok(TimeTypes { accessed: false,  modified: true,   created: false }));

510
        // Accessed
B
Benjamin Sago 已提交
511 512 513 514 515
        test!(acc:       TimeTypes <- ["--accessed"];          Both => Ok(TimeTypes { accessed: true,   modified: false,  created: false }));
        test!(a:         TimeTypes <- ["-u"];                  Both => Ok(TimeTypes { accessed: true,   modified: false,  created: false }));
        test!(time_acc:  TimeTypes <- ["--time", "accessed"];  Both => Ok(TimeTypes { accessed: true,   modified: false,  created: false }));
        test!(time_a:    TimeTypes <- ["-t", "acc"];           Both => Ok(TimeTypes { accessed: true,   modified: false,  created: false }));

516
        // Created
B
Benjamin Sago 已提交
517 518 519 520
        test!(cr:        TimeTypes <- ["--created"];           Both => Ok(TimeTypes { accessed: false,  modified: false,  created: true  }));
        test!(c:         TimeTypes <- ["-U"];                  Both => Ok(TimeTypes { accessed: false,  modified: false,  created: true  }));
        test!(time_cr:   TimeTypes <- ["--time=created"];      Both => Ok(TimeTypes { accessed: false,  modified: false,  created: true  }));
        test!(time_c:    TimeTypes <- ["-tcr"];                Both => Ok(TimeTypes { accessed: false,  modified: false,  created: true  }));
521 522

        // Multiples
B
Benjamin Sago 已提交
523 524 525 526 527
        test!(time_uu:   TimeTypes <- ["-uU"];                 Both => Ok(TimeTypes { accessed: true,   modified: false,  created: true  }));

        // Errors
        test!(time_tea:  TimeTypes <- ["--time=tea"];          Both => Err(Misfire::bad_argument(&flags::TIME, &os("tea"), super::TIMES)));
        test!(time_ea:   TimeTypes <- ["-tea"];                Both => Err(Misfire::bad_argument(&flags::TIME, &os("ea"), super::TIMES)));
528 529

        // Overriding
B
Benjamin Sago 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
        test!(overridden:   TimeTypes <- ["-tcr", "-tmod"];    Last => Ok(TimeTypes { accessed: false,  modified: true,   created: false }));
        test!(overridden_2: TimeTypes <- ["-tcr", "-tmod"];    Complain => Err(Misfire::Duplicate(Flag::Short(b't'), Flag::Short(b't'))));
    }


    mod colourses {
        use super::*;

        // Default
        test!(empty:        TerminalColours <- [];                     Both => Ok(TerminalColours::default()));

        // --colour
        test!(u_always:     TerminalColours <- ["--colour=always"];    Both => Ok(TerminalColours::Always));
        test!(u_auto:       TerminalColours <- ["--colour", "auto"];   Both => Ok(TerminalColours::Automatic));
        test!(u_never:      TerminalColours <- ["--colour=never"];     Both => Ok(TerminalColours::Never));

        // --color
        test!(no_u_always:  TerminalColours <- ["--color", "always"];  Both => Ok(TerminalColours::Always));
        test!(no_u_auto:    TerminalColours <- ["--color=auto"];       Both => Ok(TerminalColours::Automatic));
        test!(no_u_never:   TerminalColours <- ["--color", "never"];   Both => Ok(TerminalColours::Never));
550 551

        // Errors
B
Benjamin Sago 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564
        test!(no_u_error:   TerminalColours <- ["--color=upstream"];   Both => Err(Misfire::bad_argument(&flags::COLOR, &os("upstream"), super::COLOURS)));  // the error is for --color
        test!(u_error:      TerminalColours <- ["--colour=lovers"];    Both => Err(Misfire::bad_argument(&flags::COLOR, &os("lovers"),   super::COLOURS)));  // and so is this one!

        // Overriding
        test!(overridden_1: TerminalColours <- ["--colour=auto", "--colour=never"];  Last => Ok(TerminalColours::Never));
        test!(overridden_2: TerminalColours <- ["--color=auto",  "--colour=never"];  Last => Ok(TerminalColours::Never));
        test!(overridden_3: TerminalColours <- ["--colour=auto", "--color=never"];   Last => Ok(TerminalColours::Never));
        test!(overridden_4: TerminalColours <- ["--color=auto",  "--color=never"];   Last => Ok(TerminalColours::Never));

        test!(overridden_5: TerminalColours <- ["--colour=auto", "--colour=never"];  Complain => Err(Misfire::Duplicate(Flag::Long("colour"), Flag::Long("colour"))));
        test!(overridden_6: TerminalColours <- ["--color=auto",  "--colour=never"];  Complain => Err(Misfire::Duplicate(Flag::Long("color"),  Flag::Long("colour"))));
        test!(overridden_7: TerminalColours <- ["--colour=auto", "--color=never"];   Complain => Err(Misfire::Duplicate(Flag::Long("colour"), Flag::Long("color"))));
        test!(overridden_8: TerminalColours <- ["--color=auto",  "--color=never"];   Complain => Err(Misfire::Duplicate(Flag::Long("color"),  Flag::Long("color"))));
565
    }
B
Benjamin Sago 已提交
566
}