Skip to content

Commit 148d6a2

Browse files
committed
more: refactor options structure
1 parent b009cae commit 148d6a2

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

src/uu/more/src/more.rs

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const ABOUT: &str = help_about!("more.md");
2929
const USAGE: &str = help_usage!("more.md");
3030
const BELL: &str = "\x07";
3131

32+
const MULTI_FILE_TOP_PROMPT: &str = "\r::::::::::::::\n\r{}\n\r::::::::::::::\n";
33+
3234
pub mod options {
3335
pub const SILENT: &str = "silent";
3436
pub const LOGICAL: &str = "logical";
@@ -44,16 +46,16 @@ pub mod options {
4446
pub const FILES: &str = "files";
4547
}
4648

47-
const MULTI_FILE_TOP_PROMPT: &str = "\r::::::::::::::\n\r{}\n\r::::::::::::::\n";
48-
4949
struct Options {
50+
silent: bool,
51+
_logical: bool, // not implemented
52+
_no_pause: bool, // not implemented
53+
print_over: bool,
5054
clean_print: bool,
51-
from_line: usize,
55+
squeeze: bool,
5256
lines: Option<u16>,
57+
from_line: usize,
5358
pattern: Option<String>,
54-
print_over: bool,
55-
silent: bool,
56-
squeeze: bool,
5759
}
5860

5961
impl Options {
@@ -64,25 +66,24 @@ impl Options {
6466
) {
6567
// We add 1 to the number of lines to display because the last line
6668
// is used for the banner
67-
(Some(number), _) if number > 0 => Some(number + 1),
68-
(None, Some(number)) if number > 0 => Some(number + 1),
69-
(_, _) => None,
69+
(Some(n), _) | (None, Some(n)) if n > 0 => Some(n + 1),
70+
_ => None, // Use terminal height
7071
};
7172
let from_line = match matches.get_one::<usize>(options::FROM_LINE).copied() {
72-
Some(number) if number > 1 => number - 1,
73+
Some(number) => number.saturating_sub(1),
7374
_ => 0,
7475
};
75-
let pattern = matches
76-
.get_one::<String>(options::PATTERN)
77-
.map(|s| s.to_owned());
76+
let pattern = matches.get_one::<String>(options::PATTERN).cloned();
7877
Self {
78+
silent: matches.get_flag(options::SILENT),
79+
_logical: matches.get_flag(options::LOGICAL),
80+
_no_pause: matches.get_flag(options::NO_PAUSE),
81+
print_over: matches.get_flag(options::PRINT_OVER),
7982
clean_print: matches.get_flag(options::CLEAN_PRINT),
80-
from_line,
83+
squeeze: matches.get_flag(options::SQUEEZE),
8184
lines,
85+
from_line,
8286
pattern,
83-
print_over: matches.get_flag(options::PRINT_OVER),
84-
silent: matches.get_flag(options::SILENT),
85-
squeeze: matches.get_flag(options::SQUEEZE),
8687
}
8788
}
8889
}
@@ -176,58 +177,55 @@ pub fn uu_app() -> Command {
176177
.override_usage(format_usage(USAGE))
177178
.version(uucore::crate_version!())
178179
.infer_long_args(true)
179-
.arg(
180-
Arg::new(options::PRINT_OVER)
181-
.short('c')
182-
.long(options::PRINT_OVER)
183-
.help("Do not scroll, display text and clean line ends")
184-
.action(ArgAction::SetTrue),
185-
)
186180
.arg(
187181
Arg::new(options::SILENT)
188182
.short('d')
189183
.long(options::SILENT)
190-
.help("Display help instead of ringing bell")
191-
.action(ArgAction::SetTrue),
184+
.action(ArgAction::SetTrue)
185+
.help("Display help instead of ringing bell when an illegal key is pressed."),
192186
)
187+
// .arg(
188+
// Arg::new(options::LOGICAL)
189+
// .short('f')
190+
// .long(options::LOGICAL)
191+
// .action(ArgAction::SetTrue)
192+
// .help("Do not pause after form feed"),
193+
// )
194+
// .arg(
195+
// Arg::new(options::NO_PAUSE)
196+
// .short('l')
197+
// .long(options::NO_PAUSE)
198+
// .action(ArgAction::SetTrue)
199+
// .help("Count logical lines rather than screen lines"),
200+
// )
193201
.arg(
194-
Arg::new(options::CLEAN_PRINT)
202+
Arg::new(options::PRINT_OVER)
195203
.short('p')
204+
.long(options::PRINT_OVER)
205+
.action(ArgAction::SetTrue)
206+
.help("Do not scroll, clean screen and display text"),
207+
)
208+
.arg(
209+
Arg::new(options::CLEAN_PRINT)
210+
.short('c')
196211
.long(options::CLEAN_PRINT)
197-
.help("Do not scroll, clean screen and display text")
198-
.action(ArgAction::SetTrue),
212+
.action(ArgAction::SetTrue)
213+
.help("Do not scroll, display text and clean line ends"),
199214
)
200215
.arg(
201216
Arg::new(options::SQUEEZE)
202217
.short('s')
203218
.long(options::SQUEEZE)
204-
.help("Squeeze multiple blank lines into one")
205-
.action(ArgAction::SetTrue),
219+
.action(ArgAction::SetTrue)
220+
.help("Squeeze multiple blank lines into one"),
206221
)
207222
.arg(
208223
Arg::new(options::PLAIN)
209224
.short('u')
210225
.long(options::PLAIN)
211226
.action(ArgAction::SetTrue)
212-
.hide(true),
213-
)
214-
.arg(
215-
Arg::new(options::PATTERN)
216-
.short('P')
217-
.long(options::PATTERN)
218-
.allow_hyphen_values(true)
219-
.required(false)
220-
.value_name("pattern")
221-
.help("Display file beginning from pattern match"),
222-
)
223-
.arg(
224-
Arg::new(options::FROM_LINE)
225-
.short('F')
226-
.long(options::FROM_LINE)
227-
.num_args(1)
228-
.value_name("number")
229-
.value_parser(value_parser!(usize))
230-
.help("Display file beginning from line number"),
227+
.hide(true)
228+
.help("Suppress underlining"),
231229
)
232230
.arg(
233231
Arg::new(options::LINES)
@@ -243,23 +241,26 @@ pub fn uu_app() -> Command {
243241
.long(options::NUMBER)
244242
.num_args(1)
245243
.value_parser(value_parser!(u16).range(0..))
246-
.help("Same as --lines"),
244+
.help("Same as --lines option argument"),
247245
)
248-
// The commented arguments below are unimplemented:
249-
/*
250246
.arg(
251-
Arg::new(options::LOGICAL)
252-
.short('f')
253-
.long(options::LOGICAL)
254-
.help("Count logical rather than screen lines"),
247+
Arg::new(options::FROM_LINE)
248+
.short('F')
249+
.long(options::FROM_LINE)
250+
.num_args(1)
251+
.value_name("number")
252+
.value_parser(value_parser!(usize))
253+
.help("Start displaying each file at line number"),
255254
)
256255
.arg(
257-
Arg::new(options::NO_PAUSE)
258-
.short('l')
259-
.long(options::NO_PAUSE)
260-
.help("Suppress pause after form feed"),
256+
Arg::new(options::PATTERN)
257+
.short('P')
258+
.long(options::PATTERN)
259+
.allow_hyphen_values(true)
260+
.required(false)
261+
.value_name("pattern")
262+
.help("The string to be searched in each file before starting to display it"),
261263
)
262-
*/
263264
.arg(
264265
Arg::new(options::FILES)
265266
.required(false)
@@ -692,13 +693,15 @@ mod tests {
692693
Self {
693694
content: content.to_string(),
694695
options: Options {
696+
silent: false,
697+
_logical: false,
698+
_no_pause: false,
699+
print_over: false,
695700
clean_print: false,
696-
from_line: 0,
701+
squeeze: false,
697702
lines: None,
703+
from_line: 0,
698704
pattern: None,
699-
print_over: false,
700-
silent: false,
701-
squeeze: false,
702705
},
703706
rows: 24,
704707
next_file: None,

0 commit comments

Comments
 (0)