Created
April 18, 2020 21:08
-
-
Save franciscoj/8e44fc1bcf0e893522b6778b303d03fa to your computer and use it in GitHub Desktop.
Tail but in aprox 30 lines of Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is not at all like tail. Will fail in many scenarios but it was funny to write it :) | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::io::{BufReader, SeekFrom}; | |
use std::thread::sleep; | |
use std::time::Duration; | |
fn main() { | |
let file_name = "some_file.log"; | |
let file = File::open(file_name.clone()).unwrap(); | |
let mut reader = BufReader::new(file); | |
reader.seek(SeekFrom::End(0)).unwrap(); | |
loop { | |
let mut line = String::new(); | |
let res = reader.read_line(&mut line); | |
match res { | |
Ok(len) => { | |
if len > 0 { | |
println!("=> {}", line.replace("\n", "")); | |
line.clear(); | |
} else { | |
sleep(Duration::new(1, 0)); | |
} | |
} | |
Err(err) => { | |
println!("=> ERR: {}", err) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment