@microsoft/inshellisense 0.0.1-rc.2 → 0.0.1-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +80 -6
  3. package/SECURITY.md +41 -41
  4. package/build/commands/complete.js +16 -0
  5. package/build/commands/doctor.js +11 -0
  6. package/build/commands/init.js +24 -0
  7. package/build/commands/root.js +27 -30
  8. package/build/commands/specs/list.js +26 -0
  9. package/build/commands/specs/root.js +8 -0
  10. package/build/commands/uninstall.js +1 -1
  11. package/build/index.js +20 -7
  12. package/build/isterm/commandManager.js +184 -0
  13. package/build/isterm/index.js +4 -0
  14. package/build/isterm/pty.js +361 -0
  15. package/build/runtime/alias.js +66 -0
  16. package/build/runtime/generator.js +24 -11
  17. package/build/runtime/parser.js +86 -16
  18. package/build/runtime/runtime.js +103 -45
  19. package/build/runtime/spec.js +36 -0
  20. package/build/runtime/suggestion.js +70 -22
  21. package/build/runtime/template.js +33 -18
  22. package/build/runtime/utils.js +111 -12
  23. package/build/ui/suggestionManager.js +162 -0
  24. package/build/ui/ui-doctor.js +69 -0
  25. package/build/ui/ui-root.js +134 -64
  26. package/build/ui/ui-uninstall.js +3 -5
  27. package/build/ui/utils.js +57 -0
  28. package/build/utils/ansi.js +37 -0
  29. package/build/utils/config.js +99 -0
  30. package/build/utils/log.js +39 -0
  31. package/build/utils/shell.js +318 -0
  32. package/package.json +39 -6
  33. package/scripts/postinstall.js +9 -0
  34. package/shell/bash-preexec.sh +380 -0
  35. package/shell/shellIntegration-env.zsh +12 -0
  36. package/shell/shellIntegration-login.zsh +9 -0
  37. package/shell/shellIntegration-profile.zsh +9 -0
  38. package/shell/shellIntegration-rc.zsh +66 -0
  39. package/shell/shellIntegration.bash +114 -0
  40. package/shell/shellIntegration.fish +27 -0
  41. package/shell/shellIntegration.nu +29 -0
  42. package/shell/shellIntegration.ps1 +26 -0
  43. package/shell/shellIntegration.xsh +31 -0
  44. package/todo.md +17 -0
  45. package/build/commands/bind.js +0 -12
  46. package/build/ui/input.js +0 -55
  47. package/build/ui/suggestions.js +0 -84
  48. package/build/ui/ui-bind.js +0 -69
  49. package/build/utils/bindings.js +0 -216
  50. package/build/utils/cache.js +0 -21
  51. package/shell/key-bindings-powershell.ps1 +0 -27
  52. package/shell/key-bindings-pwsh.ps1 +0 -27
  53. package/shell/key-bindings.bash +0 -7
  54. package/shell/key-bindings.fish +0 -8
  55. package/shell/key-bindings.zsh +0 -10
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) Microsoft Corporation.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # inshellisense
2
2
 
3
- `inshellisense` provides IDE style autocomplete for shells. It's a terminal native runtime for [autocomplete](https://github.com/withfig/autocomplete) which has support for 600+ command line tools. `inshellisense` supports Windows, Linux, & MacOS.
3
+ `inshellisense` provides IDE style autocomplete for shells. It's a terminal native runtime for [autocomplete](https://github.com/withfig/autocomplete) which has support for 600+ command line tools. `inshellisense` supports Windows, Linux, & macOS.
4
4
 
5
- <p align="center"><img alt="demo of inshellisense working" src="/docs/demo.gif" height="450px"/></p>
5
+ <p align="center"><img alt="demo of inshellisense working" src="/docs/demo.gif"/></p>
6
6
 
7
7
  ## Getting Started
8
8
 
9
9
  ### Requirements
10
- - `node >= 16.x`
10
+
11
+ - Node.js 22.X, 20.X, 18.X
11
12
 
12
13
  ### Installation
13
14
 
@@ -17,13 +18,56 @@ npm install -g @microsoft/inshellisense
17
18
 
18
19
  ### Quickstart
19
20
 
