1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Features specific to [SwiftBar](https://swiftbar.app/)

use {
    std::{
        borrow::Cow,
        collections::BTreeMap,
        convert::TryInto,
        env,
        io,
        iter,
        path::Path,
        sync::Arc,
    },
    open::that as open,
    semver::Version,
    thiserror::Error,
    url::Url,
    crate::{
        ContentItem,
        MainOutput,
        Menu,
        MenuItem,
        attr::{
            Color,
            Command,
            Image,
            IntoUrl,
            Params,
        },
    },
};
#[cfg(feature = "assume-flavor")] use static_assertions::const_assert;
#[cfg(feature = "tokio")] use {
    std::pin::Pin,
    futures::{
        future::Future,
        stream::StreamExt as _,
    },
    crate::AsyncMainOutput,
};

/// The highest build number checked for conditional features.
#[cfg(feature = "assume-flavor")] const MAX_BUILD: usize = 402;

macro_rules! build_ge {
    ($swiftbar:expr, $build:expr) => {{
        #[cfg(feature = "assume-flavor")] const_assert!($build <= MAX_BUILD);
        $swiftbar.build >= $build
    }};
}

/// A type-safe handle for [SwiftBar](https://swiftbar.app/)-specific features.
///
/// Some SwiftBar-specific features are currently unsupported:
///
/// * [Script metadata](https://github.com/swiftbar/SwiftBar#script-metadata) is unsupported since `cargo` does not support adding metadata to binaries it produces. You will have to [add any metadata via `xattr`](https://github.com/swiftbar/SwiftBar#metadata-for-binary-plugins).
#[derive(Debug, Clone, Copy)]
pub struct SwiftBar {
    build: usize,
}

impl SwiftBar {
    /// Checks whether the plugins is running in SwiftBar by checking environment variables.
    /// If it does, returns a handle allowing use of SwiftBar-specific features.
    pub fn check() -> Option<Self> {
        Some(Self {
            build: env::var("SWIFTBAR_BUILD").ok()?.parse().ok()?,
        })
    }

    #[cfg(feature = "assume-flavor")]
    #[cfg_attr(docsrs, doc(cfg(feature = "assume-flavor")))]
    /// Returns a handle allowing use of SwiftBar-specific features **without checking whether the plugin is actually running inside SwiftBar**.
    /// If the plugin is actually running in a different implementation or an outdated version of SwiftBar, this may lead to incorrect behavior.
    pub fn assume() -> Self {
        Self {
            build: MAX_BUILD,
        }
    }

    /// The name of the plugin, including refresh time and file extension, as used in [`swiftbar:` URLs](https://github.com/swiftbar/SwiftBar#url-scheme).
    pub fn plugin_name(&self) -> Result<String, PluginNameError> {
        Ok(Path::new(&env::var_os("SWIFTBAR_PLUGIN_PATH").ok_or(PluginNameError::Env)?)
            .file_name().ok_or(PluginNameError::NoFileName)?
            .to_str().ok_or(PluginNameError::NonUtf8FileName)?
            .to_owned()
        )
    }

    /// Returns the SwiftBar version on which the plugin is running by checking environment variables.
    pub fn running_version(&self) -> Result<Version, VersionCheckError> {
        Ok(env::var("SWIFTBAR_VERSION")?.parse()?)
    }

    /// Unlike BitBar, SwiftBar supports more than 5 parameters for `bash=` commands.
    pub fn command(&self, cmd: impl IntoParams) -> Params {
        cmd.into_params(self)
    }

    /// Returns a [`Color`](crate::param::Color) that renders differently depending on whether the system is in dark mode.
    pub fn themed_color(&self, light: Color, dark: Color) -> Color {
        Color {
            light: light.light,
            dark: Some(dark.dark.unwrap_or(dark.light)),
        }
    }

    /// Adds a [SF Symbols](https://developer.apple.com/sf-symbols/) image to a menu item.
    pub fn sf_image(&self, item: &mut ContentItem, image: impl ToString) {
        Attrs::for_item(item).sf_image = Some(image.to_string());
    }
}

/// A type that can be used as `bash=` command parameters for SwiftBar, which unlike BitBar supports more than five parameters.
pub trait IntoParams {
    /// Converts this value into command parameters.
    ///
    /// Equivalent to `swiftbar.command(self)`.
    fn into_params(self, swiftbar: &SwiftBar) -> Params;
}

impl IntoParams for Params {
    fn into_params(self, _: &SwiftBar) -> Params {
        self
    }
}

