Skip to content

Commit

Permalink
Support Universal Actions
Browse files Browse the repository at this point in the history
Just implemented in the simplest way possible for now.
  • Loading branch information
rossmacarthur committed Dec 30, 2023
1 parent 32b8bf1 commit 887ae3a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
8 changes: 7 additions & 1 deletion examples/hello-alfred/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ fn main() -> Result<(), Box<dyn Error>> {
.modifier(Modifier::new(Key::Option).arg("/path/to/modified.jpg"))
.modifier(Modifier::new(Key::Control).icon(Icon::with_image("/path/to/file.png")))
.modifier(Modifier::new(Key::Shift).valid(false))
.quicklook_url("https://example.com");
.quicklook_url("https://example.com")
.action(value!({
"text": ["one", "two", "three"],
"url": "https://www.alfredapp.com",
"file": "~/Desktop",
"auto": "~/Pictures"
}));

// Output the item to Alfred!
Output::new()
Expand Down
50 changes: 49 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ use std::time::Duration;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};

pub use serde_json::json as value;
pub use serde_json::Value;

#[cfg(feature = "detach")]
pub use powerpack_detach as detach;

Expand Down Expand Up @@ -198,6 +201,9 @@ pub struct Item {
/// A Quick Look URL which will be shown if the user uses Quick Look (⌘+Y).
#[serde(rename = "quicklookurl", skip_serializing_if = "Option::is_none")]
quicklook_url: Option<String>,

#[serde(skip_serializing_if = "Value::is_null")]
action: Value,
}

/// The output of a workflow (i.e. input for the script filter)
Expand Down Expand Up @@ -399,7 +405,7 @@ impl Item {
/// Set the arguments which are passed through the workflow to the connected
/// output action.
///
/// Same as [`arg`], but allows you to pass multiple arguments.
/// Same as [`arg`][Self::arg], but allows you to pass multiple arguments.
#[must_use]
pub fn args<I, J>(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self
where
Expand Down Expand Up @@ -514,6 +520,48 @@ impl Item {
self.modifiers.insert(key, data);
self
}

/// Set the Universal Action item(s).
///
/// This element defines the Universal Action items used when actioning the
/// result, and overrides the [`arg`][Self::arg] being used for actioning.
/// The action key can take a string or array for simple types, and the
/// content type will automatically be derived by Alfred to file, url, or
/// text.
///
/// # Examples
///
/// Single item:
///
/// ```
/// # use powerpack::Item;
/// let item = Item::new("Title").action("Alfred is Great");
/// ```
///
/// Multiple Items:
///
/// ```
/// # use powerpack::{value, Item};
/// let item = Item::new("Title").action(value!(["Alfred is Great", "I use him all day long"]));
/// ```
///
/// For control over the content type of the action, you can use an object
/// with typed keys:
///
/// ```
/// # use powerpack::{value, Item};
/// let item = Item::new("Title").action(value!({
/// "text": ["one", "two", "three"],
/// "url": "https://www.alfredapp.com",
/// "file": "~/Desktop",
/// "auto": "~/Pictures"
/// }));
/// ```
///
pub fn action(mut self, action: impl Into<Value>) -> Self {
self.action = action.into();
self
}
}

fn duration_as_secs<S>(duration: &Option<Duration>, s: S) -> Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use powerpack::{Icon, Item, Key, Kind, Modifier, Output};
use powerpack::{value, Icon, Item, Key, Kind, Modifier, Output};

#[test]
fn smoke() {
Expand Down Expand Up @@ -34,7 +34,13 @@ fn all() {
.modifier(Modifier::new(Key::Option).arg("/path/to/modified.jpg"))
.modifier(Modifier::new(Key::Control).icon(Icon::with_image("/path/to/file.png")))
.modifier(Modifier::new(Key::Shift).valid(false))
.quicklook_url("https://example.com");
.quicklook_url("https://example.com")
.action(value!({
"text": ["one", "two", "three"],
"url": "https://www.alfredapp.com",
"file": "~/Desktop",
"auto": "~/Pictures"
}));

let mut output = Output::new();
output.rerun(Duration::from_millis(500)).items([item]);
Expand Down
22 changes: 16 additions & 6 deletions tests/testdata/all.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,36 @@
"autocomplete": "to this",
"type": "file:skipcheck",
"mods": {
"shift": {
"valid": false
},
"cmd": {
"subtitle": "⌘ changes the subtitle"
},
"alt": {
"arg": "/path/to/modified.jpg"
},
"ctrl": {
"icon": {
"path": "/path/to/file.png"
}
},
"alt": {
"arg": "/path/to/modified.jpg"
"shift": {
"valid": false
}
},
"text": {
"copy": "this text will be copied with ⌘C",
"largetype": "this text will be displayed with ⌘L"
},
"quicklookurl": "https://example.com"
"quicklookurl": "https://example.com",
"action": {
"auto": "~/Pictures",
"file": "~/Desktop",
"text": [
"one",
"two",
"three"
],
"url": "https://www.alfredapp.com"
}
}
]
}

0 comments on commit 887ae3a

Please sign in to comment.