/ components / PromptInput / utils.ts
utils.ts
 1  import {
 2    hasUsedBackslashReturn,
 3    isShiftEnterKeyBindingInstalled,
 4  } from '../../commands/terminalSetup/terminalSetup.js'
 5  import type { Key } from '../../ink.js'
 6  import { getGlobalConfig } from '../../utils/config.js'
 7  import { env } from '../../utils/env.js'
 8  /**
 9   * Helper function to check if vim mode is currently enabled
10   * @returns boolean indicating if vim mode is active
11   */
12  export function isVimModeEnabled(): boolean {
13    const config = getGlobalConfig()
14    return config.editorMode === 'vim'
15  }
16  
17  export function getNewlineInstructions(): string {
18    // Apple Terminal on macOS uses native modifier key detection for Shift+Enter
19    if (env.terminal === 'Apple_Terminal' && process.platform === 'darwin') {
20      return 'shift + ⏎ for newline'
21    }
22  
23    // For iTerm2 and VSCode, show Shift+Enter instructions if installed
24    if (isShiftEnterKeyBindingInstalled()) {
25      return 'shift + ⏎ for newline'
26    }
27  
28    // Otherwise show backslash+return instructions
29    return hasUsedBackslashReturn()
30      ? '\\⏎ for newline'
31      : 'backslash (\\) + return (⏎) for newline'
32  }
33  
34  /**
35   * True when the keystroke is a printable character that does not begin
36   * with whitespace — i.e., a normal letter/digit/symbol the user typed.
37   * Used to gate the lazy space inserted after an image pill.
38   */
39  export function isNonSpacePrintable(input: string, key: Key): boolean {
40    if (
41      key.ctrl ||
42      key.meta ||
43      key.escape ||
44      key.return ||
45      key.tab ||
46      key.backspace ||
47      key.delete ||
48      key.upArrow ||
49      key.downArrow ||
50      key.leftArrow ||
51      key.rightArrow ||
52      key.pageUp ||
53      key.pageDown ||
54      key.home ||
55      key.end
56    ) {
57      return false
58    }
59    return input.length > 0 && !/^\s/.test(input) && !input.startsWith('\x1b')
60  }