Skip to content

Commit

Permalink
added ability to send output to stdout in addition to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneish committed Mar 29, 2023
1 parent b926f4f commit 8db95e6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shrtcut"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
authors = ["Shane Stephenson <[email protected]>"]
readme = "README.md"
Expand Down
33 changes: 21 additions & 12 deletions src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Settings {
pub height: i32,
pub default: Option<String>,
pub env_annotation: Option<String>,
pub add_to_clipboard: Option<bool>,
pub print_to_stdout: Option<bool>,
}

impl Configs {
Expand Down Expand Up @@ -57,20 +59,27 @@ impl Configs {
config_path
}

pub fn copy_to_clipboard(&self, choice: String) -> Result<()> {
fn add_or_print_shortcut(&self, shortcut: String) -> Result<()> {
let env_annotation = self.settings.env_annotation.clone().unwrap_or("$".to_string());
let resolved_shortcut = if shortcut.starts_with(&env_annotation) {
var(&shortcut[env_annotation.len()..])?
} else {
shortcut.to_string()
};
if self.settings.add_to_clipboard.unwrap_or(true) {
GlobalClip::set(&resolved_shortcut)?;
}
if self.settings.print_to_stdout.unwrap_or(true) {
println!("{}", &resolved_shortcut);
}
Ok(())
}

pub fn use_shortcut(&self, choice: String) -> Result<()> {
let shorty = self.shortcuts.get(&choice);
match shorty {
Some(s) => {
let env_annotation = self.settings.env_annotation.clone().unwrap_or("$".to_string());
let annotation_len = env_annotation.len();
if s.starts_with(&env_annotation) {
let env_var = var(&s[annotation_len..])?;
GlobalClip::set(&env_var)?;
} else {
GlobalClip::set(s)?;
}
},
None => bail!("[error] Could not copy to clipboard."),
Some(s) => self.add_or_print_shortcut(s.to_string())?,
None => bail!("[error] Could not read shortcut."),
};
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl GuiApp {
wind.show();
while app::wait() {
if menu.value() >= 0 {
self.configs.copy_to_clipboard(self.choices[menu.value() as usize].clone())?;
self.configs.use_shortcut(self.choices[menu.value() as usize].clone())?;
menu.hide();
wind.hide();
break;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
},
"--grab" | "-g" => match env::args().nth(2) {
Some(choice) => {
match configs.copy_to_clipboard(choice) {
match configs.use_shortcut(choice) {
Ok(_) => (),
Err(e) => println!("{:?}", e),
};
Expand Down
13 changes: 10 additions & 3 deletions src/resources/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ width=300

# default allows you to specify the first shortcut in the dropdown menu
# if you don't specify a default, the first shortcut alphabetically will be used
default="google"
default="oatbread"

# add_to_clipboard allows you to specify whether or not the shortcut should be added to the clipboard after selection
# if you don't specify a value, it will default to true
add_to_clipboard=true

# print_to_stdout allows you to specify whether or not the shortcut should be printed to stdout after selection
# if you don't specify a value, it will default to true
print_to_stdout=true

# env_char allows you to specify how to annotate if a shortcut references an environment variable
# if you don't specify an env_char, it will default to using '$'
#
# env_char="$"
env_char="$"

[shortcuts]
# examples of creating shortcuts to google.com and oatbread.org
Expand Down

0 comments on commit 8db95e6

Please sign in to comment.