Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 637fec5

Browse files
committed
Adding mv command, closes Jaredk3nt#2
1 parent d5f7652 commit 637fec5

5 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ All notable changes to the "Home Term" will be documented in this file.
1010
| Minor | Addition of new functionality (commands, etc) |
1111
| Patch | Bug/style fixes and theme additions |
1212

13+
## 1.4.0
14+
15+
- Adding mv command for renaming directories and links
16+
1317
## 1.3.0
1418

1519
- Added flags for changing search engine on `search` command

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Here are the currently available commands that can be run on the terminal.
2727
| `search` | `search [-e] ["<search string>"]` | Search with given search string (Must be in quotes to capture multiple words). Supply `-e` with a search URL or pre-defined engine (`ddg`, `google`, `bing`) to change search engine |
2828
| `clear` | `clear` | Clear the terminal of past commands. |
2929
| `theme` | `theme [<theme name>]` | Change theme. |
30+
| `mv` | `mv <source path> <target path>` | "Move" file or directory. Allows for renaming/moving resources within the file tree |
3031
| `help` | `help [<command>]` | Get information on commands. |
3132

3233
## Theming

‎src/cli.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const COMMANDS = {
3030
func: joinWriter(tree, treeWriter),
3131
help: "usage: tree",
3232
},
33+
mv: {
34+
func: joinWriter(mv, textWriter),
35+
help: "usage: mv <source path> <target path>"
36+
}
3337
};
3438

3539
// Global data

‎src/commands.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,24 @@ function tree(input) {
210210
return err;
211211
}
212212
}
213+
214+
function mv(input) {
215+
try {
216+
if (input.length) {
217+
const sourcePath = input[0].split("/");
218+
// Target will contain "new" name
219+
const targetPath = input[1].split("/");
220+
const sourceParent = locateParentPath(sourcePath);
221+
const sourceName = sourcePath[sourcePath.length - 1];
222+
const target = locateParentPath(targetPath);
223+
const targetName = targetPath[targetPath.length - 1];
224+
// Assign new target
225+
target[targetName] = sourceParent[sourceName];
226+
// Remove old source
227+
delete sourceParent[sourceName];
228+
writeLinks();
229+
}
230+
} catch (err) {
231+
return err;
232+
}
233+
}

‎src/utils.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ function pushCommand(cmd) {
4343
// Returns link url if link or cursor if directory
4444
// Throw error if bad path
4545
function locatePath(path) {
46+
let cursor = locateParentPath(path);
47+
const final = path[path.length - 1];
48+
if (!cursor[final]) {
49+
throw `no such link or directory: ${final}`;
50+
}
51+
return cursor[final];
52+
}
53+
54+
function locateParentPath(fullPath) {
55+
const path = fullPath.slice(0, fullPath.length - 1);
4656
let cursor = getCurrentCursor();
4757
const newPosition = [...position];
4858
for (let i = 0; i < path.length; i++) {
@@ -55,11 +65,7 @@ function locatePath(path) {
5565
throw `no such link or directory: ${m}`;
5666
}
5767
if (locationType(cursor[m]) === types.LINK) {
58-
if (i === path.length - 1) {
59-
return cursor[m];
60-
} else {
61-
throw `not a directory: ${m}`;
62-
}
68+
throw `not a directory: ${m}`;
6369
}
6470
newPosition.push(m);
6571
cursor = getCursor(newPosition);

0 commit comments

Comments
 (0)