Thumbnail

rani/cscroll.git

Clone URL: https://git.buni.party/rani/cscroll.git

commit 2ed64437441017d2b59f024e6e7508a1beb648d3 Author: Raniconduh <clagv.randomgames@gmail.com> Date: Thu Jan 06 16:00:02 2022 +0000 added optional nerd icons diff --git a/Makefile b/Makefile index 4762741..03e4474 100644 --- a/Makefile +++ b/Makefile @@ -119 +122 @@  SOURCES ?= src/*.c +HEADERS ?= include/*.h  INCLUDEDIR ?= include  DEST ?= cscroll   +ICONS ?= 1 +  CC ?= cc -CFLAGS ?= -Wall -Wextra -pedantic $(shell pkg-config --cflags ncurses) -LIBS ?= $(shell pkg-config --libs ncurses) -ltinfo +CFLAGS += -DICONS=$(ICONS) -Wall -Wextra -pedantic $(shell pkg-config --cflags ncurses) +LIBS += $(shell pkg-config --libs ncurses) -ltinfo    PREFIX ?= /usr/local    all: cscroll   -cscroll: $(SOURCES) +cscroll: $(SOURCES) $(HEADERS)   $(CC) -I$(INCLUDEDIR) -o $(DEST) $(SOURCES) $(CFLAGS) $(LIBS)   -debug: $(SOURCES) +debug: $(SOURCES) $(HEADERS)   $(CC) -I$(INCLUDEDIR) -DDEBUG -o $(DEST) $(SOURCES) $(CFLAGS) $(LIBS) -g    install: all diff --git a/include/commands.h b/include/commands.h index 2db52ba..e0afbae 100644 --- a/include/commands.h +++ b/include/commands.h @@ -77 +78 @@ void create_cuts(char *, char **);  void free_cuts(void);  void paste_cuts(char *);  void run_cmd(char *); - +void set(char *); +void unset(char *);    extern bool cutting;  extern char * cut_start_dir; diff --git a/include/icons.h b/include/icons.h new file mode 100644 index 0000000..65bb47b --- /dev/null +++ b/include/icons.h @@ -00 +1112 @@ +#ifndef ICONS_H +#define ICONS_H + +#define ICON_DIR "\uf74a" +#define ICON_GEAR "\uf013" + +#define ICON_APK "\ue70e" +#define ICON_EXEC ICON_GEAR +#define ICON_SHELL "\uf489" +#define ICON_C "\ue61e" +#define ICON_CPP "\ue61d" +#define ICON_CS "\uf81a" +#define ICON_CSS "\ue749" +#define ICON_FSHARP "\ue7a7" +#define ICON_XML "\uf81a" +#define ICON_RB "\ue739" +#define ICON_LUA "\ue620" +#define ICON_GIT "\ue702" +#define ICON_GO "\ue626" +#define ICON_HTML "\ue736" +#define ICON_JAVA "\ue738" +#define ICON_JS "\ue781" +#define ICON_JSON "\ue60b" +#define ICON_PY "\ue73c" +#define ICON_SCALA "\ue737" +#define ICON_MD "\ue609" +#define ICON_VIM "\ue62b" + +#define ICON_AUDIO "\uf722" +#define ICON_VIDEO "\uf72a" +#define ICON_GENERIC "\uf15b" +#define ICON_ARCHIVE "\uf1c6" + +struct icon_pair { + char * ext; + char * icon; +}; + +struct icon_pair icons[] = { + /* A */ + {"apk", ICON_APK}, + {"asm", ICON_GEAR}, + /* B */ + {"bash", ICON_SHELL}, + {"bin", ICON_EXEC}, + /* C */ + {"c", ICON_C}, + {"c++", ICON_CPP}, + {"cc", ICON_CPP}, + {"class", ICON_JAVA}, + {"cs", ICON_CS}, + {"css", ICON_CSS}, + /* D */ + /* E */ + /* F */ + {"f#", ICON_FSHARP}, + {"fish", ICON_SHELL}, + /* G */ + {"git", ICON_GIT}, + {"go", ICON_GO}, + /* H */ + {"h", ICON_C}, + {"hh", ICON_CPP}, + {"hpp", ICON_CPP}, + {"htm", ICON_HTML}, + {"html", ICON_HTML}, + /* I */ + /* J */ + {"java", ICON_JAVA}, + {"js", ICON_JS}, + {"json", ICON_JSON}, + /* K */ + /* L */ + {"lua", ICON_LUA}, + /* M */ + {"md", ICON_MD}, + /* N */ + /* O */ + {"o", ICON_EXEC}, + {"out", ICON_EXEC}, + /* P */ + {"py", ICON_PY}, + {"pyc", ICON_PY}, + {"pyd", ICON_PY}, + {"pyo", ICON_PY}, + /* Q */ + /* R */ + {"rb", ICON_RB}, + /* S */ + {"scala", ICON_SCALA}, + {"sh", ICON_SHELL}, + {"so", ICON_EXEC}, + /* T */ + /* U */ + /* V */ + {"vim", ICON_VIM}, + {"vimrc", ICON_VIM}, + /* W */ + /* X */ + {"xml", ICON_XML}, + /* Y */ + /* Z */ + {"zsh", ICON_SHELL}, +}; + +#define n_icons (sizeof(icons)/sizeof(icons[0])) + +int icmp(const void * a, const void * b) { + return strcmp(*(const char **)a, ((const struct icon_pair*)b)->ext); +} + +#endif /* ICONS_H */ diff --git a/include/opts.h b/include/opts.h new file mode 100644 index 0000000..0d4dddb --- /dev/null +++ b/include/opts.h @@ -00 +19 @@ +#ifndef OPTS_H +#define OPTS_H + +#include <stdio.h> + + +extern bool show_icons; + +#endif diff --git a/src/commands.c b/src/commands.c index 1abc157..f8293f8 100644 --- a/src/commands.c +++ b/src/commands.c @@ -76 +77 @@  #include <sys/wait.h>    #include "dir.h" +#include "opts.h"  #include "commands.h"     @@ -1373 +13831 @@ void run_cmd(char * cmd) {   clear();   refresh();  } + + +void set(char * v) { + if (0) + ; +#if ICONS + if (!strcmp(v, "icons")) show_icons = true; +#endif + else { + printw("Unknown variable (%s)", v); + refresh(); + napms(500); + } +} + + +void unset(char * v) { + if (0) + ; +#if ICONS + if (!strcmp(v, "icons")) show_icons = false; +#endif + else { + printw("Unknown variable (%s)", v); + refresh(); + napms(500); + } +} diff --git a/src/io.c b/src/io.c index bffea31..fae3be9 100644 --- a/src/io.c +++ b/src/io.c @@ -66 +611 @@  #include <unistd.h>  #include <fcntl.h>   +#if ICONS +#include "icons.h" +#include "type.h" +#endif +#include "opts.h"  #include "dir.h"  #include "io.h"   @@ -576 +629 @@ void curses_write_file(struct dir_entry_t * dir_entry, bool highlight) {   int cp = -1;   char f_ident;   char * u_text = ""; +#if ICONS + char * icon = NULL; +#endif     switch (dir_entry->file_type) {   case FILE_DIR: @@ -9030 +9864 @@ void curses_write_file(struct dir_entry_t * dir_entry, bool highlight) {   break;   }   - if (dir_entry->exec && dir_entry->file_type != FILE_LINK) { - cp = GREEN; - if (f_ident == NO_IDENT) f_ident = '*'; - } else if (cp == -1) { - cp = WHITE; - f_ident = ' '; - } -   switch (dir_entry->m_type) {   case MIME_MEDIA:   cp = MAGENTA; +#if ICONS + icon = ICON_VIDEO; +#endif   break;   case MIME_ARCHIVE:   cp = RED; +#if ICONS + icon = ICON_ARCHIVE; +#endif   break;   case MIME_UNKNOWN:   default:   break;   } - + +#if ICONS + // find icon if it is not media or an archive + if (!icon && show_icons) { + char * t_ext = get_ext(dir_entry->name); + if (t_ext && *t_ext) { + char * ext = malloc(strlen(t_ext)); + strcpy(ext, t_ext); + lowers(ext); + struct icon_pair * t_icon = + bsearch(&ext, icons, n_icons, sizeof(icons[0]), icmp); + if (t_icon) icon = t_icon->icon; + free(ext); + } + } + + if (!icon && dir_entry->file_type == FILE_DIR) icon = ICON_DIR; +#endif + + if (dir_entry->exec && dir_entry->file_type != FILE_LINK) { + cp = GREEN; + if (f_ident == NO_IDENT) f_ident = '*'; +#if ICONS + if (!icon) icon = ICON_GEAR; +#endif + } else if (cp == -1) { + cp = WHITE; + f_ident = ' '; + } + +#if ICONS + if (!icon) icon = ICON_GENERIC; +#endif +   cp = COLOR_PAIR((unsigned)cp);   if (highlight) cp |= A_REVERSE;     if (dir_entry->marked) printw("%c ", '-'); +#if ICONS + if (show_icons) printw("%s ", icon); +#endif   attron(cp);   printw("%s", dir_entry->name);   attroff(cp); diff --git a/src/main.c b/src/main.c index e4881a8..d27cccb 100644 --- a/src/main.c +++ b/src/main.c @@ -86 +87 @@    #include "commands.h"  #include "main.h" +#include "opts.h"  #include "dir.h"  #include "io.h"   @@ -196 +2010 @@ int main(int argc, char ** argv) {   print_path = true;   } else if (!strcmp(argv[i], "-nc")) {   color = false; +#if ICONS + } else if (!strcmp(argv[i], "-ni")) { +#endif + show_icons = false;   } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {   help();   } else { @@ -2667 +27114 @@ int main(int argc, char ** argv) {   break;   case ':':;   char * inp = curses_getline(":"); - if (!strcmp(inp, "ma") && !cutting) + char * sp = strchr(inp, ' '); + if (sp) { + *sp = 0; + char * cmd = inp; + char * args = ++sp; + if (!strcmp(cmd, "set")) set(args); + else if (!strcmp(cmd, "unset")) unset(args); + } else if (!strcmp(inp, "ma") && !cutting)   mark_all();   else if (!strcmp(inp, "mu")) {   if (cutting) free_cuts(); @@ -3636 +3759 @@ void help(void) {   "Options:\n"   " -h, --help Show this screen and exit\n"   " -nc Turn off colors\n" +#if ICONS + " -ni Turn off icons\n" +#endif   " -p Print the path cscroll is in when it exits\n"   "\n"   "See https://github.com/Raniconduh/cscroll for documentation\n" diff --git a/src/opts.c b/src/opts.c new file mode 100644 index 0000000..5786f6d --- /dev/null +++ b/src/opts.c @@ -00 +16 @@ +#include <stdbool.h> + +#include "opts.h" + + +bool show_icons = true;