Skip to content

Commit

Permalink
tail: Refactor Input::from parameter to use AsRef<OsStr>. Settings::i…
Browse files Browse the repository at this point in the history
…nputs now uses Vec<Input>
  • Loading branch information
Joining7943 committed Apr 9, 2023
1 parent 9ec15f1 commit b7588be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
26 changes: 10 additions & 16 deletions src/uu/tail/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command};
use fundu::DurationParser;
use is_terminal::IsTerminal;
use same_file::Handle;
use std::collections::VecDeque;
use std::ffi::OsString;
use std::time::Duration;
use uucore::error::{UResult, USimpleError, UUsageError};
Expand Down Expand Up @@ -141,7 +140,8 @@ pub struct Settings {
pub use_polling: bool,
pub verbose: bool,
pub presume_input_pipe: bool,
pub inputs: VecDeque<Input>,
/// `FILE(s)` positional arguments
pub inputs: Vec<Input>,
}

impl Default for Settings {
Expand Down Expand Up @@ -173,11 +173,11 @@ impl Settings {
}
settings.mode = FilterMode::from_obsolete_args(args);
let input = if let Some(name) = name {
Input::from(&name)
Input::from(name)
} else {
Input::default()
};
settings.inputs.push_back(input);
settings.inputs.push(input);
settings
}

Expand Down Expand Up @@ -257,19 +257,13 @@ impl Settings {
}
}

let mut inputs: VecDeque<Input> = matches
.get_many::<String>(options::ARG_FILES)
.map(|v| v.map(|string| Input::from(&string)).collect())
.unwrap_or_default();
settings.inputs = matches
.get_raw(options::ARG_FILES)
.map(|v| v.map(Input::from).collect())
.unwrap_or_else(|| vec![Input::default()]);

// apply default and add '-' to inputs if none is present
if inputs.is_empty() {
inputs.push_front(Input::default());
}

settings.verbose = inputs.len() > 1 && !matches.get_flag(options::verbosity::QUIET);

settings.inputs = inputs;
settings.verbose =
settings.inputs.len() > 1 && !matches.get_flag(options::verbosity::QUIET);

Ok(settings)
}
Expand Down
3 changes: 1 addition & 2 deletions src/uu/tail/src/follow/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::follow::files::{FileHandling, PathData};
use crate::paths::{Input, InputKind, MetadataExtTail, PathExtTail};
use crate::{platform, text};
use notify::{RecommendedWatcher, RecursiveMode, Watcher, WatcherKind};
use std::collections::VecDeque;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, channel, Receiver};
Expand Down Expand Up @@ -270,7 +269,7 @@ impl Observer {
self.follow_name() && self.retry
}

fn init_files(&mut self, inputs: &VecDeque<Input>) -> UResult<()> {
fn init_files(&mut self, inputs: &Vec<Input>) -> UResult<()> {
if let Some(watcher_rx) = &mut self.watcher_rx {
for input in inputs {
match input.kind() {
Expand Down
17 changes: 6 additions & 11 deletions src/uu/tail/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ pub struct Input {
}

impl Input {
pub fn from<T: AsRef<OsStr>>(string: &T) -> Self {
let kind = if string.as_ref() == Path::new(text::DASH) {
pub fn from<T: AsRef<OsStr>>(string: T) -> Self {
let string = string.as_ref();
let kind = if string == OsStr::new(text::DASH) {
InputKind::Stdin
} else {
InputKind::File(PathBuf::from(string.as_ref()))
InputKind::File(PathBuf::from(string))
};

let display_name = match kind {
InputKind::File(_) => string.as_ref().to_string_lossy().to_string(),
InputKind::Stdin => {
if cfg!(unix) {
text::STDIN_HEADER.to_string()
} else {
string.as_ref().to_string_lossy().to_string()
}
}
InputKind::File(_) => string.to_string_lossy().to_string(),
InputKind::Stdin => text::STDIN_HEADER.to_string(),
};

Self { kind, display_name }
Expand Down

0 comments on commit b7588be

Please sign in to comment.