-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable visit all the folders/file inside the zet repository * Enable `zet search` * Improve the documentation for zet::dir * Added example of environment needed by project * Added the zet current dir for testing on gitignore
- Loading branch information
1 parent
4cd9bc4
commit 45872ee
Showing
6 changed files
with
99 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ZET_PATH="./zet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/target | ||
/zet | ||
.envrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::{ | ||
env, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
/// Returns the directory for Zettelkasten repository | ||
/// | ||
/// The Zettelkasten repository take the below procedence: | ||
/// 1. `${ZET_PATH}` | ||
/// 2. `$HOME/.config/zet` | ||
/// | ||
pub fn dir() -> PathBuf { | ||
let mut path = match env::var("ZET_PATH") { | ||
Ok(val) => String::from(val), | ||
Check warning on line 14 in src/lib.rs GitHub Actions / clippyuseless conversion to the same type: `std::string::String`
|
||
Err(_e) => "".to_string(), | ||
}; | ||
|
||
if path.is_empty() { | ||
let home = match env::var("HOME") { | ||
Ok(val) => String::from(val), | ||
Check warning on line 20 in src/lib.rs GitHub Actions / clippyuseless conversion to the same type: `std::string::String`
|
||
Err(_e) => panic!("doesn't have $HOME env set"), // TODO: this should not be a problem | ||
// for windows :( | ||
}; | ||
|
||
path = format!("{home}/.config/zet"); | ||
} | ||
|
||
return Path::new(&path).to_path_buf(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{ | ||
fs::{self}, | ||
io::{self, Read}, | ||
path::PathBuf, | ||
}; | ||
|
||
use crate::SearchArgs; | ||
|
||
pub fn run(args: SearchArgs) -> io::Result<()> { | ||
println!("Search for word: '{}'", args.target_word.as_ref().unwrap()); | ||
search_target_word(&zet::dir(), &args) | ||
} | ||
|
||
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(()) | ||
} |