macro_rules! impl_into_params {
    ($n:literal$(, $elt:ident: $t:ident)*) => {
        impl<T: ToString> IntoParams for [T; $n] {
            fn into_params(self, _: &SwiftBar) -> Params {
                let [cmd, $($elt),*] = self;
                Params {
                    cmd: cmd.to_string(),
                    params: vec![$($elt.to_string()),*],
                }
            }
        }

        impl<Cmd: ToString, $($t: ToString),*> IntoParams for (Cmd, $($t),*) {
            fn into_params(self, _: &SwiftBar) -> Params {
                let (cmd, $($elt),*) = self;
                Params {
                    cmd: cmd.to_string(),
                    params: vec![$($elt.to_string()),*],
                }
            }
        }
    };
}

impl_into_params!(1);
impl_into_params!(2, param1: A);
impl_into_params!(3, param1: A, param2: B);
impl_into_params!(4, param1: A, param2: B, param3: C);
impl_into_params!(5, param1: A, param2: B, param3: C, param4: D);
impl_into_params!(6, param1: A, param2: B, param3: C, param4: D, param5: E);
impl_into_params!(7, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F);
impl_into_params!(8, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G);
impl_into_params!(9, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H);
impl_into_params!(10, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I);
impl_into_params!(11, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J);
impl_into_params!(12, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J, param11: K);
impl_into_params!(13, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J, param11: K, param12: L);
impl_into_params!(14, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J, param11: K, param12: L, param13: M);
impl_into_params!(15, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J, param11: K, param12: L, param13: M, param14: N);
impl_into_params!(16, param1: A, param2: B, param3: C, param4: D, param5: E, param6: F, param7: G, param8: H, param9: I, param10: J, param11: K, param12: L, param13: M, param14: N, param15: O);

impl<'a, T: ToString> IntoParams for &'a [T] {
    /// # Panics
    ///
    /// If `self` is empty.
    fn into_params(self, _: &SwiftBar) -> Params {
        Params {
            cmd: self[0].to_string(),
            params: self[1..].iter().map(|param| param.to_string()).collect(),
        }
    }
}

impl<T: ToString> IntoParams for Vec<T> {
    /// # Panics
    ///
    /// If `self` is empty.
    fn into_params(mut self, _: &SwiftBar) -> Params {
        Params {
            cmd: self.remove(0).to_string(),
            params: self.into_iter().map(|param| param.to_string()).collect(),
        }
    }
}

/// Flavor-specific [`ContentItem`] attributes.
#[derive(Debug)]
pub struct Attrs {
    sf_image: Option<String>,
}

impl Attrs {
    fn for_item(item: &mut ContentItem) -> &mut Attrs {
        match item.flavor_attrs.get_or_insert(super::Attrs::SwiftBar(Attrs { sf_image: None })) {
            super::Attrs::SwiftBar(ref mut params) => params,
        }
    }

    pub(crate) fn render<'a>(&'a self, rendered_params: &mut BTreeMap<Cow<'a, str>, Cow<'a, str>>) {
        if let Some(ref sf_image) = self.sf_image {
            rendered_params.insert(Cow::Borrowed("sfimage"), Cow::Borrowed(sf_image));
        }
    }
}

/// An error that can occur when checking the running SwiftBar version.
#[derive(Debug, Error, Clone)]
pub enum VersionCheckError {
    /// The `SWIFTBAR_VERSION` environment variable was unset or not valid UTF-8
    #[error(transparent)] Env(#[from] env::VarError),
    /// The `SWIFTBAR_VERSION` environment variable was not a valid semantic version
    #[error(transparent)] Parse(Arc<semver::Error>),
}

impl From<semver::Error> for VersionCheckError {
    fn from(e: semver::Error) -> VersionCheckError {
        VersionCheckError::Parse(Arc::new(e))
    }
}

impl From<VersionCheckError> for Menu {
    fn from(e: VersionCheckError) -> Menu {
        let mut menu = vec![MenuItem::new("Error checking running SwiftBar version")];
        match e {
            VersionCheckError::Env(e) => menu.push(MenuItem::new(e)),
            VersionCheckError::Parse(e) => {
                menu.push(MenuItem::new(format!("error parsing version: {}", e)));
                menu.push(MenuItem::new(format!("{:?}", e)));
            }
        }
        Menu(menu)
    }
}

