Compare commits
No commits in common. "cb63a82646805f2d0d4bf41a0faaa97497814efa" and "e3c60f7c438f36fa5d8287f4ed72b409d0b05b23" have entirely different histories.
cb63a82646
...
e3c60f7c43
@ -39,14 +39,6 @@ pub struct App {
|
|||||||
#[arg(short, long, default_value_t = 1)]
|
#[arg(short, long, default_value_t = 1)]
|
||||||
pub padding: usize,
|
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.
|
/// Specify global indent for table.
|
||||||
#[arg(short, long, default_value_t = 0)]
|
#[arg(short, long, default_value_t = 0)]
|
||||||
pub indent: usize,
|
pub indent: usize,
|
||||||
|
27
src/main.rs
27
src/main.rs
@ -1,14 +1,14 @@
|
|||||||
mod cli;
|
mod cli;
|
||||||
mod table;
|
mod table;
|
||||||
mod util;
|
mod util;
|
||||||
mod prehandle;
|
|
||||||
|
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cli::App;
|
use cli::App;
|
||||||
use csv::{ErrorKind, ReaderBuilder};
|
use csv::{ErrorKind, ReaderBuilder};
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, BufRead, BufWriter, IsTerminal, Read},
|
fs::File,
|
||||||
|
io::{self, BufRead, BufReader, BufWriter, Cursor, IsTerminal, Read},
|
||||||
process,
|
process,
|
||||||
};
|
};
|
||||||
use table::TablePrinter;
|
use table::TablePrinter;
|
||||||
@ -67,8 +67,6 @@ fn try_main() -> anyhow::Result<()> {
|
|||||||
delimiter,
|
delimiter,
|
||||||
filter_str,
|
filter_str,
|
||||||
style,
|
style,
|
||||||
start_index,
|
|
||||||
end_index,
|
|
||||||
padding,
|
padding,
|
||||||
indent,
|
indent,
|
||||||
sniff,
|
sniff,
|
||||||
@ -94,7 +92,26 @@ fn try_main() -> anyhow::Result<()> {
|
|||||||
.has_headers(!no_headers)
|
.has_headers(!no_headers)
|
||||||
.from_reader(match file {
|
.from_reader(match file {
|
||||||
Some(path) => {
|
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 if io::stdin().is_terminal() => bail!("no input file specified (use -h for help)"),
|
||||||
None => Box::new(io::stdin()),
|
None => Box::new(io::stdin()),
|
||||||
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user