mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
more: implementing pattern option
This commit is contained in:
parent
1eaa87cd62
commit
fbd3fa3f21
1 changed files with 38 additions and 8 deletions
|
@ -54,6 +54,7 @@ struct Options {
|
||||||
clean_print: bool,
|
clean_print: bool,
|
||||||
from_line: usize,
|
from_line: usize,
|
||||||
lines: Option<u16>,
|
lines: Option<u16>,
|
||||||
|
pattern: Option<String>,
|
||||||
print_over: bool,
|
print_over: bool,
|
||||||
silent: bool,
|
silent: bool,
|
||||||
squeeze: bool,
|
squeeze: bool,
|
||||||
|
@ -75,10 +76,14 @@ impl Options {
|
||||||
Some(number) if number > 1 => number - 1,
|
Some(number) if number > 1 => number - 1,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
};
|
};
|
||||||
|
let pattern = matches
|
||||||
|
.get_one::<String>(options::PATTERN)
|
||||||
|
.map(|s| s.to_owned());
|
||||||
Self {
|
Self {
|
||||||
clean_print: matches.get_flag(options::CLEAN_PRINT),
|
clean_print: matches.get_flag(options::CLEAN_PRINT),
|
||||||
from_line,
|
from_line,
|
||||||
lines,
|
lines,
|
||||||
|
pattern,
|
||||||
print_over: matches.get_flag(options::PRINT_OVER),
|
print_over: matches.get_flag(options::PRINT_OVER),
|
||||||
silent: matches.get_flag(options::SILENT),
|
silent: matches.get_flag(options::SILENT),
|
||||||
squeeze: matches.get_flag(options::SQUEEZE),
|
squeeze: matches.get_flag(options::SQUEEZE),
|
||||||
|
@ -206,6 +211,15 @@ pub fn uu_app() -> Command {
|
||||||
.action(ArgAction::SetTrue)
|
.action(ArgAction::SetTrue)
|
||||||
.hide(true),
|
.hide(true),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new(options::PATTERN)
|
||||||
|
.short('P')
|
||||||
|
.long(options::PATTERN)
|
||||||
|
.allow_hyphen_values(true)
|
||||||
|
.required(false)
|
||||||
|
.value_name("pattern")
|
||||||
|
.help("Display file beginning from pattern match"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(options::FROM_LINE)
|
Arg::new(options::FROM_LINE)
|
||||||
.short('F')
|
.short('F')
|
||||||
|
@ -245,14 +259,6 @@ pub fn uu_app() -> Command {
|
||||||
.long(options::NO_PAUSE)
|
.long(options::NO_PAUSE)
|
||||||
.help("Suppress pause after form feed"),
|
.help("Suppress pause after form feed"),
|
||||||
)
|
)
|
||||||
.arg(
|
|
||||||
Arg::new(options::PATTERN)
|
|
||||||
.short('P')
|
|
||||||
.allow_hyphen_values(true)
|
|
||||||
.required(false)
|
|
||||||
.takes_value(true)
|
|
||||||
.help("Display file beginning from pattern match"),
|
|
||||||
)
|
|
||||||
*/
|
*/
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new(options::FILES)
|
Arg::new(options::FILES)
|
||||||
|
@ -307,6 +313,17 @@ fn more(
|
||||||
|
|
||||||
let mut pager = Pager::new(rows, lines, next_file, options);
|
let mut pager = Pager::new(rows, lines, next_file, options);
|
||||||
|
|
||||||
|
if options.pattern.is_some() {
|
||||||
|
match search_pattern_in_file(&pager.lines, &options.pattern) {
|
||||||
|
Some(number) => pager.upper_mark = number,
|
||||||
|
None => {
|
||||||
|
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine))?;
|
||||||
|
stdout.write_all("\rPattern not found\n".as_bytes())?;
|
||||||
|
pager.content_rows -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if multiple_file {
|
if multiple_file {
|
||||||
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
|
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
|
||||||
stdout.write_all(
|
stdout.write_all(
|
||||||
|
@ -592,6 +609,19 @@ impl<'a> Pager<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_pattern_in_file(lines: &[String], pattern: &Option<String>) -> Option<usize> {
|
||||||
|
let pattern = pattern.clone().unwrap_or_default();
|
||||||
|
if lines.is_empty() || pattern.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
for (line_number, line) in lines.iter().enumerate() {
|
||||||
|
if line.contains(pattern.as_str()) {
|
||||||
|
return Some(line_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn paging_add_back_message(options: &Options, stdout: &mut std::io::Stdout) -> UResult<()> {
|
fn paging_add_back_message(options: &Options, stdout: &mut std::io::Stdout) -> UResult<()> {
|
||||||
if options.lines.is_some() {
|
if options.lines.is_some() {
|
||||||
execute!(stdout, MoveUp(1))?;
|
execute!(stdout, MoveUp(1))?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue