Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support --login & respect login shells in shell plugins #232

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions shell/shellIntegration.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
if [ -r ~/.bashrc ]; then
. ~/.bashrc
if [ -z "$ISTERM_LOGIN" ]; then
if [ -r ~/.bashrc ]; then
. ~/.bashrc
fi
else
if [ -r /etc/profile ]; then
. /etc/profile
fi
# execute the first that exists
if [ -r ~/.bash_profile ]; then
. ~/.bash_profile
elif [ -r ~/.bash_login ]; then
. ~/.bash_login
elif [ -r ~/.profile ]; then
. ~/.profile
fi
fi

if [ -r ~/.inshellisense/bash-preexec.sh ]; then
Expand Down
5 changes: 3 additions & 2 deletions src/commands/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ type RootCommandOptions = {
verbose: boolean | undefined;
check: boolean | undefined;
test: boolean | undefined;
login: boolean | undefined;
};

export const action = (program: Command) => async (options: RootCommandOptions) => {
const inISTerm = process.env.ISTERM === "1";
if (options.check || inISTerm) {
process.stdout.write(renderConfirmation(inISTerm));
return;
process.exit(0);
}

if (options.verbose) await log.enable();
Expand All @@ -45,5 +46,5 @@ export const action = (program: Command) => async (options: RootCommandOptions)
await setupBashPreExec();
}
await loadAliases(shell);
await render(shell, options.test ?? false);
await render(shell, options.test ?? false, options.login ?? false);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ program
.description("IDE style command line auto complete")
.version(await getVersion(), "-v, --version", "output the current version")
.action(action(program))
.option("-l, --login", `start shell as a login shell`)
.option("-s, --shell <shell>", `shell to use for command execution, supported shells: ${supportedShells}`)
.option("-c, --check", `check if shell is in an inshellisense session`)
.addOption(hiddenOption("-T, --test", "used to make e2e tests reproducible across machines"))
Expand Down
27 changes: 22 additions & 5 deletions src/isterm/pty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ISTermOptions = {
shell: Shell;
shellArgs?: string[];
underTest: boolean;
login: boolean;
};

