Skip to content

Commit cc4245e

Browse files
committed
allow specifying an optional default browser
1 parent 8bb34a3 commit cc4245e

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Use the UI prompt to set Highbrow as your default browser.
3737
Create a configuration file at `~/.config/highbrow.toml` with the following structure:
3838

3939
```toml
40+
# Optional: Set a default browser for URLs that don't match any patterns
41+
default_browser = "_Firefox"
42+
4043
[[browsers]]
4144
label = "_Firefox" # Underscore prefix creates the keyboard shortcut Alt+f
4245
command = "firefox" # Command to launch the browser

src/config.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::PathBuf;
88
#[derive(Debug, Clone, Deserialize)]
99
pub struct Config {
1010
pub browsers: Vec<BrowserConfig>,
11+
pub default_browser: Option<String>, // Label of the default browser
1112
}
1213

1314
#[derive(Debug, Clone, Deserialize)]
@@ -80,6 +81,7 @@ fn create_default_config() -> Config {
8081
patterns: None,
8182
},
8283
],
84+
default_browser: None,
8385
}
8486
}
8587

@@ -103,12 +105,25 @@ fn validate_config(config: &Config) -> Result<(), ConfigError> {
103105
}
104106
}
105107

108+
// Validate default browser if specified
109+
if let Some(ref default_label) = config.default_browser {
110+
let default_exists = config.browsers.iter().any(|b| b.label == *default_label);
111+
if !default_exists {
112+
return Err(ConfigError::ValidationError(format!(
113+
"Default browser '{}' not found in browsers list",
114+
default_label
115+
)));
116+
}
117+
}
118+
106119
Ok(())
107120
}
108121

109122
// Find the first browser that matches the given URL based on configured patterns
123+
// If no pattern matches and a default browser is configured, return the default browser
110124
pub fn find_browser_for_url(url: &str, config: &Config) -> Option<BrowserConfig> {
111-
config
125+
// First, try to find a browser with matching patterns
126+
let pattern_match = config
112127
.browsers
113128
.iter()
114129
.find(|b| {
@@ -121,5 +136,22 @@ pub fn find_browser_for_url(url: &str, config: &Config) -> Option<BrowserConfig>
121136
})
122137
})
123138
})
124-
.cloned()
139+
.cloned();
140+
141+
// If a pattern matched, return that browser
142+
if pattern_match.is_some() {
143+
return pattern_match;
144+
}
145+
146+
// If no pattern matched, try to return the default browser
147+
if let Some(ref default_label) = config.default_browser {
148+
return config
149+
.browsers
150+
.iter()
151+
.find(|b| b.label == *default_label)
152+
.cloned();
153+
}
154+
155+
// No pattern match and no default browser configured
156+
None
125157
}

0 commit comments

Comments
 (0)