move show into worf main out of lib

the show option does not make sense to
be offered in the lib.
this commit moves it into worf::main, so it is not visible
via the api anymore
This commit is contained in:
Alexander Mohr 2025-06-28 23:32:19 +02:00
parent 6ce81a56d5
commit 893b3772d4
3 changed files with 122 additions and 123 deletions

View file

@ -62,36 +62,6 @@ pub enum KeyDetectionType {
Value,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Mode {
/// searches `$PATH` for executables and allows them to be run by selecting them.
Run,
/// searches `$XDG_DATA_HOME/applications` and `$XDG_DATA_DIRS/applications`
/// for desktop files and allows them to be run by selecting them.
Drun,
/// reads from stdin and displays options which when selected will be output to stdout.
Dmenu,
/// tries to determine automatically what to do
Auto,
/// use worf as file browser
File,
/// Use is as calculator
Math,
/// Connect via ssh to a given host
Ssh,
/// Emoji browser
Emoji,
/// Open search engine.
WebSearch,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Layer {
Background,
@ -128,27 +98,6 @@ impl FromStr for Anchor {
}
}
impl FromStr for Mode {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"run" => Ok(Mode::Run),
"drun" => Ok(Mode::Drun),
"dmenu" => Ok(Mode::Dmenu),
"file" => Ok(Mode::File),
"math" => Ok(Mode::Math),
"ssh" => Ok(Mode::Ssh),
"emoji" => Ok(Mode::Emoji),
"websearch" => Ok(Mode::WebSearch),
"auto" => Ok(Mode::Auto),
_ => Err(Error::InvalidArgument(
format!("{s} is not a valid argument, see help for details").to_owned(),
)),
}
}
}
impl FromStr for WrapMode {
type Err = Error;
@ -444,10 +393,6 @@ pub struct Config {
#[clap(long = "style")]
style: Option<String>,
/// Defines the mode worf is running in
#[clap(long = "show")]
show: Option<Mode>,
/// Default width of the window, defaults to 50% of the screen
#[clap(long = "width")]
width: Option<String>,
@ -771,25 +716,8 @@ impl Config {
}
#[must_use]
pub fn prompt(&self) -> String {
match &self.prompt {
None => match &self.show {
None => String::new(),
Some(mode) => match mode {
Mode::Run => "run".to_owned(),
Mode::Drun => "drun".to_owned(),
Mode::Dmenu => "dmenu".to_owned(),
Mode::Math => "math".to_owned(),
Mode::File => "file".to_owned(),
Mode::Auto => "auto".to_owned(),
Mode::Ssh => "ssh".to_owned(),
Mode::Emoji => "emoji".to_owned(),
Mode::WebSearch => "websearch".to_owned(),
},
},
Some(prompt) => prompt.clone(),
}
pub fn prompt(&self) -> Option<String> {
self.prompt.clone()
}
pub fn set_prompt(&mut self, val: String) {
@ -855,11 +783,6 @@ impl Config {
})
}
#[must_use]
pub fn show(&self) -> Option<Mode> {
self.show.clone()
}
#[must_use]
pub fn insensitive(&self) -> bool {
self.insensitive.unwrap_or(true)

View file

@ -762,7 +762,7 @@ fn build_search_entry<T: Clone + Send + 'static>(
ui_elements.search.set_css_classes(&["input"]);
ui_elements
.search
.set_placeholder_text(Some(config.prompt().as_ref()));
.set_placeholder_text(Some(&config.prompt().unwrap_or("Search...".to_owned())));
ui_elements.search.set_can_focus(false);
search_start_listen_delete_event(ui_elements, meta);

View file

@ -1,58 +1,134 @@
use std::env;
use std::sync::{Arc, RwLock};
use worf::{Error, config, config::Mode, desktop::fork_if_configured, modes};
use std::{
env,
sync::{Arc, RwLock},
};
use std::fmt::Display;
use std::str::FromStr;
use clap::Parser;
use worf::{Error, config, desktop::fork_if_configured, modes};
#[derive(Clone, Debug)]
pub enum Mode {
/// searches `$PATH` for executables and allows them to be run by selecting them.
Run,
/// searches `$XDG_DATA_HOME/applications` and `$XDG_DATA_DIRS/applications`
/// for desktop files and allows them to be run by selecting them.
Drun,
/// reads from stdin and displays options which when selected will be output to stdout.
Dmenu,
/// tries to determine automatically what to do
Auto,
/// use worf as file browser
File,
/// Use is as calculator
Math,
/// Connect via ssh to a given host
Ssh,
/// Emoji browser
Emoji,
/// Open search engine.
WebSearch,
}
#[derive(Debug, Parser)]
#[clap(
about = "Worf is a wofi like launcher, written in rust, it aims to be a drop-in replacement"
)]
struct MainConfig {
/// Defines the mode worf is running in
#[clap(long = "show")]
show: Mode,
#[command(flatten)]
worf: config::Config,
}
impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mode::Run => write!(f, "run"),
Mode::Drun => write!(f, "drun"),
Mode::Dmenu => write!(f, "dmenu"),
Mode::Math => write!(f, "math"),
Mode::File => write!(f, "file"),
Mode::Auto => write!(f, "auto"),
Mode::Ssh => write!(f, "ssh"),
Mode::Emoji => write!(f, "emoji"),
Mode::WebSearch => write!(f, "websearch"),
}
}
}
impl FromStr for Mode {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"run" => Ok(Mode::Run),
"drun" => Ok(Mode::Drun),
"dmenu" => Ok(Mode::Dmenu),
"file" => Ok(Mode::File),
"math" => Ok(Mode::Math),
"ssh" => Ok(Mode::Ssh),
"emoji" => Ok(Mode::Emoji),
"websearch" => Ok(Mode::WebSearch),
"auto" => Ok(Mode::Auto),
_ => Err(Error::InvalidArgument(
format!("{s} is not a valid argument, see help for details").to_owned(),
)),
}
}
}
fn main() {
env_logger::Builder::new()
.parse_filters(&env::var("RUST_LOG").unwrap_or_else(|_| "error".to_owned()))
.format_timestamp_micros()
.init();
let mut config = MainConfig::parse();
config.worf = config::load_config(Some(&config.worf)).unwrap_or(config.worf);
if config.worf.prompt().is_none() {
config.worf.set_prompt(config.show.to_string());
}
let args = config::parse_args();
let config = config::load_config(Some(&args));
let config = match config {
Ok(c) => c,
Err(e) => {
log::error!("error during config load, skipping it, {e}");
args
}
};
if config.version() {
if config.worf.version() {
println!("worf version {}", env!("CARGO_PKG_VERSION"));
return;
}
fork_if_configured(&config); // may exit the program
if let Some(show) = &config.show() {
let config = Arc::new(RwLock::new(config));
let result = match show {
Mode::Run => modes::run::show(config),
Mode::Drun => modes::drun::show(config),
Mode::Dmenu => modes::dmenu::show(config),
Mode::File => modes::file::show(config),
Mode::Math => {
modes::math::show(config);
Ok(())
}
Mode::Ssh => modes::ssh::show(config),
Mode::Emoji => modes::emoji::show(config),
Mode::Auto => modes::auto::show(&config),
Mode::WebSearch => modes::search::show(config),
};
fork_if_configured(&config.worf); // may exit the program
if let Err(err) = result {
if err == Error::NoSelection {
log::info!("no selection made");
} else {
log::error!("Error occurred {err:?}");
std::process::exit(1);
}
let cfg_arc = Arc::new(RwLock::new(config.worf));
let result = match config.show {
Mode::Run => modes::run::show(cfg_arc),
Mode::Drun => modes::drun::show(cfg_arc),
Mode::Dmenu => modes::dmenu::show(cfg_arc),
Mode::File => modes::file::show(cfg_arc),
Mode::Math => {
modes::math::show(cfg_arc);
Ok(())
}
Mode::Ssh => modes::ssh::show(cfg_arc),
Mode::Emoji => modes::emoji::show(cfg_arc),
Mode::Auto => modes::auto::show(&cfg_arc),
Mode::WebSearch => modes::search::show(cfg_arc),
};
if let Err(err) = result {
if err == Error::NoSelection {
log::info!("no selection made");
} else {
log::error!("Error occurred {err:?}");
std::process::exit(1);
}
} else {
log::error!("No mode provided");
std::process::exit(1);
}
}