1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::{error::Error, fs};
use comfy_table::{
modifiers::{UTF8_ROUND_CORNERS, UTF8_SOLID_INNER_BORDERS},
presets::UTF8_FULL,
Attribute, Cell, CellAlignment, ContentArrangement, Table,
};
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(&config.file_path)?;
let line_count = count_lines(&contents);
let word_count = count_words(&contents);
let byte_count = count_bytes(&contents);
let char_count = count_chars(&contents);
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_SOLID_INNER_BORDERS)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec![
Cell::new("Lines")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Left),
Cell::new("Words")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Left),
Cell::new("Bytes")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Left),
Cell::new("Chars")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Left),
Cell::new("File")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Left),
])
.add_row(vec![
Cell::new(line_count.to_string()).set_alignment(CellAlignment::Left),
Cell::new(word_count.to_string()).set_alignment(CellAlignment::Left),
Cell::new(byte_count.to_string()).set_alignment(CellAlignment::Left),
Cell::new(char_count.to_string()).set_alignment(CellAlignment::Left),
Cell::new(config.file_path).set_alignment(CellAlignment::Left),
]);
println!("{table}");
Ok(())
}
pub fn count_lines<'a>(contents: &'a str) -> usize {
contents.lines().count()
}
pub fn count_bytes<'a>(contents: &'a str) -> usize {
contents.bytes().count()
}
pub fn count_words<'a>(contents: &'a str) -> usize {
contents.split_whitespace().count()
}
pub fn count_chars<'a>(contents: &'a str) -> usize {
contents.chars().count()
}
pub struct Config {
pub file_path: String,
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
let file_path = args[1].clone();
Ok(Config { file_path })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn count_lines_test() {
let contents = "\
The quick brown fox
jumped over the lazy dog.
If the dog moved,
was it really lazy ?";
assert_eq!(count_lines(contents), 4);
}
#[test]
fn count_bytes_test() {
let contents = "The quick brown fox jumped over the lazy dog.";
assert_eq!(count_bytes(contents), 45);
}
#[test]
fn count_words_test() {
let contents = "The quick brown fox jumped over the lazy dog.";
assert_eq!(count_words(contents), 9);
}
#[test]
fn count_chars_test() {
let contents = "The quick brown fox jumped over the lazy dog.";
assert_eq!(count_words(contents), 9);
}
}