20
- After completing the installation, you can already run `inshellisense --shell <shell>` to start the autocomplete session for your desired shell. Additionally, you can bind `inshellisense` to a keybinding of `CTRL+a` by running the below command. This brings the added advantages of automatically starting the autocomplete session with your current shell and injecting any accepted command into your shell's history.
21
+ After completing the installation, run `is doctor` to verify your installation was successful. You can run `is` to start the autocomplete session for your desired shell. Additionally, inshellisense is also aliased under `inshellisense` after installation.
22
+
23
+ ### Shell Plugin
24
+
25
+ If you'd like to automatically start inshellisense when you open your shell, run the respective command for your shell. After running the command, inshellisense will automatically open when you start any new shell session:
21
26
 
22
27
  ```shell
23
- inshellisense bind
28
+ # bash
29
+ is init bash >> ~/.bashrc
30
+
31
+ # zsh
32
+ is init zsh >> ~/.zshrc
33
+
34
+ # fish
35
+ is init fish >> ~/.config/fish/config.fish
36
+
37
+ # pwsh
38
+ is init pwsh | Add-Content $profile
39
+
40
+ # powershell
41
+ is init powershell | Add-Content $profile
42
+
43
+ # xonsh
44
+ is init xonsh >> ~/.xonshrc
45
+
46
+ # nushell
47
+ is init nu | save $nu.env-path --append
24
48
  ```
25
49
 
