Add exec command

Fixes #673
This commit is contained in:
Henrik Friedrichsen
2021-12-17 20:50:41 +01:00
parent 8deaedce54
commit 299c470cf9
5 changed files with 27 additions and 14 deletions

5
Cargo.lock generated
View File

@@ -1357,9 +1357,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.110" version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b58a4469763e4e3a906c4ed786e1c70512d16aa88f84dded826da42640fc6a1c" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]] [[package]]
name = "libdbus-sys" name = "libdbus-sys"
@@ -1731,6 +1731,7 @@ dependencies = [
"futures 0.3.18", "futures 0.3.18",
"ioctl-rs", "ioctl-rs",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"libc",
"librespot-core", "librespot-core",
"librespot-playback", "librespot-playback",
"librespot-protocol", "librespot-protocol",

View File

@@ -46,6 +46,7 @@ regex = "1"
ioctl-rs = { version = "0.2", optional = true } ioctl-rs = { version = "0.2", optional = true }
serde_cbor = "0.11.2" serde_cbor = "0.11.2"
pancurses = { version = "0.17.0", features = ["win32"] } pancurses = { version = "0.17.0", features = ["win32"] }
libc = "0.2.111"
[dependencies.rspotify] [dependencies.rspotify]
version = "0.11.3" version = "0.11.3"

View File

@@ -259,18 +259,19 @@ time with <kbd>Escape</kbd>.
The following commands are supported: The following commands are supported:
| Command | Action | | Command | Action |
| :--------------------------------- | :--------------------------------------------------------------- | |---|---|
| `quit` | Quit `ncspot`. | | `quit` | Quit `ncspot`. |
| `logout` | Remove any cached credentials from disk and quit `ncspot`. | | `logout` | Remove any cached credentials from disk and quit `ncspot`. |
| `toggle` | Toggle playback. | | `toggle` | Toggle playback. |
| `stop` | Stop playback. | | `stop` | Stop playback. |
| `previous` | Play previous track. | | `previous` | Play previous track. |
| `next` | Play next track. | | `next` | Play next track. |
| `clear` | Clear playlist. | | `clear` | Clear playlist. |
| `share <item>` | Copies a sharable URL of the item to the system clipboard. | | `share <item>` | Copies a sharable URL of the item to the system clipboard. |
| `newplaylist <name>` | Create new playlist with name `<name>`. | | `newplaylist <name>` | Create new playlist with name `<name>`. |
| `sort <sort_key> <sort_direction>` | Sort a playlist by `<sort_key>` in direction `<sort_direction>`. | | `sort <sort_key> <sort_direction>` | Sort a playlist by `<sort_key>` in direction `<sort_direction>`. |
| `exec <cmd>` | Executes a command in the system shell. Be aware that command output is printed to the terminal, so redirection to `/dev/null` e.g. by appending `2> /dev/null` may be necessary. |
Supported `<item>` are: Supported `<item>` are:

View File

@@ -136,6 +136,7 @@ pub enum Command {
Logout, Logout,
ShowRecommendations(TargetMode), ShowRecommendations(TargetMode),
Redraw, Redraw,
Execute(String),
} }
impl fmt::Display for Command { impl fmt::Display for Command {
@@ -191,7 +192,7 @@ impl fmt::Display for Command {
Command::Jump(mode) => match mode { Command::Jump(mode) => match mode {
JumpMode::Previous => "jumpprevious".to_string(), JumpMode::Previous => "jumpprevious".to_string(),
JumpMode::Next => "jumpnext".to_string(), JumpMode::Next => "jumpnext".to_string(),
JumpMode::Query(term) => format!("jump {}", term).to_string(), JumpMode::Query(term) => String::from(format!("jump {}", term)),
}, },
Command::Help => "help".to_string(), Command::Help => "help".to_string(),
Command::ReloadConfig => "reload".to_string(), Command::ReloadConfig => "reload".to_string(),
@@ -201,6 +202,7 @@ impl fmt::Display for Command {
Command::Logout => "logout".to_string(), Command::Logout => "logout".to_string(),
Command::ShowRecommendations(mode) => format!("similar {}", mode), Command::ShowRecommendations(mode) => format!("similar {}", mode),
Command::Redraw => "redraw".to_string(), Command::Redraw => "redraw".to_string(),
Command::Execute(cmd) => format!("exec {}", cmd),
}; };
// escape the command separator // escape the command separator
let repr = repr.replace(";", ";;"); let repr = repr.replace(";", ";;");
@@ -471,6 +473,7 @@ pub fn parse(input: &str) -> Option<Vec<Command>> {
.map(Command::ShowRecommendations), .map(Command::ShowRecommendations),
"noop" => Some(Command::Noop), "noop" => Some(Command::Noop),
"redraw" => Some(Command::Redraw), "redraw" => Some(Command::Redraw),
"exec" => Some(Command::Execute(args.join(" "))),
_ => None, _ => None,
}; };
commands.push(command?); commands.push(command?);

View File

@@ -253,6 +253,13 @@ impl CommandManager {
s.quit(); s.quit();
Ok(None) Ok(None)
} }
Command::Execute(cmd) => {
log::info!("Executing command: {}", cmd);
let cmd = std::ffi::CString::new(cmd.clone()).unwrap();
let result = unsafe { libc::system(cmd.as_ptr()) };
log::info!("Exit code: {}", result);
Ok(None)
}
Command::Jump(_) Command::Jump(_)
| Command::Move(_, _) | Command::Move(_, _)
| Command::Shift(_, _) | Command::Shift(_, _)