mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 05:41:31 +00:00
Remove libmagic, use file command.
The magic.mgc file gets created in the local directory, opening the global file throws unwanted messages. So we are using the o/p of the file command to determine if the file is a plain text file.
This commit is contained in:
parent
25e1dcaa8b
commit
2958ecd040
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@ MANPREFIX = $(PREFIX)/man
|
||||||
#CPPFLAGS = -DDEBUG
|
#CPPFLAGS = -DDEBUG
|
||||||
#CFLAGS = -g
|
#CFLAGS = -g
|
||||||
CFLAGS = -O3 -march=native
|
CFLAGS = -O3 -march=native
|
||||||
LDLIBS = -lcurses -lmagic
|
LDLIBS = -lcurses
|
||||||
|
|
||||||
DISTFILES = noice.c strlcat.c strlcpy.c util.h config.def.h\
|
DISTFILES = noice.c strlcat.c strlcpy.c util.h config.def.h\
|
||||||
noice.1 Makefile README LICENSE
|
noice.1 Makefile README LICENSE
|
||||||
|
|
|
@ -9,6 +9,7 @@ char *idlecmd = "rain"; /* The screensaver program */
|
||||||
|
|
||||||
struct assoc assocs[] = {
|
struct assoc assocs[] = {
|
||||||
//{ "\\.(avi|mp4|mkv|mp3|ogg|flac|mov)$", "mpv" },
|
//{ "\\.(avi|mp4|mkv|mp3|ogg|flac|mov)$", "mpv" },
|
||||||
|
{ "\\.(c|cpp|h|txt|log)$", "vim" },
|
||||||
{ "\\.(wma|mp3|ogg|flac)$", "fmedia" },
|
{ "\\.(wma|mp3|ogg|flac)$", "fmedia" },
|
||||||
//{ "\\.(png|jpg|gif)$", "feh" },
|
//{ "\\.(png|jpg|gif)$", "feh" },
|
||||||
//{ "\\.(html|svg)$", "firefox" },
|
//{ "\\.(html|svg)$", "firefox" },
|
||||||
|
|
37
noice.c
37
noice.c
|
@ -17,7 +17,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <magic.h>
|
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -39,6 +38,7 @@
|
||||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||||
#define ISODD(x) ((x) & 1)
|
#define ISODD(x) ((x) & 1)
|
||||||
#define CONTROL(c) ((c) ^ 0x40)
|
#define CONTROL(c) ((c) ^ 0x40)
|
||||||
|
#define MAX_PATH_LEN 1024
|
||||||
|
|
||||||
struct assoc {
|
struct assoc {
|
||||||
char *regex; /* Regex to match on filename */
|
char *regex; /* Regex to match on filename */
|
||||||
|
@ -208,19 +208,6 @@ openwith(char *file)
|
||||||
char *bin = NULL;
|
char *bin = NULL;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
const char *mime;
|
|
||||||
magic_t magic;
|
|
||||||
|
|
||||||
magic = magic_open(MAGIC_MIME_TYPE);
|
|
||||||
magic_load(magic, NULL);
|
|
||||||
magic_compile(magic, NULL);
|
|
||||||
mime = magic_file(magic, file);
|
|
||||||
DPRINTF_S(mime);
|
|
||||||
|
|
||||||
if (strcmp(mime, "text/plain") == 0)
|
|
||||||
return "vim";
|
|
||||||
magic_close(magic);
|
|
||||||
|
|
||||||
for (i = 0; i < LEN(assocs); i++) {
|
for (i = 0; i < LEN(assocs); i++) {
|
||||||
if (regcomp(®ex, assocs[i].regex,
|
if (regcomp(®ex, assocs[i].regex,
|
||||||
REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
|
REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
|
||||||
|
@ -666,13 +653,31 @@ nochange:
|
||||||
goto begin;
|
goto begin;
|
||||||
case S_IFREG:
|
case S_IFREG:
|
||||||
bin = openwith(newpath);
|
bin = openwith(newpath);
|
||||||
|
char *execvim = "vim";
|
||||||
|
|
||||||
if (bin == NULL) {
|
if (bin == NULL) {
|
||||||
char cmd[512];
|
FILE *fp;
|
||||||
|
char cmd[MAX_PATH_LEN];
|
||||||
int status;
|
int status;
|
||||||
sprintf(cmd, "xdg-open \"%s\" > /dev/null 2>&1", newpath);
|
|
||||||
|
snprintf(cmd, MAX_PATH_LEN, "file \"%s\"", newpath);
|
||||||
|
fp = popen(cmd, "r");
|
||||||
|
if (fp == NULL)
|
||||||
|
goto nochange;
|
||||||
|
if (fgets(cmd, MAX_PATH_LEN, fp) == NULL) {
|
||||||
|
pclose(fp);
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
pclose(fp);
|
||||||
|
|
||||||
|
if (strstr(cmd, "ASCII text") != NULL)
|
||||||
|
bin = execvim;
|
||||||
|
else {
|
||||||
|
snprintf(cmd, MAX_PATH_LEN, "xdg-open \"%s\" > /dev/null 2>&1", newpath);
|
||||||
status = system(cmd);
|
status = system(cmd);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
exitcurses();
|
exitcurses();
|
||||||
spawn(bin, newpath, NULL);
|
spawn(bin, newpath, NULL);
|
||||||
initcurses();
|
initcurses();
|
||||||
|
|
Loading…
Reference in a new issue