From b689145f407f7463f41d5b54957a2257260353db Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Thu, 14 Dec 2023 13:19:41 -0800 Subject: [PATCH] feat: ability to check if the terminal in an inshellisense session (#122) Signed-off-by: Chapman Pendery --- src/commands/root.ts | 9 ++++++++- src/index.ts | 1 + src/isterm/pty.ts | 10 +++++++--- src/ui/ui-root.ts | 9 ++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/commands/root.ts b/src/commands/root.ts index 41c4e01..ab622c8 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import { render } from "../ui/ui-root.js"; +import { render, renderConfirmation } from "../ui/ui-root.js"; import { Shell, supportedShells as shells, setupZshDotfiles } from "../utils/shell.js"; import { inferShell } from "../utils/shell.js"; import { loadConfig } from "../utils/config.js"; @@ -13,9 +13,16 @@ export const supportedShells = shells.join(", "); type RootCommandOptions = { shell: Shell | undefined; verbose: boolean | undefined; + check: 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; + } + if (options.verbose) await log.enable(); await loadConfig(program); diff --git a/src/index.ts b/src/index.ts index e8ee11a..922d6f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ program .version(await getVersion(), "-v, --version", "output the current version") .action(action(program)) .option("-s, --shell ", `shell to use for command execution, supported shells: ${supportedShells}`) + .option("-c, --check", `check if shell is in an inshellisense session`) .option("-V, --verbose", `enable verbose logging`) .showHelpAfterError("(add --help for additional information)"); diff --git a/src/isterm/pty.ts b/src/isterm/pty.ts index a81a748..1ec10d0 100644 --- a/src/isterm/pty.ts +++ b/src/isterm/pty.ts @@ -236,14 +236,18 @@ const convertToPtyTarget = async (shell: Shell) => { }; const convertToPtyEnv = (shell: Shell) => { + const env = { + ...process.env, + ISTERM: "1", + }; switch (shell) { case Shell.Cmd: { const prompt = process.env.PROMPT ? process.env.PROMPT : "$P$G"; - return { ...process.env, PROMPT: `${IstermPromptStart}${prompt}${IstermPromptEnd}` }; + return { ...env, PROMPT: `${IstermPromptStart}${prompt}${IstermPromptEnd}` }; } case Shell.Zsh: { - return { ...process.env, ZDOTDIR: zdotdir, USER_ZDOTDIR: userZdotdir }; + return { ...env, ZDOTDIR: zdotdir, USER_ZDOTDIR: userZdotdir }; } } - return process.env; + return env; }; diff --git a/src/ui/ui-root.ts b/src/ui/ui-root.ts index 03ab2b7..cc78b20 100644 --- a/src/ui/ui-root.ts +++ b/src/ui/ui-root.ts @@ -1,14 +1,21 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +import ansi from "ansi-escapes"; +import chalk from "chalk"; + import { inputModifier } from "./input.js"; import log from "../utils/log.js"; import { Shell } from "../utils/shell.js"; import isterm from "../isterm/index.js"; import { eraseLinesBelow } from "../utils/ansi.js"; -import ansi from "ansi-escapes"; import { SuggestionManager, MAX_LINES } from "./suggestionManager.js"; +export const renderConfirmation = (live: boolean): string => { + const statusMessage = live ? chalk.green("live") : chalk.red("not found"); + return `inshellisense session [${statusMessage}]\n`; +}; + export const render = async (shell: Shell) => { const term = await isterm.spawn({ shell, rows: process.stdout.rows, cols: process.stdout.columns }); const suggestionManager = new SuggestionManager(term);