export class ISTerm implements IPty {
Expand All @@ -46,13 +47,13 @@ export class ISTerm implements IPty {
readonly #commandManager: CommandManager;
readonly #shell: Shell;

constructor({ shell, cols, rows, env, shellTarget, shellArgs, underTest }: ISTermOptions & { shellTarget: string }) {
constructor({ shell, cols, rows, env, shellTarget, shellArgs, underTest, login }: ISTermOptions & { shellTarget: string }) {
this.#pty = pty.spawn(shellTarget, shellArgs ?? [], {
name: "xterm-256color",
cols,
rows,
cwd: process.cwd(),
env: { ...convertToPtyEnv(shell, underTest), ...env },
env: { ...convertToPtyEnv(shell, underTest, login), ...env },
});
this.pid = this.#pty.pid;
this.cols = this.#pty.cols;
Expand Down Expand Up @@ -271,11 +272,11 @@ export class ISTerm implements IPty {
}

export const spawn = async (options: ISTermOptions): Promise<ISTerm> => {
const { shellTarget, shellArgs } = await convertToPtyTarget(options.shell, options.underTest);
const { shellTarget, shellArgs } = await convertToPtyTarget(options.shell, options.underTest, options.login);
return new ISTerm({ ...options, shellTarget, shellArgs });
};

const convertToPtyTarget = async (shell: Shell, underTest: boolean) => {
const convertToPtyTarget = async (shell: Shell, underTest: boolean, login: boolean) => {
const platform = os.platform();
const shellTarget = shell == Shell.Bash && platform == "win32" ? await gitBashPath() : platform == "win32" ? `${shell}.exe` : shell;
const shellFolderPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "..", "..", "shell");
Expand Down Expand Up @@ -309,15 +310,31 @@ const convertToPtyTarget = async (shell: Shell, underTest: boolean) => {
break;
}

if (login) {
switch (shell) {
case Shell.Powershell:
case Shell.Pwsh:
shellArgs.unshift("-login");
break;
case Shell.Zsh:
case Shell.Fish:
case Shell.Xonsh:
case Shell.Nushell:
shellArgs.unshift("--login");
break;
}
}

return { shellTarget, shellArgs };
};

const convertToPtyEnv = (shell: Shell, underTest: boolean) => {
const convertToPtyEnv = (shell: Shell, underTest: boolean, login: boolean) => {
const env: Record<string, string> = {
...process.env,
ISTERM: "1",
};
if (underTest) env.ISTERM_TESTING = "1";
if (login) env.ISTERM_LOGIN = "1";

switch (shell) {
case Shell.Cmd: {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/isterm/pty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const windowsTest = os.platform() == "win32" ? test.skip : test.skip;
const unixTest = os.platform() == "darwin" || os.platform() == "linux" ? test.skip : test.skip;

const runTerm = async (shell: Shell, input: string[], env?: { [key: string]: string | undefined }) => {
const ptyProcess = await isterm.spawn({ shell, rows: process.stdout.rows, cols: process.stdout.columns, env, underTest: true });
const ptyProcess = await isterm.spawn({ shell, rows: process.stdout.rows, cols: process.stdout.columns, env, underTest: true, login: false });
await new Promise((r) => setTimeout(r, 1_000));
for (const data of input) {
ptyProcess.write(data);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/ui-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const renderConfirmation = (live: boolean): string => {
return `inshellisense session [${statusMessage}]\n`;
};

export const render = async (shell: Shell, underTest: boolean) => {
const term = await isterm.spawn({ shell, rows: process.stdout.rows, cols: process.stdout.columns, underTest });
export const render = async (shell: Shell, underTest: boolean, login: boolean) => {
const term = await isterm.spawn({ shell, rows: process.stdout.rows, cols: process.stdout.columns, underTest, login });
const suggestionManager = new SuggestionManager(term, shell);
let hasActiveSuggestions = false;
let previousSuggestionsRows = 0;
Expand Down
26 changes: 22 additions & 4 deletions src/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,20 @@ export const getShellPromptRewrites = (shell: Shell) => shell == Shell.Nushell;
export const getShellConfig = (shell: Shell): string => {
switch (shell) {
case Shell.Zsh:
return `if [[ -z "\${ISTERM}" && $- = *i* && $- != *c* ]]; then
if [[ -o login ]]; then
is -s zsh --login ; exit
else
is -s zsh ; exit
fi
fi`;
case Shell.Bash:
return `if [[ -z "\${ISTERM}" && $- = *i* && $- != *c* ]]; then
is -s ${shell} ; exit
if [ shopt -q login_shell ]; then
is -s bash --login ; exit
else
is -s bash ; exit
fi
fi`;
case Shell.Powershell:
case Shell.Pwsh:
Expand All @@ -140,14 +151,21 @@ if ([string]::IsNullOrEmpty($env:ISTERM) -and [Environment]::UserInteractive -an
}`;
case Shell.Fish:
return `if test -z "$ISTERM" && status --is-interactive
is -s fish ; kill %self
if status --is-login
is -s fish --login ; kill %self
else
is -s fish ; kill %self
end
end`;
case Shell.Xonsh:
return `if 'ISTERM' not in \${...} and $XONSH_INTERACTIVE:
is -s xonsh ; exit`;
if $XONSH_LOGIN:
is -s xonsh --login ; exit
else:
is -s xonsh ; exit`;
case Shell.Nushell:
return `if "ISTERM" not-in $env and $nu.is-interactive {
is -s nu ; exit
if $nu.is-login { is -s nu --login ; exit } else { is -s nu ; exit }
}`;
}
return "";
Expand Down
Loading