Skip to content

Commit 1e43436

Browse files
Update per code review
1 parent 4867f17 commit 1e43436

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

text/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ deunicode = "1.6"
2222
walkdir = "2"
2323

2424
[dev-dependencies]
25-
proptest = "1.5.0"
26-
rand = "0.8.5"
25+
proptest = "1"
26+
rand = "0.8"
2727

2828
[lints]
2929
workspace = true

text/head.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct Args {
2929
// https://pubs.opengroup.org/onlinepubs/9799919799/utilities/head.html
3030
//
3131
/// The first <N> bytes of each input file shall be copied to standard output (mutually exclusive with -n)
32-
#[arg(long = "bytes", short, value_parser = clap::value_parser!(usize), group = N_C_GROUP)]
33-
c: Option<usize>,
32+
#[arg(long = "bytes", short = 'c', value_parser = clap::value_parser!(usize), group = N_C_GROUP)]
33+
bytes_to_copy: Option<usize>,
3434

3535
/// Files to read as input.
3636
files: Vec<PathBuf>,
@@ -65,8 +65,8 @@ fn head_file(
6565
let mut raw_buffer = [0_u8; BUFFER_SIZE];
6666

6767
match *count_type {
68-
CountType::Bytes(c) => {
69-
let mut bytes_remaining = c;
68+
CountType::Bytes(bytes_to_copy) => {
69+
let mut bytes_remaining = bytes_to_copy;
7070

7171
loop {
7272
let number_of_bytes_read = {
@@ -148,7 +148,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
148148
// BusyBox, coreutils' uutils, GNU Core Utilities, and toybox do not (and just print nothing)
149149
// POSIX says:
150150
// "The application shall ensure that the number option-argument is a positive decimal integer."
151-
let count_type = match (args.n, args.c) {
151+
let count_type = match (args.n, args.bytes_to_copy) {
152152
(None, None) => {
153153
// If no arguments are provided, the default is 10 lines
154154
CountType::Lines(10_usize)
@@ -162,14 +162,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
162162

163163
CountType::Lines(n)
164164
}
165-
(None, Some(c)) => {
166-
if c == 0_usize {
165+
(None, Some(bytes_to_copy)) => {
166+
if bytes_to_copy == 0_usize {
167167
eprintln!("head: when a value for -c is provided, it must be greater than 0");
168168

169169
std::process::exit(1_i32);
170170
}
171171

172-
CountType::Bytes(c)
172+
CountType::Bytes(bytes_to_copy)
173173
}
174174

175175
(Some(_), Some(_)) => {
@@ -182,18 +182,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
182182
textdomain(PROJECT_NAME)?;
183183
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
184184

185+
let files = &mut args.files;
186+
185187
// if no files, read from stdin
186-
if args.files.is_empty() {
187-
args.files.push(PathBuf::new());
188+
if files.is_empty() {
189+
files.push(PathBuf::new());
188190
}
189191

192+
let want_header = files.len() > 1;
193+
190194
let mut exit_code = 0;
191-
let want_header = args.files.len() > 1;
192195
let mut first = true;
193196

194197
let mut stdout_lock = io::stdout().lock();
195198

196-
for filename in &args.files {
199+
for filename in files {
197200
if let Err(e) = head_file(&count_type, filename, first, want_header, &mut stdout_lock) {
198201
exit_code = 1;
199202
eprintln!("{}: {}", filename.display(), e);

0 commit comments

Comments
 (0)