-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a94db99
commit 0e17c88
Showing
19 changed files
with
601 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,57 @@ | ||
use std::error::Error; | ||
|
||
use clap::{arg, command, Args}; | ||
use odict::search::{get_default_index_dir, SearchOptions}; | ||
|
||
use crate::{enums::PrintFormat, print_entries, CLIContext, IndexArgs, DEFAULT_INDEX_MEMORY}; | ||
|
||
#[derive(Debug, Args)] | ||
#[command(args_conflicts_with_subcommands = true)] | ||
#[command(flatten_help = true)] | ||
pub struct SearchArgs { | ||
/// Path to a compiled dictionary or an alias | ||
#[arg(required = true)] | ||
dictionary: String, | ||
|
||
/// Format in which to print the results | ||
#[arg(short, long, default_value_t = PrintFormat::JSON)] | ||
format: PrintFormat, | ||
|
||
/// Creates a new index if one doesn't already exist | ||
#[arg(long, default_value_t = false)] | ||
index: bool, | ||
|
||
/// Search query | ||
#[arg(required = true)] | ||
query: String, | ||
} | ||
|
||
pub fn search(ctx: &mut CLIContext, args: &SearchArgs) -> Result<(), Box<dyn Error>> { | ||
let file = ctx | ||
.reader | ||
.read_from_path_or_alias_with_manager(&args.dictionary, &ctx.alias_manager)?; | ||
|
||
let dict = file.to_dictionary()?; | ||
|
||
if args.index { | ||
let index_path = get_default_index_dir().join(dict.id.as_str()); | ||
|
||
if !index_path.exists() { | ||
crate::index( | ||
ctx, | ||
&IndexArgs { | ||
dictionary: args.dictionary.clone(), | ||
directory: None, | ||
overwrite: false, | ||
memory: DEFAULT_INDEX_MEMORY, | ||
}, | ||
)?; | ||
} | ||
} | ||
|
||
let results = dict.search(args.query.as_str(), SearchOptions::default())?; | ||
|
||
print_entries(ctx, vec![results], &args.format)?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.