mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 21:47:46 +00:00
more: simplify and fix logic for multiple files
This commit is contained in:
parent
1c2540a613
commit
101e55702c
1 changed files with 32 additions and 48 deletions
|
@ -136,7 +136,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if let Some(filenames) = matches.values_of(options::FILES) {
|
if let Some(filenames) = matches.values_of(options::FILES) {
|
||||||
let mut stdout = setup_term();
|
let mut stdout = setup_term();
|
||||||
let length = filenames.len();
|
let length = filenames.len();
|
||||||
for (idx, fname) in filenames.clone().enumerate() {
|
for (idx, fname) in filenames.enumerate() {
|
||||||
let fname = Path::new(fname);
|
let fname = Path::new(fname);
|
||||||
if fname.is_dir() {
|
if fname.is_dir() {
|
||||||
terminal::disable_raw_mode().unwrap();
|
terminal::disable_raw_mode().unwrap();
|
||||||
|
@ -145,20 +145,19 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
}
|
}
|
||||||
if !fname.exists() {
|
if !fname.exists() {
|
||||||
terminal::disable_raw_mode().unwrap();
|
terminal::disable_raw_mode().unwrap();
|
||||||
eprintln!(
|
show_error!(
|
||||||
"{}: cannot open {}: No such file or directory",
|
"cannot open {}: No such file or directory",
|
||||||
executable!(),
|
|
||||||
fname.display()
|
fname.display()
|
||||||
);
|
);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if filenames.len() > 1 {
|
if length > 1 {
|
||||||
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", fname.to_str().unwrap()));
|
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", fname.to_str().unwrap()));
|
||||||
}
|
}
|
||||||
let mut reader = BufReader::new(File::open(fname).unwrap());
|
let mut reader = BufReader::new(File::open(fname).unwrap());
|
||||||
reader.read_to_string(&mut buff).unwrap();
|
reader.read_to_string(&mut buff).unwrap();
|
||||||
let last = idx + 1 == length;
|
let is_last = idx + 1 == length;
|
||||||
more(&buff, &mut stdout, last);
|
more(&buff, &mut stdout, is_last);
|
||||||
buff.clear();
|
buff.clear();
|
||||||
}
|
}
|
||||||
reset_term(&mut stdout);
|
reset_term(&mut stdout);
|
||||||
|
@ -205,17 +204,9 @@ fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
||||||
let lines = break_buff(buff, usize::from(cols));
|
let lines = break_buff(buff, usize::from(cols));
|
||||||
let line_count: u16 = lines.len().try_into().unwrap();
|
let line_count: u16 = lines.len().try_into().unwrap();
|
||||||
|
|
||||||
// Print everything and quit if line count is less than the availale rows
|
|
||||||
if line_count < rows {
|
|
||||||
println!("{}", buff);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut upper_mark = 0;
|
let mut upper_mark = 0;
|
||||||
// Number of lines left
|
let mut lines_left = line_count.saturating_sub(upper_mark + rows);
|
||||||
let mut lines_left = line_count - (upper_mark + rows);
|
|
||||||
// Are we on the very last page and the next down arrow will return this function
|
|
||||||
let mut to_be_done = false;
|
|
||||||
draw(
|
draw(
|
||||||
&mut upper_mark,
|
&mut upper_mark,
|
||||||
rows,
|
rows,
|
||||||
|
@ -224,6 +215,16 @@ fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
||||||
line_count,
|
line_count,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut to_be_done = false;
|
||||||
|
if lines_left == 0 && is_last {
|
||||||
|
if is_last {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
to_be_done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if event::poll(Duration::from_millis(10)).unwrap() {
|
if event::poll(Duration::from_millis(10)).unwrap() {
|
||||||
match event::read().unwrap() {
|
match event::read().unwrap() {
|
||||||
|
@ -246,38 +247,21 @@ fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
||||||
code: KeyCode::Char(' '),
|
code: KeyCode::Char(' '),
|
||||||
modifiers: KeyModifiers::NONE,
|
modifiers: KeyModifiers::NONE,
|
||||||
}) => {
|
}) => {
|
||||||
// If this is the last page but some text
|
upper_mark = upper_mark.saturating_add(rows.saturating_sub(1));
|
||||||
// is still left that does not require a page of its own
|
lines_left = line_count.saturating_sub(upper_mark + rows);
|
||||||
// then this event will print the left lines
|
draw(
|
||||||
if lines_left < rows && !to_be_done {
|
&mut upper_mark,
|
||||||
for l in lines.iter().skip((line_count - upper_mark - rows).into()) {
|
rows,
|
||||||
stdout.write_all(format!("\r{}\n", l).as_bytes()).unwrap();
|
&mut stdout,
|
||||||
|
lines.clone(),
|
||||||
|
line_count,
|
||||||
|
);
|
||||||
|
|
||||||
|
if lines_left == 0 {
|
||||||
|
if to_be_done || is_last {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
make_prompt_and_flush(&mut stdout, line_count, line_count);
|
to_be_done = true;
|
||||||
// If this is not the last input file
|
|
||||||
// do not return, but the next down arrow must return
|
|
||||||
// because we have printed everyhing
|
|
||||||
if !is_last {
|
|
||||||
to_be_done = true;
|
|
||||||
} else {
|
|
||||||
// Else quit
|
|
||||||
reset_term(&mut stdout);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// This handles the next arrow key to quit
|
|
||||||
} else if lines_left < rows && to_be_done {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
// Print a normal page
|
|
||||||
upper_mark = upper_mark.saturating_add(rows.saturating_sub(1));
|
|
||||||
lines_left = line_count.saturating_sub(upper_mark + rows);
|
|
||||||
draw(
|
|
||||||
&mut upper_mark,
|
|
||||||
rows,
|
|
||||||
&mut stdout,
|
|
||||||
lines.clone(),
|
|
||||||
line_count,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Key(KeyEvent {
|
Event::Key(KeyEvent {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue