Przeglądaj źródła

Use readline() at chdir prompt

With the features the readline library provides it would be too naive to ignore.
We break out of the curses mode into the prompt and get back in.
This change introduces dependency on libncurses.
master
Arun Prakash Jana 8 lat temu
rodzic
commit
89224cfa98
Nie znaleziono w bazie danych klucza dla tego podpisu ID klucza GPG: A75979F35C080412
4 zmienionych plików z 78 dodań i 12 usunięć
  1. +3
    -2
      Makefile
  2. +3
    -2
      Makefile.generic
  3. +1
    -1
      README.md
  4. +71
    -7
      nnn.c

+ 3
- 2
Makefile Wyświetl plik

@@ -4,10 +4,11 @@ PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man MANPREFIX = $(PREFIX)/share/man


CFLAGS += -O3 -march=native -Wall -Wextra -Wno-unused-parameter CFLAGS += -O3 -march=native -Wall -Wextra -Wno-unused-parameter
LDLIBS = -lreadline
ifeq ($(shell uname), Darwin) ifeq ($(shell uname), Darwin)
LDLIBS = -lncurses LDLIBS += -lncurses
else else
LDLIBS = -lncursesw LDLIBS += -lncursesw
endif endif


DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE


+ 3
- 2
Makefile.generic Wyświetl plik

@@ -4,10 +4,11 @@ PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man MANPREFIX = $(PREFIX)/share/man


CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter
LDLIBS = -lreadline
ifeq ($(shell uname), Darwin) ifeq ($(shell uname), Darwin)
LDLIBS = -lncurses LDLIBS += -lncurses
else else
LDLIBS = -lncursesw LDLIBS += -lncursesw
endif endif


DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE DISTFILES = nnn.c config.def.h nnn.1 Makefile README.md LICENSE


+ 1
- 1
README.md Wyświetl plik

@@ -141,7 +141,7 @@ nnn vs. ranger memory usage while viewing a directory with 10,178 files, sorted


### Installation ### Installation


nnn needs libncursesw on Linux (or ncurses on OS X) and standard libc. nnn needs libreadline and libncursesw (on Linux or ncurses on OS X) and standard libc.


- If you are using **Homebrew**, run: - If you are using **Homebrew**, run:




+ 71
- 7
nnn.c Wyświetl plik

@@ -1,26 +1,28 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <readline/readline.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/resource.h> #include <sys/resource.h>


#include <ctype.h>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <grp.h>
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
#include <pwd.h>
#include <regex.h> #include <regex.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include <pwd.h> #include <unistd.h>
#include <grp.h>


#define __USE_XOPEN_EXTENDED #define __USE_XOPEN_EXTENDED
#include <ftw.h> #include <ftw.h>
@@ -118,6 +120,13 @@ typedef struct entry {


typedef unsigned long ulong; typedef unsigned long ulong;


/* Externs */
#ifdef __APPLE__
extern int add_history(const char *);
#else
extern void add_history(const char *string);
#endif

/* Global context */ /* Global context */
static struct entry *dents; static struct entry *dents;
static int ndents, cur; static int ndents, cur;
@@ -375,6 +384,28 @@ xstricmp(const char *s1, const char *s2)
return (int) (TOUPPER(*s1) - TOUPPER(*s2)); return (int) (TOUPPER(*s1) - TOUPPER(*s2));
} }


static char *
strstrip(char *s)
{
size_t size;
char *end;

size = strlen(s);

if (!size)
return s;

end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';

while (*s && isspace(*s))
s++;

return s;
}

static char * static char *
openwith(char *file) openwith(char *file)
{ {
@@ -1420,14 +1451,39 @@ nochange:
cur = ndents - 1; cur = ndents - 1;
break; break;
case SEL_CD: case SEL_CD:
{
/* Read target dir */ /* Read target dir */
printprompt("chdir: "); char cwd[PATH_MAX];
tmp = readln(); tmp = getcwd(cwd, PATH_MAX);
if (tmp == NULL) { if (tmp == NULL) {
clearprompt(); printwarn();
goto nochange;
}

if (chdir(path) == -1) {
printwarn();
goto nochange; goto nochange;
} }


exitcurses();
char *tmp = readline("chdir: ");
initcurses();
tmp = tmp[0] ? tmp : NULL;
if (chdir(cwd) == -1)
printwarn();

if (tmp == NULL) {
/* Save current */
if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));

goto begin;
} else
add_history(tmp);

char *input = tmp;
tmp = strstrip(tmp);

if (tmp[0] == '~') { if (tmp[0] == '~') {
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home) if (home)
@@ -1441,8 +1497,13 @@ nochange:
mkpath(path, tmp, newpath, sizeof(newpath)); mkpath(path, tmp, newpath, sizeof(newpath));


if (canopendir(newpath) == 0) { if (canopendir(newpath) == 0) {
/* Save current */
if (ndents > 0)
mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));

printwarn(); printwarn();
goto nochange; free(input);
goto begin;
} }


/* Save last working directory */ /* Save last working directory */
@@ -1452,7 +1513,10 @@ nochange:
/* Reset filter */ /* Reset filter */
xstrlcpy(fltr, ifilter, sizeof(fltr)); xstrlcpy(fltr, ifilter, sizeof(fltr));
DPRINTF_S(path); DPRINTF_S(path);
oldpath[0] = '\0';
free(input);
goto begin; goto begin;
}
case SEL_CDHOME: case SEL_CDHOME:
tmp = getenv("HOME"); tmp = getenv("HOME");
if (tmp == NULL) { if (tmp == NULL) {


||||||
x
 
000:0
Ładowanie…
Anuluj
Zapisz