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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import { connect } from "react-redux";
import React from "react";
import Input from "./console/Input";
import Completion from "./console/Completion";
import Message from "./console/Message";
import * as consoleActions from "../../console/actions/console";
import { State as AppState } from "../reducers";
import CommandLineParser, {
InputPhase,
} from "../commandline/CommandLineParser";
import { Command } from "../../shared/Command";
import ColorScheme from "../../shared/ColorScheme";
import { LightTheme, DarkTheme } from "./Theme";
import styled from "./Theme";
import { ThemeProvider } from "styled-components";
const ConsoleWrapper = styled.div`
border-top: 1px solid gray;
`;
const COMPLETION_MAX_ITEMS = 33;
type StateProps = ReturnType<typeof mapStateToProps>;
interface DispatchProps {
dispatch: (action: any) => void;
}
type Props = StateProps & DispatchProps;
class Console extends React.Component<Props> {
private input: React.RefObject<Input>;
private commandLineParser: CommandLineParser = new CommandLineParser();
constructor(props: Props) {
super(props);
this.input = React.createRef();
}
onBlur() {
if (this.props.mode === "command" || this.props.mode === "find") {
return this.props.dispatch(consoleActions.hideCommand());
}
}
doEnter(e: React.KeyboardEvent<HTMLInputElement>) {
e.stopPropagation();
e.preventDefault();
const value = (e.target as HTMLInputElement).value;
if (this.props.mode === "command") {
return this.props.dispatch(consoleActions.enterCommand(value));
} else if (this.props.mode === "find") {
return this.props.dispatch(
consoleActions.enterFind(value === "" ? undefined : value)
);
}
}
selectNext(e: React.KeyboardEvent<HTMLInputElement>) {
this.props.dispatch(consoleActions.completionNext());
e.stopPropagation();
e.preventDefault();
}
selectPrev(e: React.KeyboardEvent<HTMLInputElement>) {
this.props.dispatch(consoleActions.completionPrev());
e.stopPropagation();
e.preventDefault();
}
onKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
switch (e.key) {
case "Escape":
return this.props.dispatch(consoleActions.hideCommand());
case "Enter":
return this.doEnter(e);
case "Tab":
if (e.shiftKey) {
this.props.dispatch(consoleActions.completionPrev());
} else {
this.props.dispatch(consoleActions.completionNext());
}
e.stopPropagation();
e.preventDefault();
break;
case "[":
if (e.ctrlKey) {
e.preventDefault();
return this.props.dispatch(consoleActions.hideCommand());
}
break;
case "c":
if (e.ctrlKey) {
e.preventDefault();
return this.props.dispatch(consoleActions.hideCommand());
}
break;
case "m":
if (e.ctrlKey) {
return this.doEnter(e);
}
break;
case "n":
if (e.ctrlKey) {
this.selectNext(e);
}
break;
case "p":
if (e.ctrlKey) {
this.selectPrev(e);
}
break;
}
}
onChange(e: React.ChangeEvent<HTMLInputElement>) {
const text = e.target.value;
this.props.dispatch(consoleActions.setConsoleText(text));
if (this.props.mode !== "command") {
return;
}
this.updateCompletions(text);
}
componentDidUpdate(prevProps: Props) {
if (prevProps.mode !== "command" && this.props.mode === "command") {
this.updateCompletions(this.props.consoleText);
this.focus();
} else if (prevProps.mode !== "find" && this.props.mode === "find") {
this.focus();
}
}
render() {
let theme = this.props.colorscheme;
if (this.props.colorscheme === ColorScheme.System) {
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
theme = ColorScheme.Dark;
} else {
theme = ColorScheme.Light;
}
}
switch (this.props.mode) {
case "command":
case "find":
return (
<ThemeProvider
theme={theme === ColorScheme.Dark ? DarkTheme : LightTheme}
>
<ConsoleWrapper>
<Completion
size={COMPLETION_MAX_ITEMS}
completions={this.props.completions}
select={this.props.select}
/>
<Input
ref={this.input}
mode={this.props.mode}
onBlur={this.onBlur.bind(this)}
onKeyDown={this.onKeyDown.bind(this)}
onChange={this.onChange.bind(this)}
value={this.props.consoleText}
/>
</ConsoleWrapper>
</ThemeProvider>
);
case "info":
case "error":
return (
<ThemeProvider
theme={theme === ColorScheme.Dark ? DarkTheme : LightTheme}
>
<Message mode={this.props.mode}>{this.props.messageText}</Message>
</ThemeProvider>
);
default:
return null;
}
}
async focus() {
this.props.dispatch(consoleActions.setColorScheme());
window.focus();
if (this.input.current) {
this.input.current.focus();
}
}
private updateCompletions(text: string) {
const phase = this.commandLineParser.inputPhase(text);
if (phase === InputPhase.OnCommand) {
return this.props.dispatch(consoleActions.getCommandCompletions(text));
} else {
const cmd = this.commandLineParser.parse(text);
switch (cmd.command) {
case Command.Open:
case Command.TabOpen:
case Command.WindowOpen:
this.props.dispatch(
consoleActions.getOpenCompletions(
this.props.completionTypes,
text,
cmd.command,
cmd.args
)
);
break;
case Command.Buffer:
this.props.dispatch(
consoleActions.getTabCompletions(text, cmd.command, cmd.args, false)
);
break;
case Command.BufferDelete:
case Command.BuffersDelete:
this.props.dispatch(
consoleActions.getTabCompletions(text, cmd.command, cmd.args, true)
);
break;
case Command.BufferDeleteForce:
case Command.BuffersDeleteForce:
this.props.dispatch(
consoleActions.getTabCompletions(text, cmd.command, cmd.args, false)
);
break;
case Command.Set:
this.props.dispatch(
consoleActions.getPropertyCompletions(text, cmd.command, cmd.args)
);
break;
}
}
}
}
const mapStateToProps = (state: AppState) => ({ ...state });
export default connect(mapStateToProps)(Console);
|