mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #2357 from deantvv/more-show-next-file
more: Show next file at bottom line
This commit is contained in:
commit
6324df890f
1 changed files with 23 additions and 9 deletions
|
@ -143,7 +143,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if let Some(files) = matches.values_of(options::FILES) {
|
if let Some(files) = matches.values_of(options::FILES) {
|
||||||
let mut stdout = setup_term();
|
let mut stdout = setup_term();
|
||||||
let length = files.len();
|
let length = files.len();
|
||||||
for (idx, file) in files.enumerate() {
|
|
||||||
|
let mut files_iter = files.peekable();
|
||||||
|
while let (Some(file), next_file) = (files_iter.next(), files_iter.peek()) {
|
||||||
let file = Path::new(file);
|
let file = Path::new(file);
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
terminal::disable_raw_mode().unwrap();
|
terminal::disable_raw_mode().unwrap();
|
||||||
|
@ -160,15 +162,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
}
|
}
|
||||||
let mut reader = BufReader::new(File::open(file).unwrap());
|
let mut reader = BufReader::new(File::open(file).unwrap());
|
||||||
reader.read_to_string(&mut buff).unwrap();
|
reader.read_to_string(&mut buff).unwrap();
|
||||||
let is_last = idx + 1 == length;
|
more(&buff, &mut stdout, next_file.copied());
|
||||||
more(&buff, &mut stdout, is_last);
|
|
||||||
buff.clear();
|
buff.clear();
|
||||||
}
|
}
|
||||||
reset_term(&mut stdout);
|
reset_term(&mut stdout);
|
||||||
} else if atty::isnt(atty::Stream::Stdin) {
|
} else if atty::isnt(atty::Stream::Stdin) {
|
||||||
stdin().read_to_string(&mut buff).unwrap();
|
stdin().read_to_string(&mut buff).unwrap();
|
||||||
let mut stdout = setup_term();
|
let mut stdout = setup_term();
|
||||||
more(&buff, &mut stdout, true);
|
more(&buff, &mut stdout, None);
|
||||||
reset_term(&mut stdout);
|
reset_term(&mut stdout);
|
||||||
} else {
|
} else {
|
||||||
show_usage_error!("bad usage");
|
show_usage_error!("bad usage");
|
||||||
|
@ -203,7 +204,7 @@ fn reset_term(stdout: &mut std::io::Stdout) {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn reset_term(_: &mut usize) {}
|
fn reset_term(_: &mut usize) {}
|
||||||
|
|
||||||
fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
fn more(buff: &str, mut stdout: &mut Stdout, next_file: Option<&str>) {
|
||||||
let (cols, rows) = terminal::size().unwrap();
|
let (cols, rows) = terminal::size().unwrap();
|
||||||
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();
|
||||||
|
@ -217,8 +218,11 @@ fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
||||||
&mut stdout,
|
&mut stdout,
|
||||||
lines.clone(),
|
lines.clone(),
|
||||||
line_count,
|
line_count,
|
||||||
|
next_file,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let is_last = next_file.is_none();
|
||||||
|
|
||||||
// Specifies whether we have reached the end of the file and should
|
// Specifies whether we have reached the end of the file and should
|
||||||
// return on the next key press. However, we immediately return when
|
// return on the next key press. However, we immediately return when
|
||||||
// this is the last file.
|
// this is the last file.
|
||||||
|
@ -270,6 +274,7 @@ fn more(buff: &str, mut stdout: &mut Stdout, is_last: bool) {
|
||||||
&mut stdout,
|
&mut stdout,
|
||||||
lines.clone(),
|
lines.clone(),
|
||||||
line_count,
|
line_count,
|
||||||
|
next_file,
|
||||||
);
|
);
|
||||||
|
|
||||||
if lines_left == 0 {
|
if lines_left == 0 {
|
||||||
|
@ -288,6 +293,7 @@ fn draw(
|
||||||
mut stdout: &mut std::io::Stdout,
|
mut stdout: &mut std::io::Stdout,
|
||||||
lines: Vec<String>,
|
lines: Vec<String>,
|
||||||
lc: u16,
|
lc: u16,
|
||||||
|
next_file: Option<&str>,
|
||||||
) {
|
) {
|
||||||
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
|
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
|
||||||
let (up_mark, lower_mark) = calc_range(*upper_mark, rows, lc);
|
let (up_mark, lower_mark) = calc_range(*upper_mark, rows, lc);
|
||||||
|
@ -302,7 +308,7 @@ fn draw(
|
||||||
.write_all(format!("\r{}\n", line).as_bytes())
|
.write_all(format!("\r{}\n", line).as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
make_prompt_and_flush(&mut stdout, lower_mark, lc);
|
make_prompt_and_flush(&mut stdout, lower_mark, lc, next_file);
|
||||||
*upper_mark = up_mark;
|
*upper_mark = up_mark;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,12 +364,20 @@ fn calc_range(mut upper_mark: u16, rows: u16, line_count: u16) -> (u16, u16) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a prompt similar to original more
|
// Make a prompt similar to original more
|
||||||
fn make_prompt_and_flush(stdout: &mut Stdout, lower_mark: u16, lc: u16) {
|
fn make_prompt_and_flush(stdout: &mut Stdout, lower_mark: u16, lc: u16, next_file: Option<&str>) {
|
||||||
|
let status = if lower_mark == lc {
|
||||||
|
format!("Next file: {}", next_file.unwrap_or_default())
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"{}%",
|
||||||
|
(lower_mark as f64 / lc as f64 * 100.0).round() as u16
|
||||||
|
)
|
||||||
|
};
|
||||||
write!(
|
write!(
|
||||||
stdout,
|
stdout,
|
||||||
"\r{}--More--({}%){}",
|
"\r{}--More--({}){}",
|
||||||
Attribute::Reverse,
|
Attribute::Reverse,
|
||||||
((lower_mark as f64 / lc as f64) * 100.0).round() as u16,
|
status,
|
||||||
Attribute::Reset
|
Attribute::Reset
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue