Skip to content

Commit

Permalink
Added basic CLI args to Jolly (#31)
Browse files Browse the repository at this point in the history
* Added basic CLI args to Jolly

You can now specify a custom path to your config file, as well see --help and --version via command line.
  • Loading branch information
apgoetz authored Aug 6, 2023
1 parent 6ea1f2a commit 92fac67
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 22 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dirs = "5"
[dev-dependencies]
tempfile = "3"

[build-dependencies]
chrono = { version = "0.4.26", default-features = false, features = ["clock"]}

[target.'cfg(windows)'.build-dependencies]
resvg = "0.29.0"
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ https://user-images.githubusercontent.com/1356587/209451235-6911e5f1-fb4d-4348-9

# Quick Introduction

To use Jolly, simply run the `jolly` executable. Jolly will then look for a
suitable [configuration file](docs/file-format.md#locations) `jolly.toml`.
To use Jolly, simply run the `jolly` executable.

By default, Jolly won't show any results: just tell you how many entries it has loaded:
```bash
# Run Jolly with jolly.toml in the current directory
jolly
```

To use a config file that is not in the current directory, pass its path on the command line:

```bash
# Run Jolly with a custom config file
jolly /path/to/custom/jolly.toml
```

For more details on how Jolly finds its config file, see the
[documentation](docs/file-format.md#locations).

By default, Jolly won't show any results: just tell you how many
entries it has loaded:

![startup page](docs/static/startup.png)

Expand All @@ -29,7 +44,7 @@ or click it with the mouse.
To learn more about the file format used by Jolly, see the [file-format](docs/file-format.md) page.

To learn more about changing settings for Jolly, including how to
customize the theme, see the [config](config.md) page.
customize the theme, see the [config](docs/config.md) page.

To learn more advanced tips and tricks, see the [advanced](docs/advanced.md) usage page.

Expand Down
14 changes: 13 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
// build script to add icon to Jolly executable.
// this script is only used on windows platforms

fn common() {
use chrono::Utc;
let date = Utc::now().date_naive();
println!("cargo:rustc-env=JOLLY_BUILD_DATE={date}");
}

// no build requirements for macos FOR NOW
#[cfg(target_os = "macos")]
fn main() {}
fn main() {
common();
}

// check to make sure dependencies are installed
#[cfg(all(unix, not(target_os = "macos")))]
fn main() {
use std::env;

common();

let theme = env::var("JOLLY_DEFAULT_THEME").unwrap_or("gnome".into());
println!("cargo:rustc-env=JOLLY_DEFAULT_THEME={}", theme);

Expand Down Expand Up @@ -56,6 +66,8 @@ fn main() {
// set a nice icon
#[cfg(windows)]
fn main() {
common();

// determine path to save icon to
let out_file = format!("{}/jolly.ico", std::env::var("OUT_DIR").unwrap());

Expand Down
18 changes: 16 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
To use Jolly, simply run the `jolly` executable. Jolly will look for a
suitable [configuration file](file-format.md#locations) `jolly.toml`.
To use Jolly, simply run the `jolly` executable.

```bash
# Run Jolly with jolly.toml in the current directory
jolly
```

To use a config file that is not in the current directory, pass its path on the command line:

```bash
# Run Jolly with a custom config file
jolly /path/to/custom/jolly.toml
```

For more details on how Jolly finds its config file, see the
[documentation](file-format.md#locations).

By default, Jolly won't show any results: just tell you how many entries it has loaded:

Expand Down
31 changes: 20 additions & 11 deletions docs/file-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ the [TOML](https://toml.io) markup language.
You can find out more details about the syntax of TOML on the above
webpage, but the basics are defined below.

## <a name="locations"></a> Config File Locations

Jolly searches for a config file in the following locations:

1. A custom file location specified via the command line, such as `jolly /path/to/custom/jolly.toml`
2. A file named `jolly.toml` in the current working directory
3. A file named `jolly.toml` in the *config directory*

*config directory* is defined with different paths depending on the platform:

| Platform | Value | Example |
|----------|---------------------------------------|------------------------------------------|
| Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
| macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
| Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |

If a `jolly.toml` config file cannot be located, Jolly will show an error message and exit.

## Example Config

For the purposes of this example, we will refer to an example `jolly.toml` file located in this documentation (you can find a full version of the file [here](jolly.toml)):

```toml
Expand Down Expand Up @@ -296,17 +316,6 @@ url = 'https://duckduckgo.com/?q=%s'
keyword = 'ddg'
```

# <a name="locations"></a> Jolly Database Search Locations


Jolly searches for a database file (always named `jolly.toml` in the
following locations, in descending order of priority:

1. The current working directory
2. In the User's [configuration directory](https://docs.rs/dirs/latest/dirs/fn.config_dir.html)

*Note: The user's configuration directory is platform dependent, the
value for various platforms can be found in the above link.*

# <a name="errors"></a> Errors
Sometimes Jolly will encounter an error can cannot proceed. Usually,
Expand Down
71 changes: 71 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// command line parsing for jolly
use std::process::ExitCode;

fn help() {
let description = env!("CARGO_PKG_DESCRIPTION");
let exe = option_env!("CARGO_BIN_NAME").unwrap_or("jolly");
let name = env!("CARGO_PKG_NAME");
println!(
r#"{description}
Usage: {exe} [OPTIONS] [CONFIG FILE]
Options:
-V, --version Print version info and exit
-h, --help Print this help and exit
Use the optional parameter [CONFIG FILE] to use a non-default config file
For more details, see the {name} docs: https://github.com/apgoetz/jolly/blob/main/docs/README.md
"#
);
}

fn version() {
let version = env!("CARGO_PKG_VERSION");
let name = env!("CARGO_PKG_NAME");
let date = env!("JOLLY_BUILD_DATE");

println!("{name} {version} {date}");
}

fn err_help() {
let name = option_env!("CARGO_BIN_NAME").unwrap_or("jolly");
eprintln!("Try '{name} --help' for more information");
}

#[derive(Default)]
pub struct ParsedArgs {
pub config: Option<String>,
}

pub fn parse_args<I: Iterator<Item = String>>(args: I) -> Result<ParsedArgs, ExitCode> {
let mut parsed_args = ParsedArgs::default();

for arg in args.skip(1) {
if arg == "-V" || arg == "-v" || arg == "--version" {
version();
return Err(ExitCode::SUCCESS);
}

if arg == "-h" || arg == "--help" {
help();
return Err(ExitCode::SUCCESS);
}

if arg.starts_with("-") {
eprintln!("Invalid option '{arg}'");
err_help();
return Err(ExitCode::FAILURE);
}

if parsed_args.config.is_none() {
parsed_args.config = Some(arg)
} else {
eprintln!("Multiple config files passed, only one config file supported at this time");
err_help();
return Err(ExitCode::FAILURE);
}
}
Ok(parsed_args)
}
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ impl Default for Config {
}

impl Config {
pub fn custom_load(path: String) -> Self {
let config = load_path(path);
match config {
Err(e) => Self {
settings: Default::default(),
store: Err(e),
},
Ok(c) => c,
}
}

pub fn load() -> Self {
match get_logfile().map(load_path) {
Ok(config) => config.unwrap_or_else(|e| Self {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use iced::{executor, Application, Command, Element, Length, Renderer, Size};
use lazy_static;
use std::sync::mpsc;

pub mod cli;
pub mod config;
mod custom;
mod entry;
Expand Down
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use iced::{Application, Settings};
use jolly::{config, error, Jolly};
use jolly::{cli, config, Jolly};
use std::process::ExitCode;
use std::time::Instant;

pub fn main() -> Result<(), error::Error> {
pub fn main() -> ExitCode {
let custom_config = match cli::parse_args(std::env::args()) {
Ok(c) => c.config,
Err(e) => return e,
};

let now = Instant::now();

let mut config = config::Config::load();
let mut config = if let Some(path) = custom_config {
config::Config::custom_load(path)
} else {
config::Config::load()
};

let elapsed = now.elapsed();

Expand Down Expand Up @@ -35,5 +45,7 @@ pub fn main() -> Result<(), error::Error> {
settings.default_text_size = config.settings.ui.common.text_size().into();
settings.flags = config;

Jolly::run(settings).map_err(error::Error::IcedError)
Jolly::run(settings)
.map(|_| ExitCode::SUCCESS)
.unwrap_or(ExitCode::FAILURE)
}

0 comments on commit 92fac67

Please sign in to comment.