26
- Additionally, inshellisense is also aliased under `is` after install for convenience.
50
+ > [!NOTE]
51
+ > When updating your shell configuration in the future, make sure the inshellisense plugin is the last command in the file. Including commands after it may break the configuration (ex. initializing your shell plugin manager after the inshellisense plugin)
52
+
53
+ ### Usage
54
+
55
+ | Action | Command | Description |
56
+ | ------------------------------------- | ------- | ------------------------------------------------ |
57
+ | Start | `is` | Start inshellisense session on the current shell |
58
+ | Stop | `exit` | Stop inshellisense session on the current shell |
59
+ | Check If Inside Inshellisense Session | `is -c` | Check if shell inside inshellisense session |
60
+
61
+ #### Keybindings
62
+
63
+ All other keys are passed through to the shell. The keybindings below are only captured when the inshellisense suggestions are visible, otherwise they are passed through to the shell as well. These can be customized in the [config](#configuration).
64
+
65
+ | Action | Keybinding |
66
+ | ------------------------- | -------------- |
67
+ | Accept Current Suggestion | <kbd>tab</kbd> |
68
+ | View Next Suggestion | <kbd>↓</kbd> |
69
+ | View Previous Suggestion | <kbd>↑</kbd> |
70
+ | Dismiss Suggestions | <kbd>esc</kbd> |
27
71
 
28
72
  ## Integrations
29
73
 
@@ -34,6 +78,36 @@ inshellisense supports the following shells:
34
78
  - [fish](https://github.com/fish-shell/fish-shell)
35
79
  - [pwsh](https://github.com/PowerShell/PowerShell)
36
80
  - [powershell](https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-windows-powershell) (Windows Powershell)
81
+ - [cmd](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd) _(experimental)_
82
+ - [xonsh](https://xon.sh/)
83
+ - [nushell](https://www.nushell.sh/)
84
+
85
+ ## Configuration
86
+
87
+ All configuration is done through a [toml](https://toml.io/) file. You can create this file at `~/.inshellisenserc` or, for XDG compliance, at `~/.config/inshellisense/rc.toml`. The [JSON schema](https://json-schema.org/) for the configuration file can be found [here](https://github.com/microsoft/inshellisense/blob/main/src/utils/config.ts).
88
+
89
+ ### Keybindings
90
+
91
+ You can customize the keybindings for inshellisense by adding a `bindings` section to your config file. The following is the default configuration for the [keybindings](#keybindings):
92
+
93
+ ```toml
94
+ [bindings.acceptSuggestion]
95
+ key = "tab"
96
+ # shift and ctrl are optional and default to false
97
+ shift = false
98
+ ctrl = false
99
+
100
+ [bindings.nextSuggestion]
101
+ key = "down"
102
+
103
+ [bindings.previousSuggestion]
104
+ key = "up"
105
+
106
+ [bindings.dismissSuggestions]
107
+ key = "escape"
108
+ ```
109
+
110
+ Key names are matched against the Node.js [keypress](https://nodejs.org/api/readline.html#readlineemitkeypresseventsstream-interface) events.
37
111
 
38
112
  ## Contributing
39
113
 
package/SECURITY.md CHANGED
@@ -1,41 +1,41 @@
1
- <!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
2
-
3
- ## Security
4
-
5
- Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
6
-
7
- If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
8
-
9
- ## Reporting Security Issues
10
-
11
- **Please do not report security vulnerabilities through public GitHub issues.**
12
-
13
- Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
14
-
15
- If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
16
-
17
- You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
18
-
19
- Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20
-
21
- * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22
- * Full paths of source file(s) related to the manifestation of the issue
23
- * The location of the affected source code (tag/branch/commit or direct URL)
24
- * Any special configuration required to reproduce the issue
25
- * Step-by-step instructions to reproduce the issue
26
- * Proof-of-concept or exploit code (if possible)
27
- * Impact of the issue, including how an attacker might exploit the issue
28
-
29
- This information will help us triage your report more quickly.
30
-
31
- If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
32
-
33
- ## Preferred Languages
34
-
35
- We prefer all communications to be in English.
36
-
37
- ## Policy
38
-
39
- Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
40
-
41
- <!-- END MICROSOFT SECURITY.MD BLOCK -->
1
+ <!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
2
+
3
+ ## Security
4
+
5
+ Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
6
+
7
+ If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
8
+
9
+ ## Reporting Security Issues
10
+
11
+ **Please do not report security vulnerabilities through public GitHub issues.**
12
+
13
+ Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
14
+
15
+ If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
16
+
17
+ You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
18
+
19
+ Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20
+
21
+ * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22
+ * Full paths of source file(s) related to the manifestation of the issue
23
+ * The location of the affected source code (tag/branch/commit or direct URL)
24
+ * Any special configuration required to reproduce the issue
25
+ * Step-by-step instructions to reproduce the issue
26
+ * Proof-of-concept or exploit code (if possible)
27
+ * Impact of the issue, including how an attacker might exploit the issue
28
+
29
+ This information will help us triage your report more quickly.
30
+
31
+ If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
32
+
33
+ ## Preferred Languages
34
+
35
+ We prefer all communications to be in English.
36
+
37
+ ## Policy
38
+
39
+ Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
40
+
41
+ <!-- END MICROSOFT SECURITY.MD BLOCK -->
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import os from "node:os";
4
+ import { Command } from "commander";
5
+ import { getSuggestions } from "../runtime/runtime.js";
6
+ import { Shell } from "../utils/shell.js";
7
+ const action = async (input) => {
8
+ const suggestions = await getSuggestions(input, process.cwd(), os.platform() === "win32" ? Shell.Cmd : Shell.Bash);
9
+ process.stdout.write(JSON.stringify(suggestions));
10
+ process.exit(0);
11
+ };
12
+ const cmd = new Command("complete");
13
+ cmd.description(`generates a completion for the provided input`);
14
+ cmd.argument("<input>");
15
+ cmd.action(action);
16
+ export default cmd;
@@ -0,0 +1,11 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { Command } from "commander";
4
+ import { render } from "../ui/ui-doctor.js";
5
+ const action = async () => {
6
+ await render();
7
+ };
8
+ const cmd = new Command("doctor");
9
+ cmd.description(`checks the health of this inshellisense installation`);
10
+ cmd.action(action);
11
+ export default cmd;
@@ -0,0 +1,24 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { Command } from "commander";
4
+ import { createShellConfigs, initSupportedShells as shells, getShellSourceCommand } from "../utils/shell.js";
5
+ const supportedShells = shells.join(", ");
6
+ const action = (program) => async (shell, options) => {
7
+ if (options.generateFullConfigs) {
8
+ await createShellConfigs();
9
+ return;
10
+ }
11
+ if (shell == null)
12
+ program.error(`Shell is required, supported shells: ${supportedShells}`, { exitCode: 1 });
13
+ if (!shells.map((s) => s.valueOf()).includes(shell)) {
14
+ program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 });
15
+ }
16
+ const config = getShellSourceCommand(shell);
17
+ process.stdout.write(`\n\n${config}\n`);
18
+ };
19
+ const cmd = new Command("init");
20
+ cmd.description(`generates shell configurations and prints the source command for the specified shell`);
21
+ cmd.argument("[shell]", `shell to generate for, supported shells: ${supportedShells}`);
22
+ cmd.option("--generate-full-configs");
23
+ cmd.action(action(cmd));
24
+ export default cmd;
@@ -1,40 +1,37 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
- import { render } from "../ui/ui-root.js";
4
- import { executeShellCommandTTY } from "../runtime/utils.js";
5
- import { saveCommand, loadCommand } from "../utils/cache.js";
6
- import { supportedShells as shells } from "../utils/bindings.js";
3
+ import { render, renderConfirmation } from "../ui/ui-root.js";
4
+ import { Shell, supportedShells as shells, setupZshDotfiles, setupBashPreExec } from "../utils/shell.js";
5
+ import { inferShell } from "../utils/shell.js";
6
+ import { loadConfig } from "../utils/config.js";
7
+ import log from "../utils/log.js";
8
+ import { loadAliases } from "../runtime/alias.js";
9
+ import { loadLocalSpecsSet } from "../runtime/runtime.js";
7
10
  export const supportedShells = shells.join(", ");
8
- export const action = async (options) => {
9
- if (options.history) {
10
- process.stdout.write(await loadCommand());
11
+ export const action = (program) => async (options) => {
12
+ const inISTerm = process.env.ISTERM === "1";
13
+ if (options.check || inISTerm) {
14
+ process.stdout.write(renderConfirmation(inISTerm));
11
15
  process.exit(0);
12
16
  }
13
- const shell = options.shell ?? "";
14
- if (!shells.map((s) => s.valueOf()).includes(shell)) {
15
- console.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`);
16
- process.exit(1);
17
+ if (options.verbose)
18
+ await log.enable();
19
+ await loadConfig(program);
20
+ await loadLocalSpecsSet();
21
+ log.overrideConsole();
22
+ const shell = options.shell ?? (await inferShell());
23
+ if (shell == null) {
24
+ program.error(`Unable to identify shell, use the -s/--shell option to provide your shell`, { exitCode: 1 });
17
25
  }
18
- let executed = false;
19
- const commands = [];
20
- let result = { code: 0 };
21
- let startingCommand = options.command;
22
- while (options.duration === "session" || !executed) {
23
- const commandToExecute = await render(startingCommand);
24
- if (commandToExecute == null || commandToExecute.trim().toLowerCase() == "exit" || commandToExecute.trim().toLowerCase() == "logout") {
25
- result = { code: 0 };
26
- break;
27
- }
28
- commands.push(commandToExecute);
29
- result = await executeShellCommandTTY(shell, commandToExecute);
30
- executed = true;
31
- startingCommand = undefined;
26
+ if (!shells.map((s) => s.valueOf()).includes(shell)) {
27
+ program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 });
32
28
  }
33
- await saveCommand(commands);
34
- if (result.code) {
35
- process.exit(result.code);
29
+ if (shell == Shell.Zsh) {
30
+ await setupZshDotfiles();
36
31
  }
37
- else {
38
- process.exit(0);
32
+ else if (shell == Shell.Bash) {
33
+ await setupBashPreExec();
39
34
  }
35
+ await loadAliases(shell);
36
+ await render(program, shell, options.test ?? false, options.login ?? false);
40
37
  };
@@ -0,0 +1,26 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { Command } from "commander";
4
+ import { loadConfig } from "../../utils/config.js";
5
+ import { getSpecNames, loadLocalSpecsSet } from "../../runtime/runtime.js";
6
+ import { getAliasNames, loadAliases } from "../../runtime/alias.js";
7
+ import { aliasSupportedShells } from "../../utils/shell.js";
8
+ const supportedShells = aliasSupportedShells.join(", ");
9
+ const action = (program) => async (options) => {
10
+ await loadConfig(program);
11
+ await loadLocalSpecsSet();
12
+ const { shell } = options;
13
+ if (shell && !aliasSupportedShells.map((s) => s.valueOf()).includes(shell)) {
14
+ program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 });
15
+ }
16
+ if (shell) {
17
+ await loadAliases(shell);
18
+ }
19
+ process.stdout.write(JSON.stringify([...getAliasNames(), ...getSpecNames()]));
20
+ process.exit(0);
21
+ };
22
+ const cmd = new Command("list");
23
+ cmd.description(`list the names of all available specs`);
24
+ cmd.option("-s, --shell <shell>", `shell to use alias specs, supported shells: ${supportedShells}`);
25
+ cmd.action(action(cmd));
26
+ export default cmd;
@@ -0,0 +1,8 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { Command } from "commander";
4
+ import list from "./list.js";
5
+ const cmd = new Command("specs");
6
+ cmd.description(`manage specs`);
7
+ cmd.addCommand(list);
8
+ export default cmd;
@@ -6,6 +6,6 @@ const action = async () => {
6
6
  await render();
7
7
  };
8
8
  const cmd = new Command("uninstall");
9
- cmd.description(`removes all bindings and configuration for inshellisense`);
9
+ cmd.description(`removes all configuration for inshellisense`);
10
10
  cmd.action(action);
11
11
  export default cmd;
package/build/index.js CHANGED
@@ -2,21 +2,34 @@
2
2
  // Copyright (c) Microsoft Corporation.
3
3
  // Licensed under the MIT License.
4
4
  /* eslint-disable header/header */
5
- import { Command } from "commander";
6
- import bind from "./commands/bind.js";
5
+ import { Command, Option } from "commander";
6
+ import complete from "./commands/complete.js";
7
7
  import uninstall from "./commands/uninstall.js";
8
+ import init from "./commands/init.js";
9
+ import specs from "./commands/specs/root.js";
10
+ import doctor from "./commands/doctor.js";
8
11
  import { action, supportedShells } from "./commands/root.js";
9
12
  import { getVersion } from "./utils/version.js";
10
13
  const program = new Command();
14
+ const hiddenOption = (flags, description) => {
15
+ const option = new Option(flags, description);
16
+ option.hidden = true;
17
+ return option;
18
+ };
11
19
  program
12
20
  .name("inshellisense")
13
21
  .description("IDE style command line auto complete")
14
22
  .version(await getVersion(), "-v, --version", "output the current version")
23
+ .action(action(program))
24
+ .option("-l, --login", `start shell as a login shell`)
15
25
  .option("-s, --shell <shell>", `shell to use for command execution, supported shells: ${supportedShells}`)
16
- .option("-c, --command <commmand>", "command to use as initial input")
17
- .option("--history", "get the last command execute")
18
- .option("-d, --duration <duration>", "duration of the autocomplete session, supported durations: single, session", "session")
19
- .action(action);
20
- program.addCommand(bind);
26
+ .option("-c, --check", `check if shell is in an inshellisense session`)
27
+ .addOption(hiddenOption("-T, --test", "used to make e2e tests reproducible across machines"))
28
+ .option("-V, --verbose", `enable verbose logging`)
29
+ .passThroughOptions();
30
+ program.addCommand(complete);
21
31
  program.addCommand(uninstall);
32
+ program.addCommand(init);
33
+ program.addCommand(specs);
34
+ program.addCommand(doctor);
22
35
  program.parse();
@@ -0,0 +1,184 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import convert from "color-convert";
4
+ import { getShellPromptRewrites, Shell } from "../utils/shell.js";
5
+ import log from "../utils/log.js";
6
+ export class CommandManager {
7
+ #activeCommand;
8
+ #terminal;
9
+ #acceptedCommandLines;
10
+ #maxCursorY;
11
+ #shell;
12
+ #promptRewrites;
13
+ constructor(terminal, shell) {
14
+ this.#terminal = terminal;
15
+ this.#shell = shell;
16
+ this.#activeCommand = {};
17
+ this.#maxCursorY = 0;
18
+ this.#acceptedCommandLines = new Set();
19
+ this.#promptRewrites = getShellPromptRewrites(shell);
20
+ this.#terminal.parser.registerCsiHandler({ final: "J" }, (params) => {
21
+ if (params.at(0) == 3 || params.at(0) == 2) {
22
+ this.handleClear();
23
+ }
24
+ return false;
25
+ });
26
+ }
27
+ handlePromptStart() {
28
+ this.#activeCommand = { promptStartMarker: this.#terminal.registerMarker(0), hasOutput: false, cursorTerminated: false };
29
+ }
30
+ handlePromptEnd() {
31
+ if (this.#activeCommand.promptEndMarker != null)
32
+ return;
33
+ if (this.#hasBeenAccepted()) {
34
+ this.#activeCommand = {};
35
+ return;
36
+ }
37
+ this.#activeCommand.promptEndMarker = this.#terminal.registerMarker(0);
38
+ if (this.#activeCommand.promptEndMarker?.line === this.#terminal.buffer.active.cursorY) {
39
+ this.#activeCommand.promptEndX = this.#terminal.buffer.active.cursorX;
40
+ }
41
+ this.#activeCommand.promptText = this.#terminal.buffer.active.getLine(this.#activeCommand.promptEndMarker?.line ?? 0)?.translateToString(true);
42
+ }
43
+ #hasBeenAccepted() {
44
+ const commandLine = this.#activeCommand.promptStartMarker?.line ?? -1;
45
+ const hasBeenAccepted = this.#acceptedCommandLines.has(commandLine) && commandLine != -1;
46
+ return this.#promptRewrites && hasBeenAccepted; // this is a prompt + command that was accepted and is now being re-written by the shell for display purposes (e.g. nu)
47
+ }
48
+ handleClear() {
49
+ this.handlePromptStart();
50
+ this.#maxCursorY = 0;
51
+ this.#acceptedCommandLines.clear();
52
+ }
53
+ _getFgPaletteColor(cell) {
54
+ if (cell?.isFgDefault())
55
+ return 0;
56
+ if (cell?.isFgPalette())
57
+ return cell.getFgColor();
58
+ if (cell?.isFgRGB())
59
+ return convert.hex.ansi256(cell.getFgColor().toString(16));
60
+ }
61
+ _isSuggestion(cell) {
62
+ const color = this._getFgPaletteColor(cell);
63
+ const dim = (cell?.isDim() ?? 0) > 0;
64
+ const italic = (cell?.isItalic() ?? 0) > 0;
65
+ const dullColor = color == 8 || color == 7 || (color ?? 0) > 235 || (color == 15 && dim);
66
+ const dimItalic = dim || italic;
67
+ if (this.#shell == Shell.Pwsh || this.#shell == Shell.Powershell) {
68
+ return dimItalic;
69
+ }
70
+ return dullColor;
71
+ }
72
+ getState() {
73
+ return {
74
+ promptText: this.#activeCommand.promptText,
75
+ commandText: this.#activeCommand.commandText,
76
+ suggestionsText: this.#activeCommand.suggestionsText,
77
+ hasOutput: this.#activeCommand.hasOutput,
78
+ cursorTerminated: this.#activeCommand.cursorTerminated,
79
+ };
80
+ }
81
+ clearActiveCommand() {
82
+ this.#acceptedCommandLines.add(this.#activeCommand.promptEndMarker?.line ?? -1);
83
+ this.#activeCommand = {};
84
+ }
85
+ _getCommandLines() {
86
+ const lines = [];
87
+ let lineY = this.#activeCommand.promptEndMarker.line;
88
+ let line = this.#terminal.buffer.active.getLine(this.#activeCommand.promptEndMarker.line);
89
+ const absoluteY = this.#terminal.buffer.active.baseY + this.#terminal.buffer.active.cursorY;
90
+ for (; lineY < this.#terminal.buffer.active.baseY + this.#terminal.rows;) {
91
+ if (line)
92
+ lines.push(line);
93
+ lineY += 1;
94
+ line = this.#terminal.buffer.active.getLine(lineY);
95
+ const lineWrapped = line?.isWrapped;
96
+ const cursorWrapped = absoluteY > lineY - 1;
97
+ const wrapped = lineWrapped || cursorWrapped;
98
+ if (!wrapped)
99
+ break;
100
+ }
101
+ return lines;
102
+ }
103
+ _getCommandText(commandLines) {
104
+ const absoluteY = this.#terminal.buffer.active.baseY + this.#terminal.buffer.active.cursorY;
105
+ const cursorLine = Math.max(absoluteY - this.#activeCommand.promptEndMarker.line, 0);
106
+ let preCursorCommand = "";
107
+ let postCursorCommand = "";
108
+ let suggestion = "";
109
+ for (const [y, line] of commandLines.entries()) {
110
+ const startX = y == 0 ? this.#activeCommand.promptText?.length ?? 0 : 0;
111
+ for (let x = startX; x < this.#terminal.cols; x++) {
112
+ if (postCursorCommand.endsWith(" "))
113
+ break; // assume that a command that ends with 4 spaces is terminated, avoids capturing right prompts
114
+ const cell = line.getCell(x);
115
+ if (cell == null)
116
+ continue;
117
+ const chars = cell.getChars() == "" ? " " : cell.getChars();
118
+ const beforeCursor = y < cursorLine || (y == cursorLine && x < this.#terminal.buffer.active.cursorX);
119
+ const isCommand = !this._isSuggestion(cell) && suggestion.length == 0;
120
+ if (isCommand && beforeCursor) {
121
+ preCursorCommand += chars;
122
+ }
123
+ else if (isCommand) {
124
+ postCursorCommand += chars;
125
+ }
126
+ else {
127
+ suggestion += chars;
128
+ }
129
+ }
130
+ }
131
+ log.debug({ msg: "command text", preCursorCommand, postCursorCommand, suggestion });
132
+ return { suggestion, preCursorCommand, postCursorCommand };
133
+ }
134
+ _getCommandOutputStatus(commandLines) {
135
+ const outputLineY = this.#activeCommand.promptEndMarker.line + commandLines;
136
+ const maxLineY = this.#terminal.buffer.active.baseY + this.#terminal.rows;
137
+ if (outputLineY >= maxLineY)
138
+ return false;
139
+ const line = this.#terminal.buffer.active.getLine(outputLineY);
140
+ let cell = undefined;
141
+ for (let i = 0; i < this.#terminal.cols; i++) {
142
+ cell = line?.getCell(i, cell);
143
+ if (cell?.getChars() != "") {
144
+ return true;
145
+ }
146
+ }
147
+ return false;
148
+ }
149
+ termSync() {
150
+ if (this.#activeCommand.promptEndMarker == null || this.#activeCommand.promptStartMarker == null) {
151
+ return;
152
+ }
153
+ const globalCursorPosition = this.#terminal.buffer.active.baseY + this.#terminal.buffer.active.cursorY;
154
+ this.#maxCursorY = Math.max(this.#maxCursorY, globalCursorPosition);
155
+ if (globalCursorPosition < this.#activeCommand.promptStartMarker.line || globalCursorPosition < this.#maxCursorY) {
156
+ this.handleClear();
157
+ this.#activeCommand.promptEndMarker = this.#terminal.registerMarker(0);
158
+ return;
159
+ }
160
+ if (this.#activeCommand.promptEndMarker == null)
161
+ return;
162
+ // if the prompt is set, now parse out the values from the terminal
163
+ if (this.#activeCommand.promptText != null) {
164
+ const commandLines = this._getCommandLines();
165
+ const { suggestion, preCursorCommand, postCursorCommand } = this._getCommandText(commandLines);
166
+ const command = preCursorCommand + postCursorCommand.trim();
167
+ const cursorAtEndOfInput = postCursorCommand.trim() == "";
168
+ const hasOutput = this._getCommandOutputStatus(commandLines.length);
169
+ this.#activeCommand.persistentOutput = this.#activeCommand.hasOutput && hasOutput;
170
+ this.#activeCommand.hasOutput = hasOutput;
171
+ this.#activeCommand.suggestionsText = suggestion;
172
+ this.#activeCommand.commandText = command;
173
+ this.#activeCommand.cursorTerminated = cursorAtEndOfInput;
174
+ }
175
+ log.debug({
176
+ msg: "cmd manager state",
177
+ ...this.#activeCommand,
178
+ promptEndMarker: this.#activeCommand.promptEndMarker?.line,
179
+ promptStartMarker: this.#activeCommand.promptStartMarker?.line,
180
+ cursorX: this.#terminal.buffer.active.cursorX,
181
+ cursorY: globalCursorPosition,
182
+ });
183
+ }
184
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { spawn } from "./pty.js";
4
+ export default { spawn };