Skip to content

Commit d59f3f5

Browse files
authored
plugins/rink: Add prefix config option (#248)
1 parent 0ea56ae commit d59f3f5

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/rink/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ anyrun-plugin = { path = "../../anyrun-plugin" }
1313
abi_stable = "0.11.1"
1414
rink-core = "0.6"
1515
reqwest = { version = "0.11.13", default-features = false, features = ["blocking", "json", "rustls-tls"] }
16+
serde = { version = "1.0.219", features = ["derive"] }
17+
ron = "0.8.0"

plugins/rink/src/lib.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
use abi_stable::std_types::{ROption, RString, RVec};
22
use anyrun_plugin::*;
33
use rink_core::{ast, date, gnu_units, CURRENCY_FILE};
4+
use serde::Deserialize;
5+
use std::fs;
6+
7+
#[derive(Deserialize, Debug)]
8+
struct Config {
9+
prefix: String,
10+
}
11+
12+
impl Default for Config {
13+
fn default() -> Self {
14+
Config {
15+
prefix: "".to_string(),
16+
}
17+
}
18+
}
19+
20+
struct State {
21+
ctx: rink_core::Context,
22+
config: Config,
23+
}
424

525
#[init]
6-
fn init(_config_dir: RString) -> rink_core::Context {
26+
fn init(config_dir: RString) -> State {
727
let mut ctx = rink_core::Context::new();
828

929
let units = gnu_units::parse_str(rink_core::DEFAULT_FILE.unwrap());
@@ -29,7 +49,18 @@ fn init(_config_dir: RString) -> rink_core::Context {
2949
});
3050
ctx.load_dates(dates);
3151

32-
ctx
52+
let config = match fs::read_to_string(format!("{}/rink.ron", config_dir)) {
53+
Ok(content) => ron::from_str(&content).unwrap_or_else(|why| {
54+
eprintln!("[nix-run] Failed to parse config: {}", why);
55+
Config::default()
56+
}),
57+
Err(why) => {
58+
eprintln!("[nix-run] No config file provided, using default: {}", why);
59+
Config::default()
60+
}
61+
};
62+
63+
State { ctx, config }
3364
}
3465

3566
#[info]
@@ -41,8 +72,14 @@ fn info() -> PluginInfo {
4172
}
4273

4374
#[get_matches]
44-
fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec<Match> {
45-
match rink_core::one_line(ctx, &input) {
75+
fn get_matches(input: RString, state: &mut State) -> RVec<Match> {
76+
let input = if let Some(input) = input.strip_prefix(&state.config.prefix) {
77+
input.trim()
78+
} else {
79+
return RVec::new();
80+
};
81+
82+
match rink_core::one_line(&mut state.ctx, &input) {
4683
Ok(result) => {
4784
let (title, desc) = parse_result(result);
4885
vec![Match {

0 commit comments

Comments
 (0)