/// An error that can occur when checking the running SwiftBar plugin name.
#[derive(Debug, Error, Clone)]
pub enum PluginNameError {
    /// The `SWIFTBAR_PLUGIN_PATH` environment variable was unset
    #[error("missing `SWIFTBAR_PLUGIN_PATH` environment variable")]
    Env,
    /// The `SWIFTBAR_PLUGIN_PATH` environment variable did not end in a file name
    #[error("no filename in `SWIFTBAR_PLUGIN_PATH` environment variable")]
    NoFileName,
    /// The file name was not valid UTF-8
    #[error("plugin filename is not valid UTF-8")]
    NonUtf8FileName,
}

impl From<PluginNameError> for Menu {
    fn from(e: PluginNameError) -> Menu {
        Menu(vec![
            MenuItem::new("Error checking running SwiftBar plugin name"),
            MenuItem::new(e.to_string()),
        ])
    }
}

/// An error that can occur in [`Notification::command`].
#[derive(Debug, Error, Clone)]
pub enum NotificationCommandError<C: TryInto<Command>>
where C::Error: std::error::Error {
    /// Converting the parameter to a `Command` failed
    #[error(transparent)] Command(C::Error),
    /// Running commands on notification click is only supported on SwiftBar 1.4.3 beta 4 or newer
    #[error("running commands on notification click is only supported on SwiftBar 1.4.3 beta 4 or newer")]
    UnsupportedSwiftBarVersion,
}

impl<C: TryInto<Command>> From<NotificationCommandError<C>> for Menu
where C::Error: std::error::Error {
    fn from(e: NotificationCommandError<C>) -> Menu {
        let mut menu = vec![MenuItem::new("Error adding command to notification")];
        match e {
            NotificationCommandError::Command(e) => {
                menu.push(MenuItem::new(format!("error building command: {}", e)));
                menu.push(MenuItem::new(format!("{:?}", e)));
            }
            NotificationCommandError::UnsupportedSwiftBarVersion => menu.push(MenuItem::new("running commands on notification click is only supported on SwiftBar 1.4.3 beta 4 or newer")),
        }
        Menu(menu)
    }
}

/// A SwiftBar notification that can be opened as a URL.
pub struct Notification {
    swiftbar: SwiftBar,
    plugin_name: String,
    title: Option<String>,
    subtitle: Option<String>,
    body: Option<String>,
    href: Option<Url>,
    command: Option<Command>,
    silent: bool,
}

impl Notification {
    /// Creates a new notification with default options.
    ///
    /// Call methods on the returned instance to configure it.
    pub fn new(swiftbar: SwiftBar) -> Result<Self, PluginNameError> {
        Ok(Self {
            swiftbar,
            plugin_name: swiftbar.plugin_name()?,
            title: None,
            subtitle: None,
            body: None,
            href: None,
            command: None,
            silent: false,
        })
    }

    /// Sets the title for this notification.
    pub fn title(mut self, title: impl ToString) -> Self {
        self.title = Some(title.to_string());
        self
    }

    /// Sets the subtitle for this notification.
    pub fn subtitle(mut self, subtitle: impl ToString) -> Self {
        self.subtitle = Some(subtitle.to_string());
        self
    }

    /// Sets the text for this notification.
    pub fn body(mut self, body: impl ToString) -> Self {
        self.body = Some(body.to_string());
        self
    }

    /// Adds an URL that will be opened when this notification is clicked.
    pub fn href(mut self, href: impl IntoUrl) -> Result<Self, url::ParseError> {
        self.href = Some(href.into_url()?);
        Ok(self)
    }

    /// Makes this notification run the given command when clicked.
    pub fn command<C: TryInto<Command>>(mut self, cmd: C) -> Result<Self, NotificationCommandError<C>>
    where C::Error: std::error::Error {
        if build_ge!(self.swiftbar, 402) {
            self.command = Some(cmd.try_into().map_err(NotificationCommandError::Command)?);
            Ok(self)
        } else {
            Err(NotificationCommandError::UnsupportedSwiftBarVersion)
        }
    }

    /// Disables sound for this notification.
    pub fn silent(mut self) -> Self {
        self.silent = true;
        self
    }

    /// Displays this notification.
    pub fn send(&self) -> io::Result<()> {
        open(self.into_url().expect("failed to build SwiftBar notification URL").as_str())
    }
}

impl IntoUrl for Notification {
    fn into_url(self) -> Result<Url, url::ParseError> {
        (&self).into_url()
    }
}

