use std::{borrow::Cow, path::Path, process::Command};
use crate::{backend::CommandBackend, InputBox, InputMode};
#[derive(Clone, Debug)]
pub struct Zenity {
path: Cow<'static, Path>,
}
impl Zenity {
pub fn new() -> Self {
Self {
path: Path::new("zenity").into(),
}
}
pub fn custom(path: impl Into<Cow<'static, Path>>) -> Self {
Self { path: path.into() }
}
}
impl Default for Zenity {
fn default() -> Self {
Self::new()
}
}
impl CommandBackend for Zenity {
fn build_command<'a>(&self, input: &'a InputBox<'a>) -> (Command, Option<Cow<'a, str>>) {
let mut cmd = Command::new(&*self.path);
let stdin = match input.mode {
InputMode::Text | InputMode::Password => {
cmd.arg("--entry");
if input.mode == InputMode::Password {
cmd.arg("--hide-text");
}
cmd.args(["--entry-text", &*input.default]);
None
}
InputMode::Multiline => {
cmd.args(["--text-info", "--editable"]);
if input.scroll_to_end {
cmd.arg("--auto-scroll");
}
Some(Cow::Borrowed(&*input.default))
}
};
if let Some(title) = &input.title {
cmd.args(["--title", title]);
}
if let Some(prompt) = &input.prompt {
cmd.args(["--text", prompt]);
}
if let Some(label) = &input.cancel_label {
cmd.args(["--cancel-label", label]);
}
if let Some(label) = &input.ok_label {
cmd.args(["--ok-label", label]);
}
if let Some(width) = input.width {
cmd.args(["--width", &width.to_string()]);
}
if let Some(height) = input.height {
cmd.args(["--height", &height.to_string()]);
}
(cmd, stdin)
}
}
#[derive(Clone, Debug)]
pub struct Yad {
path: Cow<'static, Path>,
item_separator: u8,
}
impl Yad {
pub fn new() -> Self {
Self::custom(Path::new("yad"))
}
pub fn custom(path: impl Into<Cow<'static, Path>>) -> Self {
Self {
path: path.into(),
item_separator: b'!',
}
}
pub fn with_item_separator(mut self, sep: u8) -> Self {
self.item_separator = sep;
self
}
}
impl Default for Yad {
fn default() -> Self {
Self::new()
}
}
impl CommandBackend for Yad {
fn build_command<'a>(&self, input: &'a InputBox<'a>) -> (Command, Option<Cow<'a, str>>) {
let mut cmd = Command::new(&*self.path);
let stdin = match input.mode {
InputMode::Text | InputMode::Password => {
cmd.arg("--entry");
if input.mode == InputMode::Password {
cmd.arg("--hide-text");
}
if let Some(prompt) = &input.prompt {
cmd.args(["--entry-label", prompt]);
}
cmd.args(["--entry-text", &*input.default]);
None
}
InputMode::Multiline => {
cmd.args(["--text-info", "--editable"]);
if let Some(prompt) = &input.prompt {
cmd.args(["--text", prompt]);
}
if input.auto_wrap {
cmd.arg("--wrap");
}
if input.scroll_to_end {
cmd.arg("--auto-scroll");
}
Some(Cow::Borrowed(&*input.default))
}
};
if let Some(title) = &input.title {
cmd.args(["--title", title]);
}
if input.cancel_label.is_some() || input.ok_label.is_some() {
let sep = char::from_u32(self.item_separator as _).unwrap();
cmd.args(["--item-separator", &sep.to_string()]);
cmd.arg("--button");
if let Some(label) = &input.cancel_label {
cmd.arg(format!("{label}{sep}gtk-cancel:1"));
} else {
cmd.arg("yad-cancel:1");
}
cmd.arg("--button");
if let Some(label) = &input.ok_label {
cmd.arg(format!("{label}{sep}gtk-ok:0"));
} else {
cmd.arg("yad-ok:0");
}
}
if let Some(width) = input.width {
cmd.args(["--width", &width.to_string()]);
}
if let Some(height) = input.height {
cmd.args(["--height", &height.to_string()]);
}
(cmd, stdin)
}
}