aboutsummaryrefslogtreecommitdiff
path: root/src/console/components/CommandPrompt.tsx
blob: f6f4d8fcd42358a181d923df5b151eea8baa9175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from "react";
import * as consoleActions from "../actions/console";
import * as completionActions from "../actions/completion";
import AppContext from "./AppContext";
import CommandLineParser, {
  InputPhase,
} from "../commandline/CommandLineParser";
import Completion from "./console/Completion";
import Input from "./console//Input";
import { Command } from "../../shared/Command";
import styled from "styled-components";
import reducer, { defaultState, completedText } from "../reducers/completion";
import CompletionType from "../../shared/CompletionType";
import useAutoResize from "../hooks/useAutoResize";

const COMPLETION_MAX_ITEMS = 33;

const ConsoleWrapper = styled.div`
  border-top: 1px solid gray;
`;

const CommandPrompt: React.FC = () => {
  const { state, dispatch } = React.useContext(AppContext);
  const [completionState, completionDispatch] = React.useReducer(
    reducer,
    defaultState
  );
  const commandLineParser = new CommandLineParser();

  useAutoResize();

  const onBlur = () => {
    dispatch(consoleActions.hideCommand());
  };

  const doEnter = (e: React.KeyboardEvent<HTMLInputElement>) => {
    e.stopPropagation();
    e.preventDefault();

    const value = (e.target as HTMLInputElement).value;
    dispatch(consoleActions.enterCommand(value));
  };

  const selectNext = (e: React.KeyboardEvent<HTMLInputElement>) => {
    completionDispatch(completionActions.completionNext());
    e.stopPropagation();
    e.preventDefault();
  };

  const selectPrev = (e: React.KeyboardEvent<HTMLInputElement>) => {
    completionDispatch(completionActions.completionPrev());
    e.stopPropagation();
    e.preventDefault();
  };

  const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
    switch (e.key) {
      case "Escape":
        dispatch(consoleActions.hideCommand());
        break;
      case "Enter":
        doEnter(e);
        break;
      case "Tab":
        if (e.shiftKey) {
          completionDispatch(completionActions.completionPrev());
        } else {
          completionDispatch(completionActions.completionNext());
        }
        e.stopPropagation();
        e.preventDefault();
        break;
      case "[":
        if (e.ctrlKey) {
          e.preventDefault();
          dispatch(consoleActions.hideCommand());
        }
        break;
      case "c":
        if (e.ctrlKey) {
          e.preventDefault();
          dispatch(consoleActions.hideCommand());
        }
        break;
      case "m":
        if (e.ctrlKey) {
          doEnter(e);
        }
        break;
      case "n":
        if (e.ctrlKey) {
          selectNext(e);
        }
        break;
      case "p":
        if (e.ctrlKey) {
          selectPrev(e);
        }
        break;
    }
  };

  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const text = e.target.value;
    dispatch(consoleActions.setConsoleText(text));
    const action = getCompletionAction(text);
    Promise.resolve(action).then((a) => {
      if (a) {
        completionDispatch(a);
      }
    });
  };

  React.useEffect(() => {
    completionActions.startCompletion().then((action) => {
      completionDispatch(action);

      const completionAction = getCompletionAction(
        state.consoleText,
        action.completionTypes
      );
      Promise.resolve(completionAction).then((a) => {
        if (a) {
          completionDispatch(a);
        }
      });
    });
  }, []);

  const getCompletionAction = (
    text: string,
    completionTypes: CompletionType[] | undefined = undefined
  ) => {
    const types = completionTypes || completionState.completionTypes;
    const phase = commandLineParser.inputPhase(text);
    if (phase === InputPhase.OnCommand) {
      return completionActions.getCommandCompletions(text);
    } else {
      const cmd = commandLineParser.parse(text);
      switch (cmd.command) {
        case Command.Open:
        case Command.TabOpen:
        case Command.WindowOpen:
          return completionActions.getOpenCompletions(
            types,
            text,
            cmd.command,
            cmd.args
          );
        case Command.Buffer:
          return completionActions.getTabCompletions(
            text,
            cmd.command,
            cmd.args,
            false
          );
        case Command.BufferDelete:
        case Command.BuffersDelete:
          return completionActions.getTabCompletions(
            text,
            cmd.command,
            cmd.args,
            true
          );
        case Command.BufferDeleteForce:
        case Command.BuffersDeleteForce:
          return completionActions.getTabCompletions(
            text,
            cmd.command,
            cmd.args,
            false
          );
        case Command.Set:
          return completionActions.getPropertyCompletions(
            text,
            cmd.command,
            cmd.args
          );
      }
    }
    return undefined;
  };

  return (
    <ConsoleWrapper>
      <Completion
        size={COMPLETION_MAX_ITEMS}
        completions={completionState.completions}
        select={completionState.select}
      />
      <Input
        prompt={":"}
        onBlur={onBlur}
        onKeyDown={onKeyDown}
        onChange={onChange}
        value={
          completionState.select < 0
            ? state.consoleText
            : completedText(completionState)
        }
      />
    </ConsoleWrapper>
  );
};

export default CommandPrompt;