Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to work better on windows with pwsh #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function prompt {
$env:code = $LASTEXITCODE
$env:cmdtime = [uint64]($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime).TotalMilliseconds
$env:jobs = @(Get-Job).Count
Start-Process -Wait -NoNewWindow silver lprint
Start-Process -Wait -NoNewWindow -FilePath silver
"$([char]0x1b)[0m"
}
$Env:VIRTUAL_ENV_DISABLE_PROMPT = 1
114 changes: 64 additions & 50 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ static CONFIG: Lazy<config::Config> = Lazy::new(|| {
pub struct Segment {
background: String,
foreground: String,
value: String,
value: String,
}

impl Default for Segment {
fn default() -> Self {
Self {
background: "none".to_owned(),
foreground: "none".to_owned(),
value: String::new(),
value: String::new(),
}
}
}
Expand All @@ -43,42 +43,42 @@ fn main() {
let process = sys.get_process(get_current_pid().unwrap()).unwrap();
let parent = sys.get_process(process.parent().unwrap()).unwrap();
let shell = parent.name().trim();

let opt = cli::Silver::from_args();

if let Some(path) = opt.config {
let path = Path::new(path.as_str()).canonicalize().unwrap();
CONFIG_PATH.set(path).unwrap()
}

match opt.cmd {
Command::Init => {
print!(
"{}",
match shell {
"bash" => include_str!("init.bash"),
"powershell" | "pwsh" => include_str!("init.ps1"),
"ion" => include_str!("init.ion"),
_ =>
panic!(
"unknown shell: \"{}\". Supported shells: bash, ion, powershell",
shell
),
}
.replace(
"silver",
format!(
"silver{}",
if let Some(path) = CONFIG_PATH.get() {
format!(" --config {}", path.display())
} else {
String::new()
}
)
.as_str()
Command::Init => print!(
"{}",
match shell {
"bash" => include_str!("init.bash"),
"powershell" | "pwsh" | "powershell.exe" | "pwsh.exe" => include_str!("init.ps1"),
// "codelldb.exe" => include_str!("init.ps1"), //only used when debugging with codelldb on windows
"ion" => include_str!("init.ion"),
_ => panic!(
"unknown shell: \"{}\". Supported shells: bash, ion, powershell",
shell
),
}
.replace(
"silver",
format!(
"silver {}",
if let Some(path) = CONFIG_PATH.get() {
let path_canon = path.canonicalize().expect("invalid path");
format!(
"-ArgumentList @('--config','{}','lprint')",
adjust_canonicalization(path_canon)
)
} else {
"-ArgumentList @('lprint')".to_string()
}
)
.as_str()
)
}
),
Command::Lprint => {
print::prompt(&shell, &CONFIG.left, |_, (_, c, n)| {
vec![
Expand All @@ -105,29 +105,43 @@ fn main() {
print!(" ")
}

Command::Rprint => {
print::prompt(&shell, &CONFIG.right, |_, (p, c, _)| {
vec![
if p.background == c.background {
(
c.background.to_owned(),
c.foreground.to_owned(),
CONFIG.separator.right.thin.to_owned(),
)
} else {
(
p.background.to_owned(),
c.background.to_owned(),
CONFIG.separator.right.thick.to_owned(),
)
},
Command::Rprint => print::prompt(&shell, &CONFIG.right, |_, (p, c, _)| {
vec![
if p.background == c.background {
(
c.background.to_owned(),
c.foreground.to_owned(),
format!(" {} ", c.value),
),
]
})
}
CONFIG.separator.right.thin.to_owned(),
)
} else {
(
p.background.to_owned(),
c.background.to_owned(),
CONFIG.separator.right.thick.to_owned(),
)
},
(
c.background.to_owned(),
c.foreground.to_owned(),
format!(" {} ", c.value),
),
]
}),
}
}

#[cfg(not(target_os = "windows"))]
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
p.as_ref().display().to_string()
}

#[cfg(target_os = "windows")]
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
const VERBATIM_PREFIX: &str = r#"\\?\"#;
let p = p.as_ref().display().to_string();
if p.starts_with(VERBATIM_PREFIX) {
p[VERBATIM_PREFIX.len()..].to_string()
} else {
p
}
}