Skip to content

Commit

Permalink
feature(vim) add hot keys
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Sep 8, 2017
1 parent dc79ab4 commit 3240227
Show file tree
Hide file tree
Showing 13 changed files with 649 additions and 21 deletions.
28 changes: 26 additions & 2 deletions HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,20 @@ Cloud Commander supports command line parameters:
| `--console` | enable console
| `--terminal` | enable terminal
| `--terminal-path` | set terminal path
| `--vim` | enable vim hot keys
| `--no-server` | do not start server
| `--no-auth` | disable authorization
| `--no-online` | load scripts from local server
| `--no-open` | do not open web browser when server started
| `--no-name` | set empty tab name in web browser
| `--no-one-panel-mode` | unset one panel mode
| `--no-progress` | do not show progress of file operations
| `--no-html-dialogs` | do not use html dialogs
| `--no-one-panel-mode` | unset one panel mode
| `--no-contact` | disable contact
| `--no-config-dialog` | disable config dialog
| `--no-console` | disable console
| `--no-terminal` | disable terminal
| `--no-name` | set empty tab name in web browser
| `--no-vim` | disable vim hot keys


If no parameters given Cloud Commander reads information from `~/.cloudcmd.json` and use
Expand Down Expand Up @@ -174,6 +176,27 @@ Hot keys
| `~` | console
| `Ctrl + Click` | open file on new tab

### Vim

When `--vim` option provided, or configuration parameter `vim` set, next hot keys become available:

|Key |Operation
|:----------------------|:--------------------------------------------
| `j` | navigate to next file
| `k` | navigate to previous file
| `dd` | remove current file
| `G` | navigate to bottom file
| `gg` | navigate to top file
| `v` | visual mode
| `y` | copy (selected in visual mode files)
| `p` | paste files
| `Esc` | unselect all

Commands can be joined, for example:
- `5j` will navigate `5` files below current;
- `d5j` will remove next `5` files;
- `dG` will remove all files from current to bottom;

