feat(pwsh): Support vi command mode indicator (#5049)

Support vi command mode in powershell
This commit is contained in:
Nemo157 2023-06-12 11:05:44 +02:00 committed by GitHub
parent ed68fd6e3d
commit 318050976b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View File

@ -141,6 +141,10 @@ $null = New-Module starship {
$arguments += "--status=$($lastExitCodeForPrompt)"
if ([Microsoft.PowerShell.PSConsoleReadLine]::InViCommandMode()) {
$arguments += "--keymap=vi"
}
# Invoke Starship
$promptText = if ($script:TransientPrompt) {
$script:TransientPrompt = $false
@ -206,6 +210,12 @@ $null = New-Module starship {
)
)
try {
Set-PSReadLineOption -ViModeIndicator script -ViModeChangeHandler {
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
}
} catch {}
Export-ModuleMember -Function @(
"Enable-TransientPrompt"
"Disable-TransientPrompt"

View File

@ -35,9 +35,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// We do some environment detection in src/init.rs to translate.
// The result: in non-vi fish, keymap is always reported as "insert"
let mode = match (&context.shell, keymap) {
(Shell::Fish, "default") | (Shell::Zsh, "vicmd") | (Shell::Cmd, "vi") => {
ShellEditMode::Normal
}
(Shell::Fish, "default")
| (Shell::Zsh, "vicmd")
| (Shell::Cmd | Shell::PowerShell, "vi") => ShellEditMode::Normal,
(Shell::Fish, "visual") => ShellEditMode::Visual,
(Shell::Fish, "replace") => ShellEditMode::Replace,
(Shell::Fish, "replace_one") => ShellEditMode::ReplaceOne,
@ -260,4 +260,36 @@ mod test {
.collect();
assert_eq!(expected_other, actual);
}
#[test]
fn powershell_keymap() {
let expected_vicmd = Some(format!("{} ", Color::Green.bold().paint("")));
let expected_specified = Some(format!("{} ", Color::Green.bold().paint("V")));
let expected_other = Some(format!("{} ", Color::Green.bold().paint("")));
// powershell keymap is vi
let actual = ModuleRenderer::new("character")
.shell(Shell::PowerShell)
.keymap("vi")
.collect();
assert_eq!(expected_vicmd, actual);
// specified vicmd character
let actual = ModuleRenderer::new("character")
.config(toml::toml! {
[character]
vicmd_symbol = "[V](bold green)"
})
.shell(Shell::PowerShell)
.keymap("vi")
.collect();
assert_eq!(expected_specified, actual);
// powershell keymap is other
let actual = ModuleRenderer::new("character")
.shell(Shell::PowerShell)
.keymap("visual")
.collect();
assert_eq!(expected_other, actual);
}
}