Skip to content

Commit

Permalink
Linux support | Fix -n > file size | print last few lines before foll…
Browse files Browse the repository at this point in the history
…owing
  • Loading branch information
stupendoussuperpowers committed Mar 30, 2024
1 parent 221b82f commit 70dbf6b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "trunk"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["stupendoussuperpowers"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -11,3 +12,4 @@ colored = "2.1.0"
filesize = "0.2.0"
notify = { version = "6.1.1" }
static-str = "0.2.0"

13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ TODO: ensuring linux support as well.

# Usage

Added feature for "sieving". Will follow for file changes -
Added feature for "sieving". Will follow for file changes

`trunk -s <filter> /path/to/file`

Usual tail commands are compatible -
Usual tail commands are compatible

`trunk -n <number of lines> /path/to/file`

`trunk -f /path/to/file`

.

.

.

`trunk -h` -
`trunk -h`

```
Usage: trunk.exe [OPTIONS] <FILE>
Arguments:
Expand All @@ -36,6 +42,7 @@ Options:
-n, --num-lines <NUM_LINES> Number of lines from the end to tail [default: 5]
-h, --help Print help
-V, --version Print version
```

# Building

Expand Down
35 changes: 23 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ struct FileSpec {

impl FileSpec {
fn new(fpath: &'static Path) -> Self {
let size: u64 = fpath.size_on_disk().unwrap();
Self { fpath, size }
let size: u64 = 0;
let mut ret = Self { fpath, size };
ret.update_size();
ret
}

// size_on_disk() wasn't returning actual file size for linux.
#[cfg(target_os = "linux")]
fn update_size(&mut self) {
self.size = self.fpath.metadata().unwrap().len();
}

#[cfg(target_os = "windows")]
fn update_size(&mut self) {
self.size = self.fpath.size_on_disk().unwrap();
}
}

fn main() {
let mut args = Args::parse();
println!("Args: {:#?}", args);

let filepath = to_str(args.file);
let sieve = to_str(args.sieve);
Expand All @@ -45,6 +53,9 @@ fn main() {

let mut fspec = FileSpec::new(path);

let num = args.num_lines.parse::<i32>().unwrap();
read_last_n_lines(&mut fspec, num);

if args.follow {
let mut watcher = notify::recommended_watcher(move |res| match res {
Ok(_event) => follow_filter(&mut fspec, sieve),
Expand All @@ -54,9 +65,6 @@ fn main() {

watcher.watch(path, RecursiveMode::Recursive).unwrap();
loop {}
} else {
let num = args.num_lines.parse::<i32>().unwrap();
read_last_n_lines(&mut fspec, num)
}
}

Expand All @@ -68,15 +76,18 @@ fn read_last_n_lines(file: &mut FileSpec, num: i32) {
.open(file.fpath)
.unwrap();

let mut start = 1;
while b_ns > 0 {
let mut start: u64 = 0;

// Stop when number of \n are met, or the file is completely read.
while b_ns > 0 && start < file.size {
// Read one byte at a time until we reach the specified number of \n's (b_ns)
start = start + 1;

f.seek(SeekFrom::Start(file.size - start)).unwrap();

let mut buf = vec![0; 1];
f.read_exact(&mut buf).unwrap();

start = start + 1;

if from_utf8(&buf).unwrap() == "\n".to_string() {
b_ns -= 1;
}
Expand All @@ -88,11 +99,11 @@ fn read_last_n_lines(file: &mut FileSpec, num: i32) {
let mut buf_print = Vec::new();
f.read_to_end(&mut buf_print).unwrap();

println!("{}", String::from_utf8(buf_print).unwrap());
print!("{}", String::from_utf8(buf_print).unwrap());
}

fn follow_filter(file: &mut FileSpec, filter: &str) {
if file.fpath.size_on_disk().unwrap() > file.size {
if file.fpath.metadata().unwrap().len() >= file.size {
// Regular tail -f behaviour so far.
let mut f = File::options()
.read(true)
Expand Down

0 comments on commit 70dbf6b

Please sign in to comment.