Skip to content

Commit

Permalink
Enable zet search
Browse files Browse the repository at this point in the history
  • Loading branch information
peterramaldes committed Nov 1, 2023
1 parent 500c307 commit 0134433
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod create;
pub mod search;

use clap::Parser;
use clap::{Args, Parser};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -20,14 +20,25 @@ enum Subcommand {

/// Search specific words on the Zettelkasten repository
#[clap(visible_alias = "s")]
Search,
Search(SearchArgs),
}

#[derive(Args, Debug)]
pub struct SearchArgs {
/// The word that you are trying to find on Zettelkasten repository
#[arg(short, long)]
target_word: Option<String>,

/// If this flag enable, means that the word search using the `target_word` will ignore case
#[arg(short, long)]
ignore_case: bool,
}

fn main() -> std::io::Result<()> {
let cli = Cli::parse();

return match cli.subcommand {
Subcommand::Create => create::run(),
Subcommand::Search => search::run(),
Subcommand::Search(args) => search::run(args),
};

Check warning on line 43 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/main.rs:40:5 | 40 | / return match cli.subcommand { 41 | | Subcommand::Create => create::run(), 42 | | Subcommand::Search(args) => search::run(args), 43 | | }; | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 40 ~ match cli.subcommand { 41 + Subcommand::Create => create::run(), 42 + Subcommand::Search(args) => search::run(args), 43 ~ } |
}
42 changes: 30 additions & 12 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
use std::{
fs::{self},
io,
io::{self, Read},
path::PathBuf,
};

pub fn run() -> io::Result<()> {
let _ = visit_dirs(&zet::dir());
use crate::SearchArgs;

Ok(())
pub fn run(args: SearchArgs) -> io::Result<()> {
println!("Search for word: '{}'", args.target_word.as_ref().unwrap());
search_target_word(&zet::dir(), &args)
}

fn visit_dirs(dir: &PathBuf) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
println!("{:?}", path);
if path.is_dir() {
visit_dirs(&path)?;
fn search_target_word(dir: &PathBuf, args: &SearchArgs) -> io::Result<()> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();

if entry.file_type()?.is_file() {
// Check if contains the word that we are searching
let file_path = entry.path();

// Open the file
let mut file = fs::File::open(&file_path)?;

// Read the file content into a String
let mut content = String::new();
file.read_to_string(&mut content)?;

// Search for the target word in the content
let target_word = args.target_word.as_ref().unwrap();
if (args.ignore_case && content.to_lowercase().contains(&target_word.to_lowercase()))
|| content.contains(target_word)
{
println!("* {:?}", file_path);
}
} else {
search_target_word(&path, args)?;
}
}

Ok(())
}

0 comments on commit 0134433

Please sign in to comment.