impl<'a> IntoUrl for &'a Notification {
    fn into_url(self) -> Result<Url, url::ParseError> {
        let Notification { swiftbar: _, plugin_name, title, subtitle, body, command, href, silent } = self;
        Url::parse_with_params("swiftbar://notify", iter::once((Cow::Borrowed("plugin"), &**plugin_name))
            .chain(title.as_deref().map(|title| (Cow::Borrowed("title"), title)))
            .chain(subtitle.as_deref().map(|subtitle| (Cow::Borrowed("subtitle"), subtitle)))
            .chain(body.as_deref().map(|body| (Cow::Borrowed("body"), body)))
            .chain(command.iter().flat_map(|command| iter::once((Cow::Borrowed("bash"), &*command.params.cmd))
                .chain(command.params.params.iter().enumerate().map(|(n, arg)| (Cow::Owned(format!("param{}", n + 1)), &**arg)))
                .chain((!command.terminal).then(|| (Cow::Borrowed("terminal"), "false")))
            ))
            .chain(href.as_ref().map(|href| (Cow::Borrowed("href"), href.as_str())))
            .chain(silent.then(|| (Cow::Borrowed("silent"), "true")))
        )
    }
}

/// A type that [streams](https://github.com/swiftbar/SwiftBar#streamable) menus from an iterator.
///
/// Note that the following [plugin metadata](https://github.com/swiftbar/SwiftBar#script-metadata) items must be set for this to work:
/// * `<swiftbar.type>streamable</swiftbar.type>`
/// * `<swiftbar.useTrailingStreamSeparator>true</swiftbar.useTrailingStreamSeparator>`
///
/// The [`cargo-bitbar`](https://crates.io/crates/cargo-bitbar) crate can be used to add this metadata to the plugin. First, add this to your *workspace* manifest:
///
/// ```toml
/// [workspace.metadata.bitbar]
/// type = "streamable"
/// ```
///
/// Then, after building the plugin, run `cargo bitbar attr target/release/my-bitbar-plugin`.
pub struct BlockingStream<'a, I: MainOutput> {
    swiftbar: SwiftBar,
    inner: Box<dyn Iterator<Item = I> + 'a>,
}

impl<'a, I: MainOutput> BlockingStream<'a, I> {
    #[allow(missing_docs)]
    pub fn new(swiftbar: SwiftBar, iter: impl IntoIterator<Item = I> + 'a) -> Self {
        Self { swiftbar, inner: Box::new(iter.into_iter()) }
    }
}

impl<'a, I: MainOutput> MainOutput for BlockingStream<'a, I> {
    fn main_output(self, error_template_image: Option<Image>) {
        if build_ge!(self.swiftbar, 399) {
            for elt in self.inner {
                elt.main_output(error_template_image.clone());
                println!("~~~");
            }
        } else {
            for elt in self.inner {
                println!("~~~");
                elt.main_output(error_template_image.clone());
            }
        }
    }
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
/// A type that [streams](https://github.com/swiftbar/SwiftBar#streamable) menus from a stream (async iterator).
///
/// Note that the following [plugin metadata](https://github.com/swiftbar/SwiftBar#script-metadata) items must be set for this to work:
/// * `<swiftbar.type>streamable</swiftbar.type>`
/// * `<swiftbar.useTrailingStreamSeparator>true</swiftbar.useTrailingStreamSeparator>`
///
/// The [`cargo-bitbar`](https://crates.io/crates/cargo-bitbar) crate can be used to add this metadata to the plugin. First, add this to your *workspace* manifest:
///
/// ```toml
/// [workspace.metadata.bitbar]
/// type = "streamable"
/// ```
///
/// Then, after building the plugin, run `cargo bitbar attr target/release/my-bitbar-plugin`.
pub struct Stream<'a, I: AsyncMainOutput<'a> + 'a> {
    swiftbar: SwiftBar,
    inner: Pin<Box<dyn futures::stream::Stream<Item = I> + 'a>>,
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<'a, I: AsyncMainOutput<'a> + 'a> Stream<'a, I> {
    #[allow(missing_docs)]
    pub fn new(swiftbar: SwiftBar, stream: impl futures::stream::Stream<Item = I> + 'a) -> Self {
        Self { swiftbar, inner: Box::pin(stream) }
    }
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<'a, I: AsyncMainOutput<'a> + 'a> AsyncMainOutput<'a> for Stream<'a, I> {
    fn main_output(mut self, error_template_image: Option<Image>) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
        if build_ge!(self.swiftbar, 399) {
            Box::pin(async move {
                while let Some(elt) = self.inner.next().await {
                    elt.main_output(error_template_image.clone()).await;
                    println!("~~~");
                }
            })
        } else {
            Box::pin(async move {
                while let Some(elt) = self.inner.next().await {
                    println!("~~~");
                    elt.main_output(error_template_image.clone()).await;
                }
            })
        }
    }
}