mirror of
https://github.com/jarun/nnn.git
synced 2025-02-26 03:01:32 +00:00
Support file and dir create
This commit is contained in:
parent
5ab6dd2fed
commit
28f69e59fa
4 changed files with 87 additions and 25 deletions
19
README.md
19
README.md
|
@ -105,7 +105,7 @@ Have fun with it! PRs are welcome. Check out [#1](https://github.com/jarun/nnn/i
|
||||||
- Numeric order (1, 2, ... 10, 11, ...) for numeric names
|
- Numeric order (1, 2, ... 10, 11, ...) for numeric names
|
||||||
- Sort by modification time, size
|
- Sort by modification time, size
|
||||||
- Convenience
|
- Convenience
|
||||||
- Rename files
|
- Create, rename files and directories
|
||||||
- Spawn SHELL (fallback sh) in the current directory
|
- Spawn SHELL (fallback sh) in the current directory
|
||||||
- Invoke file path copier (*easy* shell integration)
|
- Invoke file path copier (*easy* shell integration)
|
||||||
- Change directory at exit (*easy* shell integration)
|
- Change directory at exit (*easy* shell integration)
|
||||||
|
@ -209,17 +209,18 @@ Search keyword and option completion scripts for Bash, Fish and Zsh can be found
|
||||||
- | Go to last visited dir
|
- | Go to last visited dir
|
||||||
/ | Filter dir contents
|
/ | Filter dir contents
|
||||||
^/ | Open desktop search tool
|
^/ | Open desktop search tool
|
||||||
. | Toggle hide .dot files
|
. | Toggle hide . files
|
||||||
b | Show bookmark prompt
|
b | Bookmark prompt
|
||||||
^B | Pin current dir
|
^B | Pin current dir
|
||||||
^V | Go to pinned dir
|
^V | Go to pinned dir
|
||||||
c | Show change dir prompt
|
c | Change dir prompt
|
||||||
d | Toggle detail view
|
d | Toggle detail view
|
||||||
D | Show current file details
|
D | File details
|
||||||
m | Show concise media info
|
m | Brief media info
|
||||||
M | Show full media info
|
M | Full media info
|
||||||
|
n | Create new
|
||||||
^R | Rename selected entry
|
^R | Rename selected entry
|
||||||
s | Toggle sort by file size
|
s | Toggle sort by size
|
||||||
S | Toggle disk usage mode
|
S | Toggle disk usage mode
|
||||||
t | Toggle sort by mtime
|
t | Toggle sort by mtime
|
||||||
! | Spawn SHELL in dir
|
! | Spawn SHELL in dir
|
||||||
|
@ -228,7 +229,7 @@ Search keyword and option completion scripts for Bash, Fish and Zsh can be found
|
||||||
p | Open entry in PAGER
|
p | Open entry in PAGER
|
||||||
^K | Invoke file path copier
|
^K | Invoke file path copier
|
||||||
^L | Redraw, clear prompt
|
^L | Redraw, clear prompt
|
||||||
? | Show help, settings
|
? | Help, settings
|
||||||
Q | Quit and change dir
|
Q | Quit and change dir
|
||||||
q, ^Q | Quit
|
q, ^Q | Quit
|
||||||
```
|
```
|
||||||
|
|
4
nnn.1
4
nnn.1
|
@ -72,9 +72,11 @@ Toggle detail view
|
||||||
.It Ic D
|
.It Ic D
|
||||||
Show current file details screen
|
Show current file details screen
|
||||||
.It Ic m
|
.It Ic m
|
||||||
Show concise media info
|
Show brief media info
|
||||||
.It Ic M
|
.It Ic M
|
||||||
Show full media info
|
Show full media info
|
||||||
|
.It Ic n
|
||||||
|
Create a new file or directory
|
||||||
.It Ic ^R
|
.It Ic ^R
|
||||||
Rename selected entry
|
Rename selected entry
|
||||||
.It Ic s
|
.It Ic s
|
||||||
|
|
80
nnn.c
80
nnn.c
|
@ -254,6 +254,7 @@ static char * const utils[] = {
|
||||||
static char *STR_NFTWFAIL = "nftw(3) failed";
|
static char *STR_NFTWFAIL = "nftw(3) failed";
|
||||||
static char *STR_ATROOT = "You are at /";
|
static char *STR_ATROOT = "You are at /";
|
||||||
static char *STR_NOHOME = "HOME not set";
|
static char *STR_NOHOME = "HOME not set";
|
||||||
|
static char *STR_INPUT = "No traversal delimiter allowed";
|
||||||
|
|
||||||
/* For use in functions which are isolated and don't return the buffer */
|
/* For use in functions which are isolated and don't return the buffer */
|
||||||
static char g_buf[MAX_CMD_LEN];
|
static char g_buf[MAX_CMD_LEN];
|
||||||
|
@ -1016,8 +1017,12 @@ xreadline(char *fname)
|
||||||
wchar_t *buf = (wchar_t *)g_buf;
|
wchar_t *buf = (wchar_t *)g_buf;
|
||||||
size_t buflen = NAME_MAX - 1;
|
size_t buflen = NAME_MAX - 1;
|
||||||
|
|
||||||
DPRINTF_S(fname)
|
if (fname) {
|
||||||
|
DPRINTF_S(fname);
|
||||||
len = pos = mbstowcs(buf, fname, NAME_MAX);
|
len = pos = mbstowcs(buf, fname, NAME_MAX);
|
||||||
|
} else
|
||||||
|
len = (size_t)-1;
|
||||||
|
|
||||||
/* For future: handle NULL, say for a new name */
|
/* For future: handle NULL, say for a new name */
|
||||||
if (len == (size_t)-1) {
|
if (len == (size_t)-1) {
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
|
@ -1083,7 +1088,7 @@ xreadline(char *fname)
|
||||||
if (old_curs != ERR) curs_set(old_curs);
|
if (old_curs != ERR) curs_set(old_curs);
|
||||||
|
|
||||||
settimeout();
|
settimeout();
|
||||||
DPRINTF_S(buf)
|
DPRINTF_S(buf);
|
||||||
wcstombs(g_buf, buf, NAME_MAX);
|
wcstombs(g_buf, buf, NAME_MAX);
|
||||||
return g_buf;
|
return g_buf;
|
||||||
}
|
}
|
||||||
|
@ -1647,17 +1652,18 @@ show_help(char *path)
|
||||||
"e- | Go to last visited dir\n"
|
"e- | Go to last visited dir\n"
|
||||||
"e/ | Filter dir contents\n"
|
"e/ | Filter dir contents\n"
|
||||||
"d^/ | Open desktop search tool\n"
|
"d^/ | Open desktop search tool\n"
|
||||||
"e. | Toggle hide .dot files\n"
|
"e. | Toggle hide . files\n"
|
||||||
"eb | Show bookmark prompt\n"
|
"eb | Bookmark prompt\n"
|
||||||
"d^B | Pin current dir\n"
|
"d^B | Pin current dir\n"
|
||||||
"d^V | Go to pinned dir\n"
|
"d^V | Go to pinned dir\n"
|
||||||
"ec | Show change dir prompt\n"
|
"ec | Change dir prompt\n"
|
||||||
"ed | Toggle detail view\n"
|
"ed | Toggle detail view\n"
|
||||||
"eD | Show current file details\n"
|
"eD | File details\n"
|
||||||
"em | Show concise media info\n"
|
"em | Brief media info\n"
|
||||||
"eM | Show full media info\n"
|
"eM | Full media info\n"
|
||||||
|
"en | Create new\n"
|
||||||
"d^R | Rename selected entry\n"
|
"d^R | Rename selected entry\n"
|
||||||
"es | Toggle sort by file size\n"
|
"es | Toggle sort by size\n"
|
||||||
"eS | Toggle disk usage mode\n"
|
"eS | Toggle disk usage mode\n"
|
||||||
"et | Toggle sort by mtime\n"
|
"et | Toggle sort by mtime\n"
|
||||||
"e! | Spawn SHELL in dir\n"
|
"e! | Spawn SHELL in dir\n"
|
||||||
|
@ -1666,7 +1672,7 @@ show_help(char *path)
|
||||||
"ep | Open entry in PAGER\n"
|
"ep | Open entry in PAGER\n"
|
||||||
"d^K | Invoke file path copier\n"
|
"d^K | Invoke file path copier\n"
|
||||||
"d^L | Redraw, clear prompt\n"
|
"d^L | Redraw, clear prompt\n"
|
||||||
"e? | Show help, settings\n"
|
"e? | Help, settings\n"
|
||||||
"eQ | Quit and change dir\n"
|
"eQ | Quit and change dir\n"
|
||||||
"aq, ^Q | Quit\n\n");
|
"aq, ^Q | Quit\n\n");
|
||||||
|
|
||||||
|
@ -2607,6 +2613,56 @@ nochange:
|
||||||
} else if (!copier)
|
} else if (!copier)
|
||||||
printmsg("NNN_COPIER is not set");
|
printmsg("NNN_COPIER is not set");
|
||||||
goto nochange;
|
goto nochange;
|
||||||
|
case SEL_NEW:
|
||||||
|
printprompt("name: ");
|
||||||
|
tmp = xreadline(NULL);
|
||||||
|
clearprompt();
|
||||||
|
if (tmp == NULL || tmp[0] == '\0')
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Allow only relative, same dir paths */
|
||||||
|
if (tmp[0] == '/' || basename(tmp) != tmp) {
|
||||||
|
printmsg(STR_INPUT);
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open the descriptor to currently open directory */
|
||||||
|
fd = open(path, O_RDONLY | O_DIRECTORY);
|
||||||
|
if (fd == -1) {
|
||||||
|
printwarn();
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if another file with same name exists */
|
||||||
|
if (faccessat(fd, tmp, F_OK, AT_SYMLINK_NOFOLLOW) != -1) {
|
||||||
|
printmsg("Entry exists");
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if it's a dir or file */
|
||||||
|
printprompt("Press 'f' for file or 'd' for dir");
|
||||||
|
cleartimeout();
|
||||||
|
r = getch();
|
||||||
|
settimeout();
|
||||||
|
if (r == 'f') {
|
||||||
|
r = openat(fd, tmp, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||||
|
close(r);
|
||||||
|
} else if (r == 'd')
|
||||||
|
r = mkdirat(fd, tmp, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||||
|
else {
|
||||||
|
close(fd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r == -1) {
|
||||||
|
printwarn();
|
||||||
|
close(fd);
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
mkpath(path, tmp, oldpath, PATH_MAX);
|
||||||
|
goto begin;
|
||||||
case SEL_RENAME:
|
case SEL_RENAME:
|
||||||
if (ndents <= 0)
|
if (ndents <= 0)
|
||||||
break;
|
break;
|
||||||
|
@ -2617,9 +2673,9 @@ nochange:
|
||||||
if (tmp == NULL || tmp[0] == '\0')
|
if (tmp == NULL || tmp[0] == '\0')
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Allow only relative paths */
|
/* Allow only relative, same dir paths */
|
||||||
if (tmp[0] == '/' || basename(tmp) != tmp) {
|
if (tmp[0] == '/' || basename(tmp) != tmp) {
|
||||||
printmsg("relative paths only");
|
printmsg(STR_INPUT);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
nnn.h
7
nnn.h
|
@ -34,6 +34,7 @@ enum action {
|
||||||
SEL_MTIME,
|
SEL_MTIME,
|
||||||
SEL_REDRAW,
|
SEL_REDRAW,
|
||||||
SEL_COPY,
|
SEL_COPY,
|
||||||
|
SEL_NEW,
|
||||||
SEL_RENAME,
|
SEL_RENAME,
|
||||||
SEL_HELP,
|
SEL_HELP,
|
||||||
SEL_RUN,
|
SEL_RUN,
|
||||||
|
@ -138,12 +139,14 @@ static struct key bindings[] = {
|
||||||
{ 't', SEL_MTIME, "", "" },
|
{ 't', SEL_MTIME, "", "" },
|
||||||
/* Redraw window */
|
/* Redraw window */
|
||||||
{ CONTROL('L'), SEL_REDRAW, "", "" },
|
{ CONTROL('L'), SEL_REDRAW, "", "" },
|
||||||
{ KEY_F(5), SEL_REDRAW, "", "" },
|
{ KEY_F(5), SEL_REDRAW, "", "" }, /* Undocumented */
|
||||||
/* Copy currently selected file path */
|
/* Copy currently selected file path */
|
||||||
{ CONTROL('K'), SEL_COPY, "", "" },
|
{ CONTROL('K'), SEL_COPY, "", "" },
|
||||||
|
/* Create a new file */
|
||||||
|
{ 'n', SEL_NEW, "", "" },
|
||||||
/* Show rename prompt */
|
/* Show rename prompt */
|
||||||
{ CONTROL('R'), SEL_RENAME, "", "" },
|
{ CONTROL('R'), SEL_RENAME, "", "" },
|
||||||
{ KEY_F(2), SEL_RENAME, "", "" },
|
{ KEY_F(2), SEL_RENAME, "", "" }, /* Undocumented */
|
||||||
/* Show help */
|
/* Show help */
|
||||||
{ '?', SEL_HELP, "", "" },
|
{ '?', SEL_HELP, "", "" },
|
||||||
/* Run command */
|
/* Run command */
|
||||||
|
|
Loading…
Add table
Reference in a new issue