From 6b709dada9dafe45cb8580349bb2c08d2f393b71 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 27 May 2019 20:26:31 +0300 Subject: [PATCH] docs(cloudcmd) add menu cookbook --- User-Menu-Cookbook.md | 101 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 User-Menu-Cookbook.md diff --git a/User-Menu-Cookbook.md b/User-Menu-Cookbook.md new file mode 100644 index 0000000..4206214 --- /dev/null +++ b/User-Menu-Cookbook.md @@ -0,0 +1,101 @@ +User Menu is a power feature of `Cloud Commander` that was added in [v12.2.0](https://github.com/coderaiser/cloudcmd/releases/tag/v12.2.0). + +If you create file `.cloudcmd.menu.js` in the `HOME` or any top-level directory, it will be used when you press +`F2`. If you enabled it before with help of `--user-menu` command line flag. + +Here you can find ready to use scripts for your `user menu`. + +## Rename file + +The simplest way to show rename file dialog. + +```js +module.exports = { + 'F2 - Rename file': async ({DOM}) => { + await DOM.renameCurrent(); + }, +}; +``` + +## Convert flac to mp3 + +```js + +const isMp3 = (a) => /\.mp3$/.test(a); + +module.exports = { + 'F - Convert flac to mp3': async ({DOM, CloudCmd}) => { + const convert = 'for f in *.flac; do ffmpeg -vsync 2 -i "$f" -b:a 320k "${f%flac}mp3"; done'; + const command = `bash -c '${convert}'`; + + const exitCode = await CloudCmd.TerminalRun.show({ + command, + }); + + if (exitCode) + return; + + await CloudCmd.refresh(); + + const {IO, CurrentInfo} = DOM; + + // get mp3 file names + const {dirPath, files} = CurrentInfo; + const names = DOM.getFilenames(files); + const mp3Dir = `${dirPath}mp3`; + const mp3Names = names.filter(isMp3); + + // create mp3 directory + await IO.write(`${mp3Dir}?dir`); + + // move *.mp3 files to a mp3 directory + await IO.mv({ + from: dirPath, + to: mp3Dir, + names: mp3Names, + }); + + await CloudCmd.refresh(); + }, +} +``` + +## Create User Menu File + +```js + module.exports = { + 'C - Create User Menu File': async ({DOM, CloudCmd}) => { + const {CurrentInfo} = DOM; + + const {dirPath} = CurrentInfo; + const path = `${dirPath}.cloudcmd.menu.js`; + const {prefix} = CloudCmd; + + const data = await readDefaultMenu({prefix}); + await createDefaultMenu({ + path, + data, + DOM, + CloudCmd, + }); + }, +}; + +async function createDefaultMenu({path, data, DOM, CloudCmd}) { + const {IO} = DOM; + + await IO.write(path, data); + await CloudCmd.refresh(); + + DOM.setCurrentByName('.cloudcmd.menu.js'); + + await CloudCmd.EditFile.show(); +} + +async function readDefaultMenu({prefix}) { + const res = await fetch(`${prefix}/api/v1/user-menu/default`); + const data = await res.text(); + + return data; +} +``` \ No newline at end of file