Compare commits

..

No commits in common. "cb63a82646805f2d0d4bf41a0faaa97497814efa" and "e3c60f7c438f36fa5d8287f4ed72b409d0b05b23" have entirely different histories.

3 changed files with 22 additions and 56 deletions

View File

@ -39,14 +39,6 @@ pub struct App {
#[arg(short, long, default_value_t = 1)]
pub padding: usize,
///select line between start and end;
#[arg(long="start", default_value_t = 0)]
pub start_index: usize,
///select line between start and end;
#[arg(long="end", default_value_t = 0)]
pub end_index: usize,
/// Specify global indent for table.
#[arg(short, long, default_value_t = 0)]
pub indent: usize,

View File

@ -1,14 +1,14 @@
mod cli;
mod table;
mod util;
mod prehandle;
use anyhow::bail;
use clap::Parser;
use cli::App;
use csv::{ErrorKind, ReaderBuilder};
use std::{
io::{self, BufRead, BufWriter, IsTerminal, Read},
fs::File,
io::{self, BufRead, BufReader, BufWriter, Cursor, IsTerminal, Read},
process,
};
use table::TablePrinter;
@ -67,8 +67,6 @@ fn try_main() -> anyhow::Result<()> {
delimiter,
filter_str,
style,
start_index,
end_index,
padding,
indent,
sniff,
@ -94,7 +92,26 @@ fn try_main() -> anyhow::Result<()> {
.has_headers(!no_headers)
.from_reader(match file {
Some(path) => {
prehandle::handle(path, filter_str, start_index as u32, end_index as u32)
let data = Box::new(File::open(path)?) as Box<dyn Read>;
if filter_str == "" {
data
} else {
let reader = BufReader::new(data);
let filter_data: Vec<u8> = reader
.lines()
.filter_map(|line| {
let line = line.unwrap();
if line.contains(&filter_str) {
Some(line+"\n")
} else {
None
}
})
.flat_map(|str| str.into_bytes())
.collect();
println!("{}", String::from_utf8_lossy(&filter_data));
Box::new(Cursor::new(filter_data)) as Box<dyn Read>
}
}
None if io::stdin().is_terminal() => bail!("no input file specified (use -h for help)"),
None => Box::new(io::stdin()),

View File

@ -1,43 +0,0 @@
use std::{io::Read, path::PathBuf};
use std::fs::File;
use std::io::{BufRead, BufReader, Cursor};
use std::ptr::read;
pub fn handle(path: PathBuf, filter_str:String, start_index: u32, end_index: u32) -> Box<dyn Read>{
let reader = BufReader::new(File::open(path).unwrap());
let mut lines = reader.lines().collect::<Result<Vec<_>, _>>().unwrap();
lines = filter_by_string(lines, filter_str.as_str());
lines = take(&mut lines, start_index, end_index);
Box::new(Cursor::new(lines.join("\n").into_bytes())) as Box<dyn Read>
}
fn filter_by_string(reader: Vec<String>, filter_str: &str) ->Vec<String>{
if filter_str == "" {
return reader
}
let mut result: Vec<String> = Vec::new();
result.push(reader[0].clone());
result.extend(reader.into_iter().skip(1).filter(|line| line.contains(filter_str)));
result
}
fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<String> {
if end_index < 0 && start_index < 0{
panic!("start_index and end_index must be positive");
}
if end_index == 0 && start_index == 0{
return reader.to_owned();
}
let header = reader[0].clone();
let mut body= Vec::new();
body.push(header);
if end_index == 0{
body.append(&mut reader[start_index as usize..].to_owned());
}else if(end_index < reader.len() as u32){
body.append(&mut reader[start_index as usize..end_index as usize].to_owned());
}else {
body.append(&mut reader[start_index as usize..].to_owned());
}
body
}