添加预处理相关逻辑, 将筛选逻辑移进预处理逻辑, 添加根据Start, End截取功能
This commit is contained in:
parent
e3c60f7c43
commit
c6dcd6b13a
@ -39,6 +39,14 @@ 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,
|
||||
|
27
src/main.rs
27
src/main.rs
@ -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::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader, BufWriter, Cursor, IsTerminal, Read},
|
||||
io::{self, BufRead, BufWriter, IsTerminal, Read},
|
||||
process,
|
||||
};
|
||||
use table::TablePrinter;
|
||||
@ -67,6 +67,8 @@ fn try_main() -> anyhow::Result<()> {
|
||||
delimiter,
|
||||
filter_str,
|
||||
style,
|
||||
start_index,
|
||||
end_index,
|
||||
padding,
|
||||
indent,
|
||||
sniff,
|
||||
@ -92,26 +94,7 @@ fn try_main() -> anyhow::Result<()> {
|
||||
.has_headers(!no_headers)
|
||||
.from_reader(match file {
|
||||
Some(path) => {
|
||||
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>
|
||||
}
|
||||
prehandle::handle(path, filter_str, start_index as u32, end_index as u32)
|
||||
}
|
||||
None if io::stdin().is_terminal() => bail!("no input file specified (use -h for help)"),
|
||||
None => Box::new(io::stdin()),
|
||||
|
41
src/prehandle.rs
Normal file
41
src/prehandle.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use std::{io::Read, path::PathBuf};
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Cursor};
|
||||
|
||||
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());
|
||||
for line in reader[1..].iter(){
|
||||
if line.contains(filter_str){
|
||||
result.push(line.to_string());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<String> {
|
||||
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{
|
||||
body.append(&mut reader[start_index as usize..end_index as usize].to_owned());
|
||||
}
|
||||
body
|
||||
}
|
Loading…
Reference in New Issue
Block a user