diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh new file mode 100644 index 0000000..25fcc78 --- /dev/null +++ b/shell/key-bindings.zsh @@ -0,0 +1,10 @@ +__inshellisense__() { + input=$LBUFFER + LBUFFER= + inshellisense -c "$input" -s zsh < $TTY + print -s $(inshellisense --history) + zle reset-prompt +} + +zle -N __inshellisense __inshellisense__ +bindkey '^A' __inshellisense \ No newline at end of file diff --git a/src/commands/root.ts b/src/commands/root.ts index e623cc7..44f2500 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -4,8 +4,8 @@ import { render } from "../ui/ui-root.js"; import { executeShellCommandTTY } from "../runtime/utils.js"; import { saveCommand, loadCommand } from "../utils/cache.js"; +import { supportedShells as shells } from "../utils/bindings.js"; -const shells = ["bash", "powershell", "pwsh"]; export const supportedShells = shells.join(", "); type RootCommandOptions = { @@ -21,7 +21,7 @@ export const action = async (options: RootCommandOptions) => { } const shell = options.shell ?? ""; - if (!shells.includes(shell)) { + if (!shells.map((s) => s.valueOf()).includes(shell)) { console.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`); process.exit(1); } diff --git a/src/utils/bindings.ts b/src/utils/bindings.ts index 47b9826..91d8301 100644 --- a/src/utils/bindings.ts +++ b/src/utils/bindings.ts @@ -16,14 +16,19 @@ export enum Shell { Bash = "bash", Powershell = "powershell", Pwsh = "pwsh", + Zsh = "zsh", } -export const supportedShells = [Shell.Bash, Shell.Powershell, Shell.Pwsh]; +export const supportedShells = [Shell.Bash, Shell.Powershell, Shell.Pwsh, Shell.Zsh]; const bashScriptCommand = (): string => { return `[ -f ~/${cacheFolder}/key-bindings.bash ] && source ~/${cacheFolder}/key-bindings.bash`; }; +const zshScriptCommand = (): string => { + return `[ -f ~/${cacheFolder}/key-bindings.zsh ] && source ~/${cacheFolder}/key-bindings.zsh`; +}; + const powershellScriptCommand = (): string => { const bindingsPath = path.join(os.homedir(), cacheFolder, "key-bindings-powershell.ps1"); return `if(Test-Path '${bindingsPath}' -PathType Leaf){. ${bindingsPath}}`; @@ -63,6 +68,16 @@ export const availableBindings = async (): Promise => { } } + const zshConfigPath = path.join(os.homedir(), ".zshrc"); + if (!fs.existsSync(zshConfigPath)) { + bindings.push(Shell.Zsh); + } else { + const zshConfigContent = fsAsync.readFile(zshConfigPath, { encoding: "utf-8" }); + if (!(await zshConfigContent).includes(zshScriptCommand())) { + bindings.push(Shell.Zsh); + } + } + const powershellConfigPath = path.join(os.homedir(), "Documents", "WindowsPowershell", "Microsoft.PowerShell_profile.ps1"); if (!fs.existsSync(powershellConfigPath)) { bindings.push(Shell.Powershell); @@ -97,6 +112,12 @@ export const bind = async (shell: Shell): Promise => { await fsAsync.copyFile(path.join(__dirname, "..", "..", "shell", "key-bindings.bash"), path.join(os.homedir(), cacheFolder, "key-bindings.bash")); break; } + case Shell.Zsh: { + const zshConfigPath = path.join(os.homedir(), ".zshrc"); + await fsAsync.appendFile(zshConfigPath, `\n${zshScriptCommand()}`); + await fsAsync.copyFile(path.join(__dirname, "..", "..", "shell", "key-bindings.zsh"), path.join(os.homedir(), cacheFolder, "key-bindings.zsh")); + break; + } case Shell.Powershell: { const powershellConfigPath = path.join(os.homedir(), "Documents", "WindowsPowershell", "Microsoft.PowerShell_profile.ps1"); await fsAsync.appendFile(powershellConfigPath, `\n${powershellScriptCommand()}`);