feat: add zsh, powershell, & windows-powershell binding support

Signed-off-by: cpendery <cpendery@vt.edu>
This commit is contained in:
cpendery
2023-09-16 18:02:07 -04:00
parent d82f77303f
commit d77b14cad9
6 changed files with 84 additions and 23 deletions
+6
View File
@@ -36,6 +36,12 @@ func runBindCmd(_ *cobra.Command, args []string) error {
switch userSelectedShell {
case "bash":
return shell.CreateBashBinding()
case "windows-powershell":
return shell.CreateWindowsPowershellBinding()
case "powershell":
return shell.CreatePowershellBinding()
case "zsh":
return shell.CreateZshBinding()
}
return nil
}
-4
View File
@@ -1,7 +1,3 @@
__clac_cmd() {
return "./clac"
}
__clac__() {
./clac "$READLINE_LINE"
output=$(./clac -o)
+9
View File
@@ -0,0 +1,9 @@
Set-PSReadLineKeyHandler -Chord "Ctrl+a" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::ClearKillRing()
[Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine()
[Microsoft.PowerShell.PSConsoleReadLine]::KillLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('.\clac.exe "')
[Microsoft.PowerShell.PSConsoleReadLine]::Yank()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('" ; .\clac.exe -o | iex')
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
+8
View File
@@ -0,0 +1,8 @@
clac-widget() {
./clac "${(qqq)LBUFFER}"
output=$(./clac -o)
eval "$output"
}
zle -N clac-widget
bindkey '^A' clac-widget
+58 -10
View File
@@ -5,12 +5,13 @@ import (
"fmt"
"os"
"path"
"runtime"
"github.com/adrg/xdg"
)
var (
SupportedShells = []string{"bash"}
SupportedShells = []string{"bash", "windows-powershell", "powershell"}
)
const (
@@ -21,30 +22,77 @@ const (
//go:embed key-bindings.bash
var bashBindings []byte
//go:embed key-bindings.ps1
var powershellBindings []byte
//go:embed key-bindings.zsh
var zshBindings []byte
func CreateBashBinding() error {
return createShBinding("key-bindings.bash", ".bashrc", bashBindings)
}
func CreateZshBinding() error {
return createShBinding("key-bindings.zsh", ".zshrc", zshBindings)
}
func createShBinding(keyBindingFilename, rcFilename string, bindingData []byte) error {
if err := os.MkdirAll(path.Join(xdg.Home, clacHomeFolder), 0770); err != nil {
return fmt.Errorf("unable to create .clac folder: %w", err)
}
f, err := os.Create(path.Join(xdg.Home, clacHomeFolder, "key-bindings.bash"))
f, err := os.Create(path.Join(xdg.Home, clacHomeFolder, keyBindingFilename))
if err != nil {
return fmt.Errorf("unable to create bash keybindings file: %w", err)
return fmt.Errorf("unable to create keybindings file: %w", err)
}
if _, err = f.Write(bashBindings); err != nil {
return fmt.Errorf("unable to write to bash keybindings file: %w", err)
return fmt.Errorf("unable to write to keybindings file: %w", err)
}
f, err = os.OpenFile(path.Join(xdg.Home, bashrcFile), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f, err = os.OpenFile(path.Join(xdg.Home, rcFilename), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("unable to append to bashrc: %w", err)
return fmt.Errorf("unable to append to rc file: %w", err)
}
bindingsPath := "~/.clac/key-bindings.bash"
content := fmt.Sprintf("\n[ -f %s ] && source %s", bindingsPath, bindingsPath)
if _, err := f.Write([]byte(content)); err != nil {
return fmt.Errorf("unable to append to bashrc: %w", err)
bindingsPath := fmt.Sprintf("~/.clac/%s", keyBindingFilename)
if _, err := fmt.Fprintf(f, "\n[ -f %s ] && source %s", bindingsPath, bindingsPath); err != nil {
return fmt.Errorf("unable to append to rc file: %w", err)
}
return err
}
func CreateWindowsPowershellBinding() error {
homeDir, _ := os.UserHomeDir()
profilePath := path.Join(homeDir, "Documents", "WindowsPowershell")
return createPShellBinding(profilePath, "Microsoft.PowerShell_profile.ps1")
}
func CreatePowershellBinding() error {
homeDir, _ := os.UserHomeDir()
profilePath := ""
switch runtime.GOOS {
case "windows":
profilePath = path.Join(homeDir, "Documents", "Powershell")
case "linux", "darwin":
profilePath = path.Join("~", ".config", "powershell")
}
return createPShellBinding(profilePath, "Microsoft.PowerShell_profile.ps1")
}
func createPShellBinding(profileLocation string, profileFile string) error {
if err := os.MkdirAll(profileLocation, 0600); err != nil {
return fmt.Errorf("unable to create powershell's profile directory: %w", err)
}
profilePath := path.Join(profileLocation, profileFile)
f, err := os.OpenFile(profilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("unable to open/create powershell's profile file: %w", err)
}
if _, err := f.Write(powershellBindings); err != nil {
return fmt.Errorf("unable to append keybindings to powershell's profile: %w", err)
}
return nil
}
+3 -9
View File
@@ -58,12 +58,6 @@ func New() Model {
LineDown: key.NewBinding(
key.WithKeys("down"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup"),
),
PageDown: key.NewBinding(
key.WithKeys("pgdown"),
),
}
return Model{
cursor: 0,
@@ -127,11 +121,11 @@ func (m Model) Update(msg tea.Msg, command string, userInputCursorLocation int)
if m.suggestionId == msg.Id {
m.suggestions, m.argDescription, m.runesToRemove = msg.Suggestions, msg.ArgumentDescription, msg.RunesToRemove
}
if m.cursor > len(m.suggestions)-1 {
m.cursor = 0
}
}
m.userInputCursorLocation = userInputCursorLocation
if m.cursor > len(m.suggestions)-1 {
m.cursor = 0
}
return m, tea.Batch(cmds...)
}