Compare commits
	
		
			6 Commits
		
	
	
		
			e3c60f7c43
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1844585619 | |||
| 1707b02b27 | |||
| 0c4be634de | |||
| cb63a82646 | |||
| 7cd9616cf6 | |||
| 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,  BufWriter, IsTerminal},
 | 
			
		||||
    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()),
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										60
									
								
								src/prehandle.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/prehandle.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
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());
 | 
			
		||||
    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{
 | 
			
		||||
        return reader.to_owned();
 | 
			
		||||
    }
 | 
			
		||||
    let header = reader[0].clone();
 | 
			
		||||
    reader.remove(0);
 | 
			
		||||
    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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod test{
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_filter_by_string(){
 | 
			
		||||
        let reader = vec!["a".to_string(), "b".to_string(), "c".to_string()];
 | 
			
		||||
        let filter_str = "a";
 | 
			
		||||
        let result = super::filter_by_string(reader, filter_str);
 | 
			
		||||
        assert_eq!(result, vec!["a".to_string()]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_take(){
 | 
			
		||||
        let reader = vec!["a".to_string(), "b".to_string(), "c".to_string()];
 | 
			
		||||
        let start_index = 1;
 | 
			
		||||
        let end_index = 2;
 | 
			
		||||
        let result = super::take(&mut reader.to_owned(), start_index, end_index);
 | 
			
		||||
        assert_eq!(result, vec!["a".to_string(), "b".to_string()]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user