Skip to content

Commit

Permalink
feat(pwsh): Support vi command mode indicator (#5049)
Browse files Browse the repository at this point in the history
Support vi command mode in powershell
  • Loading branch information
Nemo157 committed Jun 12, 2023
1 parent ed68fd6 commit 3180509
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/init/starship.ps1
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
38 changes: 35 additions & 3 deletions src/modules/character.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 3180509

Please sign in to comment.