use crate::sdk::plugin::DevicePlugin;
use crate::shell::{CommandOutput, Shell};
pub(crate) fn plugin<T>(
shell: &Shell,
f: impl FnOnce(&Box<dyn DevicePlugin>) -> Result<T, String>,
) -> Result<T, CommandOutput> {
let guard = match shell.plugin.lock() {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
};
match guard.as_ref() {
Some(p) => f(p).map_err(|e| CommandOutput::error(e, 1)),
None => Err(CommandOutput::not_supported("device plugin")),
}
}
fn ok(msg: &str) -> CommandOutput {
CommandOutput::success(format!("{msg}\n"))
}
impl Shell {
pub(crate) fn open_is_recent(&self, key: &str) -> bool {
let now = std::time::Instant::now();
let mut m = self.open_recent.borrow_mut();
m.retain(|_, t| now.duration_since(*t) < std::time::Duration::from_secs(30));
m.get(key)
.map(|t| now.duration_since(*t) < std::time::Duration::from_millis(1500))
.unwrap_or(false)
}
pub(crate) fn mark_open(&self, key: &str) {
self.open_recent
.borrow_mut()
.insert(key.to_string(), std::time::Instant::now());
}
}
impl Shell {
pub(crate) fn device_host_path(&self, vpath: &str) -> Result<String, CommandOutput> {
match self.vfs.resolve(vpath, &self.cwd) {
Ok(p) => Ok(p.to_string_lossy().to_string()),
Err(e) => Err(CommandOutput::error(format!("{vpath}: {e}\n"), 1)),
}
}
}
impl Shell {
pub fn cmd_camera(&self, args: &[&str]) -> CommandOutput {
if args.first().is_some_and(|a| matches!(*a, "-h" | "--help")) {
return CommandOutput::success(
"usage: camera [PATH]\n Capture a photo and save it to PATH (default /photo.jpg).\n"
.to_string(),
);
}
let path = args
.first()
.map(|s| s.to_string())
.unwrap_or_else(|| "/photo.jpg".to_string());
if let Some(perm) = self.check_device_permission("camera", "photo") {
return perm;
}
let host = match self.device_host_path(&path) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| p.take_photo(&host)) {
Ok(()) => CommandOutput::success(format!("Photo saved to {}\n", path)),
Err(e) => e,
}
}
pub fn cmd_screencapture(&self, args: &[&str]) -> CommandOutput {
if args.first().is_some_and(|a| matches!(*a, "-h" | "--help")) {
return CommandOutput::success(
"usage: screencapture [PATH]\n Capture the screen to PATH (default /screenshot.png).\n"
.to_string(),
);
}
let path = args
.first()
.map(|s| s.to_string())
.unwrap_or_else(|| "/screenshot.png".to_string());
if let Some(perm) = self.check_device_permission("screen", "capture") {
return perm;
}
let host = match self.device_host_path(&path) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| p.take_screenshot(&host)) {
Ok(()) => CommandOutput::success(format!("Screenshot saved to {}\n", path)),
Err(e) => e,
}
}
pub fn cmd_photolib(&self, args: &[&str]) -> CommandOutput {
let mut media_type = "image";
let mut count = 1u32;
let mut output_dir = "/".to_string();
let mut i = 0;
while i < args.len() {
match args[i] {
"--video" => media_type = "video",
"-n" | "--count" => {
if i + 1 < args.len() {
count = args[i + 1].parse::<u32>().unwrap_or(1).max(1);
i += 1;
}
}
"-o" | "--output" => {
if i + 1 < args.len() {
output_dir = args[i + 1].to_string();
i += 1;
}
}
arg if !arg.starts_with('-') => {
output_dir = arg.to_string();
}
_ => {}
}
i += 1;
}
if let Some(perm) = self.check_device_permission("photolib", "read") {
return perm;
}
let output_dir = match self.device_host_path(&output_dir) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| {
if media_type == "video" {
p.pick_video(&output_dir).map(|p| {
serde_json::to_string_pretty(&serde_json::json!({"video": p})).unwrap_or(p)
})
} else {
let files = p.pick_photo(&output_dir, count)?;
let json = serde_json::json!({"photos": files});
serde_json::to_string_pretty(&json).map_err(|e| e.to_string())
}
}) {
Ok(out) => CommandOutput::success(format!("{}\n", out)),
Err(e) => e,
}
}
pub fn cmd_record(&self, args: &[&str]) -> CommandOutput {
let mut path = "/recording.wav".to_string();
let mut duration: u32 = 10;
let mut i = 0;
while i < args.len() {
match args[i] {
"-d" | "--duration" => {
if i + 1 < args.len() {
duration = args[i + 1].parse().unwrap_or(10);
i += 1;
}
}
"-o" | "--output" => {
if i + 1 < args.len() {
path = args[i + 1].to_string();
i += 1;
}
}
arg if !arg.starts_with('-') => {
path = arg.to_string();
}
_ => {}
}
i += 1;
}
if let Some(perm) = self.check_device_permission("microphone", "record") {
return perm;
}
let duration = duration.max(1);
let host = match self.device_host_path(&path) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| p.record_audio(&host, duration)) {
Ok(()) => CommandOutput::success(format!("Recorded {}s to {}\n", duration, path)),
Err(e) => e,
}
}
pub fn cmd_play(&self, args: &[&str]) -> CommandOutput {
let path = match args.first() {
Some(p) => p.to_string(),
None => return CommandOutput::error("play: missing file path\n".to_string(), 1),
};
let host = match self.device_host_path(&path) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| p.play_audio(&host)) {
Ok(()) => ok("playing"),
Err(e) => e,
}
}
pub fn cmd_say(&self, args: &[&str]) -> CommandOutput {
let text = args.join(" ");
if text.is_empty() {
return CommandOutput::error("say: missing text\n".to_string(), 1);
}
match plugin(self, |p| p.text_to_speech(&text)) {
Ok(()) => ok("spoken"),
Err(e) => e,
}
}
pub fn cmd_speech(&self, args: &[&str]) -> CommandOutput {
let path = match args.first() {
Some(p) => p.to_string(),
None => {
return CommandOutput::error("speech: missing audio file path\n".to_string(), 1)
}
};
if let Some(perm) = self.check_device_permission("microphone", "speech") {
return perm;
}
let host = match self.device_host_path(&path) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p| p.speech_to_text(&host)) {
Ok(text) => CommandOutput::success(format!("{}\n", text)),
Err(e) => e,
}
}
pub fn cmd_contacts(&self, args: &[&str]) -> CommandOutput {
let mut query = String::new();
let mut limit = 50u32;
let mut contact_id: Option<String> = None;
let mut i = 0;
while i < args.len() {
match args[i] {
"get" => {
if i + 1 < args.len() {
contact_id = Some(args[i + 1].to_string());
i += 1;
}
}
"search" => {
if i + 1 < args.len() {
query = args[i + 1].to_string();
i += 1;
}
}
"-n" | "--limit" => {
if i + 1 < args.len() {
limit = args[i + 1].parse().unwrap_or(50);
i += 1;
}
}
_ => {}
}
i += 1;
}
if let Some(perm) = self.check_device_permission("contacts", "read") {
return perm;
}
match plugin(self, |p| {
if let Some(cid) = contact_id {
let c = p.get_contact(&cid)?;
serde_json::to_string_pretty(&c).map_err(|e| e.to_string())
} else {
let list = p.list_contacts(&query, limit)?;
serde_json::to_string_pretty(&list).map_err(|e| e.to_string())
}
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
}
}
pub fn cmd_location(&self, _args: &[&str]) -> CommandOutput {
if let Some(perm) = self.check_device_permission("location", "gps") {
return perm;
}
match plugin(self, |p| {
let loc = p.get_location()?;
serde_json::to_string_pretty(&loc).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
}
}
pub fn cmd_clipboard(&self, args: &[&str]) -> CommandOutput {
if args.first() == Some(&"set") {
let text = args[1..].join(" ");
if text.is_empty() {
return CommandOutput::error("clipboard set: missing text\n".to_string(), 1);
}
return match plugin(self, |p| p.set_clipboard(&text)) {
Ok(()) => ok("clipboard set"),
Err(e) => e,
};
}
match plugin(self, |p| p.get_clipboard()) {
Ok(text) => CommandOutput::success(format!("{}\n", text)),
Err(e) => e,
}
}
pub fn cmd_pbcopy(&self, args: &[&str]) -> CommandOutput {
let text = args.join(" ");
if text.is_empty() {
return CommandOutput::error("pbcopy: missing text\n".to_string(), 1);
}
match plugin(self, |p| p.set_clipboard(&text)) {
Ok(()) => ok("clipboard set"),
Err(e) => e,
}
}
pub fn cmd_sensor(&self, args: &[&str]) -> CommandOutput {
let sub = args.first().map(|s| *s).unwrap_or("list");
match sub {
"orientation" => match plugin(self, |p| {
let o = p.get_orientation()?;
serde_json::to_string_pretty(&o).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
},
"motion" => match plugin(self, |p| {
let m = p.get_motion()?;
serde_json::to_string_pretty(&m).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
},
"light" => match plugin(self, |p| p.get_ambient_light()) {
Ok(v) => CommandOutput::success(format!("{} lux\n", v)),
Err(e) => e,
},
"proximity" => match plugin(self, |p| p.get_proximity()) {
Ok(v) => CommandOutput::success(format!("{}\n", if v { "near" } else { "far" })),
Err(e) => e,
},
"list" | _ => match plugin(self, |p| {
let sensors = p.list_sensors()?;
serde_json::to_string_pretty(&sensors).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
},
}
}
pub fn cmd_notify(&self, args: &[&str]) -> CommandOutput {
let mut title_opt: Option<String> = None;
let mut body_opt: Option<String> = None;
let mut rest: Vec<&str> = Vec::new();
let mut i = 0;
while i < args.len() {
match args[i] {
"--title" | "-t" => {
if i + 1 < args.len() {
title_opt = Some(args[i + 1].to_string());
i += 1;
}
}
"--message" | "--body" | "-m" | "-b" => {
if i + 1 < args.len() {
body_opt = Some(args[i + 1].to_string());
i += 1;
}
}
a if a.starts_with("--title=") => {
title_opt = Some(a["--title=".len()..].to_string())
}
a if a.starts_with("--message=") => {
body_opt = Some(a["--message=".len()..].to_string())
}
a => rest.push(a),
}
i += 1;
}
if title_opt.is_some() || body_opt.is_some() {
let title = title_opt.unwrap_or_else(|| "fastshell".to_string());
let body = body_opt.unwrap_or_default();
let sound = args.contains(&"--sound");
return match plugin(self, |p| p.send_notification(&title, &body, sound)) {
Ok(()) => ok("notified"),
Err(e) => e,
};
}
let args: &[&str] = &rest;
let (title, body) = if let Some(pos) = args.iter().position(|&a| a == "--") {
let title = args[..pos].join(" ");
let body = args[pos + 1..].join(" ");
(title, body)
} else if args.len() >= 2 {
(args[0].to_string(), args[1..].join(" "))
} else if args.len() == 1 {
("fastshell".to_string(), args[0].to_string())
} else {
return CommandOutput::error("notify-send: missing message\n".to_string(), 1);
};
let sound = args.contains(&"--sound");
match plugin(self, |p| p.send_notification(&title, &body, sound)) {
Ok(()) => ok("notified"),
Err(e) => e,
}
}
pub fn cmd_share(&self, args: &[&str]) -> CommandOutput {
let mut path: Option<String> = None;
let mut text: Option<String> = None;
let mut mime = "application/octet-stream".to_string();
let mut i = 0;
while i < args.len() {
match args[i] {
"--text" => {
if i + 1 < args.len() {
text = Some(args[i + 1].to_string());
i += 1;
}
}
"--mime" => {
if i + 1 < args.len() {
mime = args[i + 1].to_string();
i += 1;
}
}
arg if !arg.starts_with('-') => {
path = Some(arg.to_string());
}
_ => {}
}
i += 1;
}
match (path, text) {
(Some(p), _) => {
let host = match self.device_host_path(&p) {
Ok(h) => h,
Err(e) => return e,
};
match plugin(self, |p2| p2.share_file(&host, &mime)) {
Ok(()) => ok("shared"),
Err(e) => e,
}
}
(_, Some(t)) => match plugin(self, |p2| p2.share_text(&t)) {
Ok(()) => ok("shared"),
Err(e) => e,
},
_ => CommandOutput::error("share: missing file or --text\n".to_string(), 1),
}
}
pub fn cmd_open_url(&self, args: &[&str]) -> CommandOutput {
if matches!(args.first(), Some(&"-h") | Some(&"--help")) {
return CommandOutput::success(
"usage: open <url>\n Open a URL via the host OS (http/https/mailto/tel/…).\n To open app/system settings use `open_settings`.\n"
.to_string(),
);
}
let url = match args.first() {
Some(u) => u.to_string(),
None => return CommandOutput::error("open: missing URL\n".to_string(), 1),
};
let key = format!("open:{url}");
if self.open_is_recent(&key) {
return ok(&format!("already opening: {url}"));
}
match plugin(self, |p| p.open_url(&url)) {
Ok(()) => {
self.mark_open(&key);
ok(&format!("opened: {url}"))
}
Err(e) => e,
}
}
pub fn cmd_open_settings(&self, args: &[&str]) -> CommandOutput {
if matches!(args.first(), Some(&"-h") | Some(&"--help")) {
return CommandOutput::success(
"usage: open_settings [app|system|accessibility|notification]\n Open a settings page (default: app).\n"
.to_string(),
);
}
let target = match args.first() {
None => "app",
Some(t) => match *t {
"app" | "system" | "accessibility" | "notification" => *t,
other => {
return CommandOutput::error(
format!(
"open_settings: unknown target '{other}' (app|system|accessibility|notification)\n"
),
1,
)
}
},
};
let key = format!("settings:{target}");
if self.open_is_recent(&key) {
return ok(&format!("already opening: {target}"));
}
match plugin(self, |p| p.open_settings(target)) {
Ok(()) => {
self.mark_open(&key);
ok(&format!("settings opened: {target}"))
}
Err(e) => e,
}
}
pub fn cmd_auth(&self, args: &[&str]) -> CommandOutput {
let reason = if args.first() == Some(&"bio") {
args.get(1)
.map(|s| s.to_string())
.unwrap_or_else(|| "Authenticate".to_string())
} else {
args.join(" ")
};
if reason.is_empty() {
return CommandOutput::error("auth: missing reason\n".to_string(), 1);
}
match plugin(self, |p| p.authenticate_biometric(&reason)) {
Ok(true) => CommandOutput::success("authenticated\n".to_string()),
Ok(false) => CommandOutput::error("authentication failed\n".to_string(), 1),
Err(e) => e,
}
}
pub fn cmd_battery(&self, _args: &[&str]) -> CommandOutput {
match plugin(self, |p| {
let b = p.get_battery()?;
serde_json::to_string_pretty(&b).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
}
}
pub fn cmd_vibrate(&self, args: &[&str]) -> CommandOutput {
let ms: u32 = args.first().and_then(|s| s.parse().ok()).unwrap_or(200);
match plugin(self, |p| p.vibrate(ms)) {
Ok(()) => ok("vibrated"),
Err(e) => e,
}
}
pub fn cmd_screen(&self, args: &[&str]) -> CommandOutput {
let mut i = 0;
while i < args.len() {
match args[i] {
"brightness" => {
if i + 1 < args.len() {
if let Ok(level) = args[i + 1].parse::<f64>() {
let level = level.clamp(0.0, 1.0);
return match plugin(self, |p| p.set_brightness(level)) {
Ok(()) => ok("brightness set"),
Err(e) => e,
};
}
}
return CommandOutput::error(
"screen brightness: missing or invalid level (0.0-1.0)\n".to_string(),
1,
);
}
"on" => {
return match plugin(self, |p| p.keep_screen_on(true)) {
Ok(()) => ok("screen on"),
Err(e) => e,
};
}
"off" => {
return match plugin(self, |p| p.keep_screen_on(false)) {
Ok(()) => ok("screen off"),
Err(e) => e,
};
}
_ => {}
}
i += 1;
}
CommandOutput::error(
"screen: usage: screen brightness <0.0-1.0> | on | off\n".to_string(),
1,
)
}
pub fn cmd_device(&self, args: &[&str]) -> CommandOutput {
let sub = args.first().map(|s| *s);
match sub {
Some("info") => match plugin(self, |p| {
let d = p.get_device_info()?;
serde_json::to_string_pretty(&d).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
},
Some("network") => match plugin(self, |p| {
let n = p.get_network_type()?;
serde_json::to_string_pretty(&n).map_err(|e| e.to_string())
}) {
Ok(json) => CommandOutput::success(format!("{}\n", json)),
Err(e) => e,
},
_ => CommandOutput::error(
"device: usage: device info | device network\n".to_string(),
1,
),
}
}
}