View
---------------
![View](/img/screen/view.png "View")
Expand Down Expand Up @@ -333,6 +356,7 @@ Here is description of options:
"console" : true, /* enable console */
"terminal" : false, /* disable terminal */
"terminalPath" : '', /* path of a terminal */
"vim" : false, /* disable vim hot keys */
}
```

Expand Down
8 changes: 8 additions & 0 deletions client/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ function CmdProto() {
return Cmd;
};

this.unselectFile = (currentFile) => {
const current = currentFile || DOM.getCurrentFile();

current.classList.remove(SELECTED_FILE);

return Cmd;
};

this.toggleSelectedFile = (currentFile) => {
const current = currentFile || DOM.getCurrentFile();
const name = DOM.getCurrentName(current);
Expand Down
19 changes: 13 additions & 6 deletions client/key/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const exec = require('execon');
const Events = require('../dom/events');
const Buffer = require('../dom/buffer');
const KEY = require('./key');
const vim = require('./vim');
const setCurrentByChar = require('./set-current-by-char');
const fullstore = require('fullstore/legacy');
const Chars = fullstore();
Expand Down Expand Up @@ -71,15 +72,19 @@ function KeyProto() {
char = isSymbol;
}

/* in case buttons can be processed */
if (!Key.isBind())
return;

if (!isNumpad && !alt && !ctrl && !meta && (isBetween || isSymbol))
const isVim = CloudCmd.config('vim');

if (!isVim && !isNumpad && !alt && !ctrl && !meta && (isBetween || isSymbol))
return setCurrentByChar(char, Chars);

Chars([]);
switchKey(event);

if (isVim)
vim(char, event);
}

function getSymbol(shift, keyCode) {
Expand All @@ -106,6 +111,8 @@ function KeyProto() {
function switchKey(event) {
let i, isSelected, prev, next;
let current = Info.element;
let dataName;

const name = Info.name;

const {Operation} = CloudCmd;
Expand Down Expand Up @@ -301,9 +308,9 @@ function KeyProto() {

event.preventDefault();

const attr = Info.panel.getAttribute('data-name');
dataName = Info.panel.getAttribute('data-name');

if (attr === 'js-right')
if (dataName === 'js-right')
DOM.duplicatePanel();

break;
Expand All @@ -314,9 +321,9 @@ function KeyProto() {

event.preventDefault();

name = Info.panel.getAttribute('data-name');
dataName = Info.panel.getAttribute('data-name');

if (name === 'js-left')
if (dataName === 'js-left')
DOM.duplicatePanel();

break;
Expand Down
9 changes: 9 additions & 0 deletions client/key/key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
BACKSPACE : 8,
TAB : 9,
Expand All @@ -20,13 +22,20 @@ module.exports = {

ZERO : 48,

SEMICOLON : 52,

COLON : 54,

A : 65,

C : 67,
D : 68,

G : 71,

J : 74,
K : 75,

M : 77,

O : 79,
Expand Down
3 changes: 1 addition & 2 deletions client/key/set-current-by-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

const Info = DOM.CurrentInfo;

const {escapeRegExp} = require('../../common/util');

module.exports = function setCurrentByChar(char, charStore) {
Expand Down Expand Up @@ -56,5 +55,5 @@ module.exports = function setCurrentByChar(char, charStore) {
DOM.setCurrentFile(firstByName);
charStore([char]);
}
}
};

172 changes: 172 additions & 0 deletions client/key/vim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
'use strict';
/* global CloudCmd, DOM */

const Info = DOM.CurrentInfo;
const KEY = require('./key');

const fullstore = require('fullstore/legacy');
const store = fullstore('');
const visual = fullstore(false);

const stopVisual = () => {
visual(false);
};

const end = () => {
store('');
};

const rmFirst = (a) => {
return a
.split('')
.slice(1)
.join('');
};

module.exports = (key, event) => {
const current = Info.element;
const keyCode = event.keyCode;
const prevStore = store();

const value = store(prevStore.concat(key));

if (keyCode === KEY.ENTER)
return end();

if (keyCode === KEY.ESC) {
DOM.unselectFiles();
visual(false);
return end();
}

if (key === 'j') {
move('next', {
prevStore,
current,
});

return end();
}

if (key === 'k') {
move('previous', {
prevStore,
current,
});

return end();
}

if (/gg/.test(value)) {
move('previous', {
current,
prevStore,
max: Infinity,
});

return end();
}

if (key === 'd' && (visual() || prevStore === 'd')) {
CloudCmd.Operation.show('delete');
stopVisual();
return end();
}

if (key === 'G') {
move('next', {
current,
prevStore,
max: Infinity,
});

return end();
}

if (key === 'y') {
if (!visual())
return end();

DOM.Buffer.copy();
stopVisual();
DOM.unselectFiles();
return end();
}

if (/^p$/i.test(key)) {
DOM.Buffer.paste();
return end();
}

if (/^v$/i.test(key)) {
DOM.toggleSelectedFile(current);
visual(!visual());

return end();
}
};

module.exports.selectFile = selectFile;

function move(sibling, {max, current, prevStore}) {
const isDelete = prevStore[0] === 'd';

if (isDelete) {
visual(true);
prevStore = rmFirst(prevStore);
}

const n = max || getNumber(prevStore);

if (isNaN(n))
return;

setCurrent({
n,
current,
sibling,
visual: visual(),
});

if (isDelete)
CloudCmd.Operation.show('delete');
}

function getNumber(value) {
if (!value)
return 1;

if (value === 'g')
return 1;

return parseInt(value);
}

function selectFile(current) {
const name = DOM.getCurrentName(current);

if (name === '..')
return;

DOM.selectFile(current);
}

function setCurrent({n, current, visual, sibling}) {
const select = visual ? selectFile : DOM.unselectFile;

select(current);

const position = `${sibling}Sibling`;
for (let i = 0; i < n; i++) {
const next = current[position];

if (!next)
break;

current = next;
select(current);
}

DOM.setCurrentFile(current);
}

Binary file modified img/screen/config.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion json/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"console": true,
"terminal": false,
"terminalPath": "",
"showConfig": "false"
"showConfig": "false",
"vim": "true"
}

10 changes: 6 additions & 4 deletions json/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@
"--port ": "set port number",
"--progress ": "show progress of file operations",
"--html-dialogs ": "use html dialogs",
"--open ": "open web browser when server started",
"--name ": "set tab name in web browser",
"--one-panel-mode ": "set one panel mode",
"--config-dialog ": "enable config dialog",
"--console ": "enable console",
"--contact ": "enable contact",
"--terminal ": "enable terminal",
"--terminal-path ": "set terminal path",
"--open ": "open web browser when server started",
"--name ": "set tab name in web browser",
"--vim ": "enable vim hot keys",
"--no-server ": "do not start server",
"--no-auth ": "disable authorization",
"--no-online ": "load scripts from local server",
"--no-open ": "do not open web browser when server started",
"--no-name ": "set default tab name in web browser",
"--no-one-panel-mode ": "unset one panel mode",
"--no-progress ": "do not show progress of file operations",
"--no-html-dialogs ": "do not use html dialogs",
"--no-one-panel-mode ": "unset one panel mode",
"--no-config-dialog ": "disable config dialog",
"--no-console ": "disable console",
"--no-contact ": "disable contact",
"--no-terminal ": "disable terminal",
"--no-name ": "set default tab name in web browser"
"--no-vim ": "disable vim hot keys"
}

0 comments on commit 3240227

Please sign in to comment.