use std::{borrow::Cow, path::Path, process::Command};
use serde_json::json;
use crate::{backend::CommandBackend, InputBox, DEFAULT_CANCEL_LABEL, DEFAULT_OK_LABEL};
const PS_SCRIPT: &str = include_str!("inputbox.ps1");
#[derive(Clone, Debug)]
pub struct PSScript {
path: Cow<'static, Path>,
}
impl PSScript {
pub fn new() -> Self {
Self {
path: Path::new("powershell").into(),
}
}
pub fn custom(path: impl Into<Cow<'static, Path>>) -> Self {
Self { path: path.into() }
}
}
impl Default for PSScript {
fn default() -> Self {
Self::new()
}
}
impl CommandBackend for PSScript {
fn build_command<'a>(&self, input: &'a InputBox<'a>) -> (Command, Option<Cow<'a, str>>) {
let cancel_label = input
.cancel_label
.as_deref()
.unwrap_or(DEFAULT_CANCEL_LABEL);
let ok_label = input.ok_label.as_deref().unwrap_or(DEFAULT_OK_LABEL);
let value = json!({
"title": input.title,
"prompt": input.prompt,
"default": input.default,
"mode": input.mode.as_str(),
"width": input.width,
"height": input.height,
"cancel_label": cancel_label,
"ok_label": ok_label,
"auto_wrap": input.auto_wrap,
"scroll_to_end": input.scroll_to_end,
});
let stdin = value.to_string();
let mut cmd = Command::new(&*self.path);
cmd.args(["-NoProfile", "-NoLogo", "-Command", PS_SCRIPT]);
(cmd, Some(Cow::Owned(stdin)))
}
}