mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-08 20:45:24 +00:00
fix(pwsh): use adequate quoting when formatting preview commands for pwsh
This commit is contained in:
parent
6d26db2993
commit
e2f83bdaac
@ -2,7 +2,7 @@
|
|||||||
[[cable_channel]]
|
[[cable_channel]]
|
||||||
name = "git-diff"
|
name = "git-diff"
|
||||||
source_command = "git diff --name-only"
|
source_command = "git diff --name-only"
|
||||||
preview_command = "git diff --color=always {0}"
|
preview_command = "git diff --color=always -- {0}"
|
||||||
|
|
||||||
[[cable_channel]]
|
[[cable_channel]]
|
||||||
name = "git-reflog"
|
name = "git-reflog"
|
||||||
|
@ -4,7 +4,9 @@ use crate::channels::entry::Entry;
|
|||||||
use crate::channels::entry::PreviewType;
|
use crate::channels::entry::PreviewType;
|
||||||
use crate::channels::OnAir;
|
use crate::channels::OnAir;
|
||||||
use crate::matcher::{config::Config, injector::Injector, Matcher};
|
use crate::matcher::{config::Config, injector::Injector, Matcher};
|
||||||
|
use crate::utils::command::shell_command;
|
||||||
use crate::utils::indices::sep_name_and_value_indices;
|
use crate::utils::indices::sep_name_and_value_indices;
|
||||||
|
use crate::utils::shell::Shell;
|
||||||
use devicons::FileIcon;
|
use devicons::FileIcon;
|
||||||
use rustc_hash::FxBuildHasher;
|
use rustc_hash::FxBuildHasher;
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
@ -32,17 +34,18 @@ pub struct Channel {
|
|||||||
const NUM_THREADS: usize = 1;
|
const NUM_THREADS: usize = 1;
|
||||||
|
|
||||||
const FILE_ICON_STR: &str = "nu";
|
const FILE_ICON_STR: &str = "nu";
|
||||||
const SHELL_ENV_VAR: &str = "SHELL";
|
|
||||||
|
|
||||||
fn get_current_shell() -> Option<String> {
|
fn get_raw_aliases(shell: Shell) -> Vec<String> {
|
||||||
std::env::var(SHELL_ENV_VAR).ok()
|
// this needs to be run in an interactive shell in order to get the aliases
|
||||||
}
|
let mut command = shell_command(true);
|
||||||
|
|
||||||
fn get_raw_aliases(shell: &str) -> Vec<String> {
|
let output = match shell {
|
||||||
let output = std::process::Command::new(shell)
|
Shell::PowerShell => {
|
||||||
.arg("-i")
|
command.arg("Get-Alias | Format-List -Property Name, Definition")
|
||||||
.arg("-c")
|
}
|
||||||
.arg("alias")
|
Shell::Cmd => command.arg("doskey /macros"),
|
||||||
|
_ => command.arg("-i").arg("alias").arg("2>/dev/null"),
|
||||||
|
}
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process");
|
.expect("failed to execute process");
|
||||||
|
|
||||||
@ -151,8 +154,7 @@ impl OnAir for Channel {
|
|||||||
|
|
||||||
#[allow(clippy::unused_async)]
|
#[allow(clippy::unused_async)]
|
||||||
async fn load_aliases(injector: Injector<Alias>) {
|
async fn load_aliases(injector: Injector<Alias>) {
|
||||||
let raw_shell = get_current_shell().unwrap_or("bash".to_string());
|
let shell = Shell::from_env().unwrap_or_default();
|
||||||
let shell = raw_shell.split('/').last().unwrap();
|
|
||||||
debug!("Current shell: {}", shell);
|
debug!("Current shell: {}", shell);
|
||||||
let raw_aliases = get_raw_aliases(shell);
|
let raw_aliases = get_raw_aliases(shell);
|
||||||
|
|
||||||
@ -167,6 +169,8 @@ async fn load_aliases(injector: Injector<Alias>) {
|
|||||||
value.to_string(),
|
value.to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
debug!("Invalid alias format: {}", alias);
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
|
@ -110,7 +110,7 @@ impl Channel {
|
|||||||
#[allow(clippy::unused_async)]
|
#[allow(clippy::unused_async)]
|
||||||
async fn load_candidates(command: String, injector: Injector<String>) {
|
async fn load_candidates(command: String, injector: Injector<String>) {
|
||||||
debug!("Loading candidates from command: {:?}", command);
|
debug!("Loading candidates from command: {:?}", command);
|
||||||
let mut child = shell_command()
|
let mut child = shell_command(false)
|
||||||
.arg(command)
|
.arg(command)
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
|
@ -174,7 +174,7 @@ pub fn try_preview(
|
|||||||
let command = format_command(command, entry, command_re);
|
let command = format_command(command, entry, command_re);
|
||||||
debug!("Formatted preview command: {:?}", command);
|
debug!("Formatted preview command: {:?}", command);
|
||||||
|
|
||||||
let child = shell_command()
|
let child = shell_command(false)
|
||||||
.arg(&command)
|
.arg(&command)
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process");
|
.expect("failed to execute process");
|
||||||
|
@ -1,19 +1,21 @@
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
use super::shell::Shell;
|
||||||
pub fn shell_command() -> Command {
|
|
||||||
let mut cmd = Command::new("sh");
|
|
||||||
|
|
||||||
cmd.arg("-c");
|
pub fn shell_command(interactive: bool) -> Command {
|
||||||
|
let shell = Shell::from_env().unwrap_or_default();
|
||||||
cmd
|
let mut cmd = Command::new(shell.executable());
|
||||||
}
|
|
||||||
|
cmd.arg(match shell {
|
||||||
#[cfg(windows)]
|
Shell::PowerShell => "-Command",
|
||||||
pub fn shell_command() -> Command {
|
Shell::Cmd => "/C",
|
||||||
let mut cmd = Command::new("cmd");
|
_ => "-c",
|
||||||
|
});
|
||||||
cmd.arg("/c");
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
if interactive {
|
||||||
|
cmd.arg("-i");
|
||||||
|
}
|
||||||
|
|
||||||
cmd
|
cmd
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
use crate::cli::args::Shell as CliShell;
|
use crate::cli::args::Shell as CliShell;
|
||||||
use crate::config::shell_integration::ShellIntegrationConfig;
|
use crate::config::shell_integration::ShellIntegrationConfig;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use strum::Display;
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Display)]
|
||||||
pub enum Shell {
|
pub enum Shell {
|
||||||
Bash,
|
Bash,
|
||||||
Zsh,
|
Zsh,
|
||||||
@ -11,6 +13,62 @@ pub enum Shell {
|
|||||||
Cmd,
|
Cmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Shell {
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn default() -> Self {
|
||||||
|
Shell::Bash
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
fn default() -> Self {
|
||||||
|
Shell::PowerShell
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHELL_ENV_VAR: &str = "SHELL";
|
||||||
|
|
||||||
|
impl TryFrom<&str> for Shell {
|
||||||
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
|
fn try_from(value: &str) -> Result<Self> {
|
||||||
|
if value.contains("bash") {
|
||||||
|
Ok(Shell::Bash)
|
||||||
|
} else if value.contains("zsh") {
|
||||||
|
Ok(Shell::Zsh)
|
||||||
|
} else if value.contains("fish") {
|
||||||
|
Ok(Shell::Fish)
|
||||||
|
} else if value.contains("powershell") {
|
||||||
|
Ok(Shell::PowerShell)
|
||||||
|
} else if value.contains("cmd") {
|
||||||
|
Ok(Shell::Cmd)
|
||||||
|
} else {
|
||||||
|
Err(anyhow::anyhow!("Unsupported shell: {}", value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Shell {
|
||||||
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
|
pub fn from_env() -> Result<Self> {
|
||||||
|
if let Ok(shell) = std::env::var(SHELL_ENV_VAR) {
|
||||||
|
Shell::try_from(shell.as_str())
|
||||||
|
} else {
|
||||||
|
debug!("Environment variable {} not set", SHELL_ENV_VAR);
|
||||||
|
Ok(Shell::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn executable(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Shell::Bash => "bash",
|
||||||
|
Shell::Zsh => "zsh",
|
||||||
|
Shell::Fish => "fish",
|
||||||
|
Shell::PowerShell => "powershell",
|
||||||
|
Shell::Cmd => "cmd",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<CliShell> for Shell {
|
impl From<CliShell> for Shell {
|
||||||
fn from(val: CliShell) -> Self {
|
fn from(val: CliShell) -> Self {
|
||||||
match val {
|
match val {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user