Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/options/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,14 @@ impl clap::builder::TypedValueParser for TimeFormatParser {
_arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, Error> {
match TimeFormat::try_from_str(value.to_str().unwrap()) {
let s = value.to_str().ok_or_else(|| {
Error::raw(
clap::error::ErrorKind::InvalidUtf8,
format!("--time-style value '{:?}' is not valid UTF-8", value),
)
.with_cmd(cmd)
})?;
match TimeFormat::try_from_str(s) {
Err(s) => Err(Error::raw(clap::error::ErrorKind::InvalidValue, s).with_cmd(cmd)),
Ok(v) => Ok(v),
}
Expand Down Expand Up @@ -346,4 +353,15 @@ pub mod test {
["file1", "file2"]
);
}

#[test]
fn time_style_rejects_non_utf8() {
use std::os::unix::ffi::OsStringExt;
let args = vec![
OsString::from("--time-style"),
OsString::from_vec(b"\xff\xfe".to_vec()),
];
let err = mock_cli_try(args).unwrap_err();
assert_eq!(err.kind(), clap::error::ErrorKind::InvalidUtf8);
}
}
4 changes: 3 additions & 1 deletion src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::output::table::{
use crate::output::time::TimeFormat;
use crate::output::{Mode, TerminalWidth, View, details, grid};

use std::io::{self, IsTerminal};

use super::parser::{ColorScaleArgs, TimeArgs};

impl View {
Expand All @@ -30,7 +32,7 @@ impl View {
strict: bool,
) -> Result<Self, OptionsError> {
let width = TerminalWidth::deduce(matches, vars)?;
let is_tty = width.actual_terminal_width().is_some();
let is_tty = io::stdout().is_terminal();
let mode = Mode::deduce(matches, vars, is_tty, strict)?;
let deref_links = matches.get_flag("dereference");
let follow_links = matches.get_flag("follow-symlinks");
Expand Down
Loading