2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
2017-05-26 06:20:00 +00:00
|
|
|
import * as actions from './actions';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
|
|
|
const HISTORY_MAX_LENGTH = 128;
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
const initialState = {
|
|
|
|
history: [],
|
2015-12-28 23:34:32 +00:00
|
|
|
index: 0
|
2018-04-25 03:36:27 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
export const getCurrentInputHistoryEntry = state => {
|
|
|
|
if (state.input.index === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
return state.input.history[state.input.index];
|
2017-05-26 06:20:00 +00:00
|
|
|
};
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(initialState, {
|
|
|
|
[actions.INPUT_HISTORY_ADD](state, { line }) {
|
|
|
|
if (line.trim() && line !== state.history[0]) {
|
2018-11-04 06:22:46 +00:00
|
|
|
if (state.history.length === HISTORY_MAX_LENGTH) {
|
2018-04-25 03:36:27 +00:00
|
|
|
state.history.pop();
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
state.history.unshift(line);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.INPUT_HISTORY_RESET](state) {
|
2018-04-25 03:36:27 +00:00
|
|
|
state.index = -1;
|
2015-12-28 23:34:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.INPUT_HISTORY_INCREMENT](state) {
|
2018-04-25 03:36:27 +00:00
|
|
|
if (state.index < state.history.length - 1) {
|
|
|
|
state.index++;
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.INPUT_HISTORY_DECREMENT](state) {
|
|
|
|
if (state.index >= 0) {
|
2018-04-25 03:36:27 +00:00
|
|
|
state.index--;
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 06:20:00 +00:00
|
|
|
|
|
|
|
export function addInputHistory(line) {
|
|
|
|
return {
|
|
|
|
type: actions.INPUT_HISTORY_ADD,
|
|
|
|
line
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resetInputHistory() {
|
|
|
|
return {
|
|
|
|
type: actions.INPUT_HISTORY_RESET
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function incrementInputHistory() {
|
|
|
|
return {
|
|
|
|
type: actions.INPUT_HISTORY_INCREMENT
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decrementInputHistory() {
|
|
|
|
return {
|
|
|
|
type: actions.INPUT_HISTORY_DECREMENT
|
|
|
|
};
|
|
|
|
}
|