Skip to content

Commit

Permalink
add command | sss pattern-string support
Browse files Browse the repository at this point in the history
  • Loading branch information
xdevz committed Oct 19, 2019
1 parent 66392a5 commit 22c79b9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 28 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ maintenance = { status = "..." }

[dependencies]
colored = "1.8.0"
isatty = "0.1"

[profile.release]
opt-level = 2
75 changes: 54 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@
# so stupid search tool
# so stupid search tool <阿Q的哥锐普>

# install
# English Documentation

## install from source code
* Download the file
* Enter the directory contains file with name of "Cargo.toml"
* Run command as blow:
## install

### install from source code
1.install rust toolchain
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
2.Download the file
```bash
git clone https://github.com/Lispre/so_stupid_search.git
```
3.Enter the directory contains file with name of "Cargo.toml" and Run command as blow:
```bash
cd so_stupid_search
cargo build --release
```
4.Then you will get the executable file "so_stupid_search/target/release/sss"

* Then you will get the executable file ./target/release/sfind

## install by cargo

5.move the executable file sss to your $PATH directory
```bash
cargo install so_stupid_search
alias sss=$HOME/.cargo/bin/so_stupid_search
sudo mv ./target/release/sss /usr/local/bin/
```

# Usage

```bash
## Usage
### search file system
```bash
sss search-string start-directory
```
```
### filter command pipe
```bash
command | sss main.go
```

# Example
## Example

## common search
### common search
```bash
sss walk .
sss "func main(" .
```

## search specified type of file

### search specified type of file
```bash
#only search in kubernetes yaml files
sss -t yaml fuck_str .
```

# 中文文档

## 安装
### 从源代码构建安装
1.安装rust编译器
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
2.下载so_stupid_search源代码
```bash
git clone https://github.com/Lispre/so_stupid_search.git
```
3.进入源码根目录进行构建
```bash
cd so_stupid_search
cargo build --release
```

4.获得可执行文件 "so_stupid_search/target/release/sss"

5.将sss可执行文件复制到 $PATH 变量包含的一个目录中
```bash
sudo mv ./target/release/sss /usr/local/bin/
```
6.现在你就可以像阿Q一样使用sss了
45 changes: 38 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate colored;
extern crate isatty;
use colored::*;

use std::collections::VecDeque;
Expand All @@ -9,6 +10,7 @@ use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use isatty::{stdin_isatty};

fn is_text(file_path: &Path) -> bool {
if let Ok(mut file_handle) = File::open(file_path) {
Expand Down Expand Up @@ -103,16 +105,45 @@ fn call_back(de: &Path, pt: &String) {

fn main() {
let args: Vec<String> = args().collect();
if args.len() != 3 && args.len() != 5 {
println!("usage: sss pattern-string root-directory");
println!(" sss -t file_ext pattern-string root-directory");
println!(" sss pattern-string root-directory -t file_ext");
println!("eg: sss main ./src");
println!("eg: sss -t cpp main ./src");
println!("eg: sss main ./src -t cpp");
if (args.len() == 2 && stdin_isatty()) || (args.len() != 2 && args.len() != 3 && args.len() != 5) {
println!("usage: sss pattern-string root-directory 在指定目录root-directory下面搜索pattern-string");
println!(" sss -t file_ext pattern-string root-directory 在指定目录root-directory下面对扩展名是file_ext的文件搜索pattern-string");
println!(" sss pattern-string root-directory -t file_ext 在指定目录root-directory下面对扩展名是file_ext的文件搜索pattern-string");
println!(" command | sss pattern-string 对命令command的输出进行pattern-string搜索");
println!("version: 3.4.1");
println!(`eg: sss "func main(" ./src`);
println!(`eg: sss -t go "func main(" ./src`);
println!(`eg: sss "func main(" ./src -t cpp`);
println!(`eg: somme_command | sss pattern-string`);
return;
}

if args.len() == 2 && !stdin_isatty() {
let buf = io::BufReader::new(std::io::stdin());
for line in io::BufRead::lines(buf) {
match line {
Ok(ln) => {
if ln.contains(args[1].as_str()) {
let v: Vec<&str> = ln.split(args[1].as_str()).collect();
let v_len = v.len();
for i in 1..v_len + 1 {
if i == v_len {
println!("{}", &v[i - 1]);
} else {
print!("{}", &v[i - 1]);
print!("{}", args[1].red().purple().magenta().bold());
}
}
} else {
()
}
}
Err(_) => (),
}
}
return
}

let mut queue: VecDeque<PathBuf> = VecDeque::new();
if args.len() == 3 {
let pattern_str = &args[1];
Expand Down

0 comments on commit 22c79b9

Please sign in to comment.