-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht_reducers.js
121 lines (107 loc) · 3.83 KB
/
t_reducers.js
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
import {
EXECUTE_INPUT, RECIEVED_OUTPUT, RECIEVED_ERROR, UPDATE_INPUT, LINE_HEIGHT,
ADJUST_POSITION, DEACTIVATE_TERMINAL, ACTIVATE_TERMINAL, CURSOR_TO_LEFT, CURSOR_TO_RIGHT,
NEW_PROMPT, TO_PREV, TO_NEXT, OLD_PROMPT, CUT_INPUT,
} from './t_actions';
const historyReducers = function (state = [], action) {
switch (action.type) {
case EXECUTE_INPUT:
return [...state, { type: EXECUTE_INPUT.toLowerCase(), value: action.input }];
case RECIEVED_OUTPUT:
return state.concat(action.output.map(
str => ({ type: RECIEVED_OUTPUT.toLowerCase(), value: str })
));
case RECIEVED_ERROR:
return state.concat(action.error.map(
str => ({ type: RECIEVED_ERROR.toLowerCase(), value: str })
));
default:
return state;
}
};
const inputStatusReducers = function (state = null, action) {
switch (action.type) {
case EXECUTE_INPUT:
case RECIEVED_OUTPUT:
case RECIEVED_ERROR:
return action.type;
default:
return state;
}
};
const getPromptHistory = (prompts = []) => ({
ref: prompts.length,
prompts,
});
const propmtHistoryReducers = function (state = getPromptHistory(), action, currentPrompt) {
switch (action.type) {
case EXECUTE_INPUT:
return getPromptHistory([...state.prompts, currentPrompt]);
case TO_PREV:
return Object.assign({}, state,
{ ref: (state.ref > 0) ? (state.ref - 1) : 0 });
case TO_NEXT:
return Object.assign({}, state,
{ ref: (state.ref < state.prompts.length - 1) ? state.ref + 1 : state.ref });
default:
return state;
}
};
const defaultPromptState = {
input: '',
isActive: false,
cursorLeftPos: 0,
cursorLeftStack: [0],
lineHeight: 0,
};
const cutInput = ({ cursorLeftPos, input }) =>
input.substring(0, cursorLeftPos - 1) + input.substring(cursorLeftPos, input.length);
const injectInput = ({ cursorLeftPos, input }, action) =>
input.substring(0, cursorLeftPos) + action.input + input.substring(cursorLeftPos, input.length);
const promptReducers = function (state = defaultPromptState, action, inputsHistory) {
switch (action.type) {
case ADJUST_POSITION:
if (typeof(state.cursorLeftStack[action.index]) !== 'number') {
const stack = state.cursorLeftStack;
stack[action.index] = action.pos;
return Object.assign({}, state, { cursorLeftStack: stack });
}
return state;
case DEACTIVATE_TERMINAL:
return Object.assign({}, state, { isActive: false });
case ACTIVATE_TERMINAL:
return Object.assign({}, state, { isActive: true });
case UPDATE_INPUT:
return Object.assign({}, state,
{ input: injectInput(state, action), cursorLeftPos: state.cursorLeftPos + 1 });
case CURSOR_TO_LEFT:
return Object.assign({}, state, { cursorLeftPos: state.cursorLeftPos - 1 });
case CURSOR_TO_RIGHT:
return Object.assign({}, state, { cursorLeftPos: state.cursorLeftPos + 1 });
case LINE_HEIGHT:
return Object.assign({}, state, { lineHeight: action.lineheight });
case NEW_PROMPT:
return Object.assign({}, state, { input: '', cursorLeftPos: 0 });
case OLD_PROMPT:
return inputsHistory.prompts[inputsHistory.ref] || state;
case CUT_INPUT:
if (state.cursorLeftPos === 0) {
return state;
}
return Object.assign({}, state,
{ input: cutInput(state), cursorLeftPos: state.cursorLeftPos - 1 });
default:
return state;
}
};
export const reducers = (state = {}, action) => ({
history: historyReducers(state.history, action),
input_status: inputStatusReducers(state.input_status, action),
prompt_history: propmtHistoryReducers(state.prompt_history, action, state.prompt),
prompt: promptReducers(state.prompt, action, state.prompt_history),
});
export default function (state = {}, action) {
return {
terminal: reducers(state.terminal, action),
};
}