Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more: use UResult #2767

Merged
merged 1 commit into from
Dec 15, 2021
Merged
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
33 changes: 18 additions & 15 deletions src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

// spell-checker:ignore (methods) isnt

#[macro_use]
extern crate uucore;

use std::{
fs::File,
io::{stdin, stdout, BufReader, Read, Stdout, Write},
Expand All @@ -31,6 +28,7 @@ use crossterm::{
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError, UUsageError};

const BELL: &str = "\x07";

Expand All @@ -51,7 +49,8 @@ pub mod options {

const MULTI_FILE_TOP_PROMPT: &str = "::::::::::::::\n{}\n::::::::::::::\n";

pub fn uumain(args: impl uucore::Args) -> i32 {
#[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args);

let mut buff = String::new();
Expand All @@ -65,32 +64,36 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let file = Path::new(file);
if file.is_dir() {
terminal::disable_raw_mode().unwrap();
show_usage_error!("{} is a directory.", file.quote());
return 1;
return Err(UUsageError::new(
1,
format!("{} is a directory.", file.quote()),
));
}
if !file.exists() {
terminal::disable_raw_mode().unwrap();
show_error!("cannot open {}: No such file or directory", file.quote());
return 1;
return Err(USimpleError::new(
1,
format!("cannot open {}: No such file or directory", file.quote()),
));
}
if length > 1 {
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", file.to_str().unwrap()));
}
let mut reader = BufReader::new(File::open(file).unwrap());
reader.read_to_string(&mut buff).unwrap();
more(&buff, &mut stdout, next_file.copied(), silent);
more(&buff, &mut stdout, next_file.copied(), silent)?;
buff.clear();
}
reset_term(&mut stdout);
} else if atty::isnt(atty::Stream::Stdin) {
stdin().read_to_string(&mut buff).unwrap();
let mut stdout = setup_term();
more(&buff, &mut stdout, None, silent);
more(&buff, &mut stdout, None, silent)?;
reset_term(&mut stdout);
} else {
show_usage_error!("bad usage");
return Err(UUsageError::new(1, "bad usage"));
}
0
Ok(())
}

pub fn uu_app() -> App<'static, 'static> {
Expand Down Expand Up @@ -210,14 +213,14 @@ fn reset_term(stdout: &mut std::io::Stdout) {
#[inline(always)]
fn reset_term(_: &mut usize) {}

fn more(buff: &str, stdout: &mut Stdout, next_file: Option<&str>, silent: bool) {
fn more(buff: &str, stdout: &mut Stdout, next_file: Option<&str>, silent: bool) -> UResult<()> {
let (cols, rows) = terminal::size().unwrap();
let lines = break_buff(buff, usize::from(cols));

let mut pager = Pager::new(rows, lines, next_file, silent);
pager.draw(stdout, None);
if pager.should_close() {
return;
return Ok(());
}

loop {
Expand All @@ -244,7 +247,7 @@ fn more(buff: &str, stdout: &mut Stdout, next_file: Option<&str>, silent: bool)
modifiers: KeyModifiers::NONE,
}) => {
if pager.should_close() {
return;
return Ok(());
} else {
pager.page_down();
}
Expand Down