From c6dcd6b13a1e6af9329f92878ab8fe271669997b Mon Sep 17 00:00:00 2001 From: JIe Date: Wed, 20 Nov 2024 16:47:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91,=20=E5=B0=86=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E9=80=BB=E8=BE=91=E7=A7=BB=E8=BF=9B=E9=A2=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91,=20=E6=B7=BB=E5=8A=A0=E6=A0=B9?= =?UTF-8?q?=E6=8D=AEStart,=20End=E6=88=AA=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.rs | 8 ++++++++ src/main.rs | 27 +++++---------------------- src/prehandle.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 src/prehandle.rs diff --git a/src/cli.rs b/src/cli.rs index 1d70065..a069cbd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, diff --git a/src/main.rs b/src/main.rs index 9a1ca95..eb91f45 100644 --- a/src/main.rs +++ b/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; - if filter_str == "" { - data - } else { - let reader = BufReader::new(data); - let filter_data: Vec = 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 - } + 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()), diff --git a/src/prehandle.rs b/src/prehandle.rs new file mode 100644 index 0000000..3e11076 --- /dev/null +++ b/src/prehandle.rs @@ -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{ + let reader = BufReader::new(File::open(path).unwrap()); + let mut lines = reader.lines().collect::, _>>().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 +} + +fn filter_by_string(reader: Vec, filter_str: &str) ->Vec{ + if filter_str == "" { + return reader + } + let mut result: Vec = 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, start_index: u32, end_index: u32) -> Vec { + 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 +} \ No newline at end of file