diff --git a/cmd/bind.go b/cmd/bind.go index 1bf8157..a743427 100644 --- a/cmd/bind.go +++ b/cmd/bind.go @@ -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 } diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 00a1275..6a0108a 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -1,7 +1,3 @@ -__clac_cmd() { - return "./clac" -} - __clac__() { ./clac "$READLINE_LINE" output=$(./clac -o) diff --git a/shell/key-bindings.ps1 b/shell/key-bindings.ps1 new file mode 100644 index 0000000..30af8f0 --- /dev/null +++ b/shell/key-bindings.ps1 @@ -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() +} \ No newline at end of file diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh new file mode 100644 index 0000000..387fd25 --- /dev/null +++ b/shell/key-bindings.zsh @@ -0,0 +1,8 @@ +clac-widget() { + ./clac "${(qqq)LBUFFER}" + output=$(./clac -o) + eval "$output" +} + +zle -N clac-widget +bindkey '^A' clac-widget \ No newline at end of file diff --git a/shell/shell.go b/shell/shell.go index 0e8901b..ff1bef8 100644 --- a/shell/shell.go +++ b/shell/shell.go @@ -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 +} diff --git a/ui/suggestions/suggestions.go b/ui/suggestions/suggestions.go index bc0e615..c813b57 100644 --- a/ui/suggestions/suggestions.go +++ b/ui/suggestions/suggestions.go @@ -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...) }