Skip to content

Commit

Permalink
non UTF-8 files can now be printed
Browse files Browse the repository at this point in the history
  • Loading branch information
Skosulor committed Jun 6, 2020
1 parent 999f300 commit 348bae7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ if no file is given, input is taken from `stdin`

## Differences from cat

* [ ] TODO: cannot open any file, e.g. opening a binary file produces an error.
* [ ] TODO: with flag 'non-printing' non-ascii characters are printed as '^?'
instead of the control sequence.
* If show-ends and number-nonblank flags are set the '$' sign in blank lines will
have the same line start as numbered lines. This shall be kept as an feature as
it looks neater.

* When flag show-nonprinting flag (-v) is set, non-ascii characters are printed
as '^?', and i just don't care about it enough to do a proper implementation


## Implemented features
Expand All @@ -54,3 +52,8 @@ it looks neater.
* [X] -T, --show-tabs
* [X] -u (ignored)
* [X] -v, --show-nonprinting
* [ ] Print actual control sequences instead of '^?' when show-nonprinting flag is set.

### Issues

* [X] FIXED: cannot open non UTF-8 encoded files, e.g. opening a binary file produces an error.
55 changes: 35 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct Options {
struct Output {
out: Vec<String>,
opt: Options,
ln: u64,
}

enum Input {
Expand Down Expand Up @@ -93,7 +94,7 @@ impl Output {
// -n & -b
fn number_lines(&mut self) {
if self.opt.numbered || self.opt.numbered_nonblank {
let mut n = 1;
let mut n = self.ln;
let mut _prefix = String::from("");

for line in self.out.iter_mut() {
Expand All @@ -106,6 +107,7 @@ impl Output {
let temp = format!("{0:>6} ", _prefix);
*line = String::from(temp + line);
}
self.ln = n;
}
}
fn show_ends(&mut self) {
Expand Down Expand Up @@ -140,21 +142,18 @@ impl Output {
fn show_tabs(&mut self) {
if self.opt.show_tabs || self.opt.non_print_and_show_tabs || self.opt.show_all {
for line in self.out.iter_mut() {
*line = line.replace("\t", "^I").clone();
*line = line.replace('\t', "^I").clone();
}
}
}

//
// display charactars as ^ which are not supported by the terminal
fn show_nonprinting(&mut self) {
if self.opt.non_print_and_show_tabs
|| self.opt.non_print_and_show_ends
|| self.opt.non_printing
|| self.opt.show_all
{
for line in self.out.iter_mut() {
//line.retain(|c| c.is_ascii());
let mut temp = String::new();
for c in &mut line.chars() {
if c.is_ascii() {
Expand All @@ -173,6 +172,7 @@ impl Output {
let mut out = Output {
out: Vec::new(),
opt: o,
ln: 0,
};

let mut f = match Input::from(&out.opt.path) {
Expand All @@ -181,23 +181,36 @@ impl Output {
};

loop {
match f.readline() {
Ok(res) => match res {
ReadResult::Line(l) => out.out.push(l),
ReadResult::EOF => break,
},
Err(err) => return Err(err),
let mut done = false;
for _x in 1..10 {
match f.readline() {
Ok(res) => match res {
ReadResult::Line(l) => out.out.push(l),
ReadResult::EOF => {
done = true;
break;
}
},
Err(err) => return Err(err),
}
}

out.format_output();
out.print();
if done {
break;
}
}

out.format_output();
out.print();
return Ok(());
}
fn print(&self) {
for line in self.out.iter() {
println!("{}", line);
}
fn print(&mut self) {
self.out.retain(|line| {
let _delete = {
println!("{}", line);
return false;
};
})
}
}

Expand All @@ -221,13 +234,15 @@ impl Input {
let res = match self {
Input::FromStdin => {
let res = std::io::stdin().read_line(&mut input);
input = String::from(input.trim());
input = input.replace('\n', "").clone();
res
}

Input::FromFile(f) => {
let res = f.read_line(&mut input);
input = String::from(input.trim());
let mut buf = vec![];
let res = f.read_until(b'\n', &mut buf);
input = String::from_utf8_lossy(&buf).to_string();
input = input.replace('\n', "").clone();
res
}
};
Expand Down

0 comments on commit 348bae7

Please sign in to comment.