The SearchEngine
enum represents different search engines supported by the application.
pub enum SearchEngine {
Google(Google),
Bing(Bing),
DuckDuckGo(DuckDuckGo),
}
next(&self) -> Self
: Switch to the next search engine in rotationas_str(&self) -> &'static str
: Get the name of the current search enginesearch(&self, query: &str, start: u16) -> Result<Vec<SearchResult>>
: Perform a search operation
The App
struct manages the application state and core functionality.
pub struct App {
pub input: String, // User search query input
pub search_results: Vec<SearchResult>, // List of search results
pub selected_index: usize, // Currently selected result index
pub input_mode: bool, // Whether in input mode
// ... other fields
}
new() -> Self
: Create a new application instanceperform_search(&mut self) -> Result<()>
: Execute a search operationnext_search(&mut self) -> Result<()>
: Load more search resultschange_page(&mut self, direction: i32) -> Result<()>
: Navigate between pagesopen_selected_url(&mut self) -> Result<()>
: Open the selected URL in browser
The Config
struct handles application configuration.
pub struct Config {
pub search_config: SearchConfig,
pub cache_config: CacheConfig,
}
Configuration is loaded from:
- Linux/macOS:
~/.config/st/config.toml
- Windows:
%APPDATA%\st\config.toml
The application uses the thiserror
crate for error handling. Main error types:
pub enum SearchError {
NetworkError(String),
ParseError(String),
NoResults,
RateLimit,
Other(String),
}
use search_in_terminal::{App, SearchEngine};
#[tokio::main]
async fn main() -> Result<()> {
let mut app = App::new();
// Set search query
app.input = "rust programming".to_string();
// Perform search
app.perform_search().await?;
// Open first result in browser
app.open_selected_url()?;
Ok(())
}
use search_in_terminal::Config;
let config = Config {
search_config: SearchConfig {
max_retries: 3,
base_delay: 1000,
max_jitter: 500,
request_timeout: 10,
response_timeout: 10,
user_agents: vec![
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36".to_string(),
],
},
cache_config: CacheConfig {
max_capacity: 100,
time_to_live: 600,
},
};
use search_in_terminal::SearchEngine;
let mut engine = SearchEngine::Google(Google);
// Switch to next engine (Bing)
engine = engine.next();
// Switch to next engine (DuckDuckGo)
engine = engine.next();
The application uses crossterm
for terminal events:
use crossterm::event::{Event, KeyCode};
match event::read()? {
Event::Key(key) => {
match key.code {
KeyCode::Enter => perform_search(),
KeyCode::Tab => switch_engine(),
KeyCode::Char('q') => quit(),
// ... handle other keys
}
}
}