Compare commits

...

10 commits

6 changed files with 65 additions and 52 deletions

View file

@ -1,5 +1,4 @@
.POSIX:
.SUFFIXES:
VERSION = 1.0
TARGET = ccc
@ -8,18 +7,21 @@ PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1
CFLAGS = -Os -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600
CFLAGS += -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600
SRC = ccc.c util.c file.c icons.c
OBJS = $(SRC:.c=.o)
$(TARGET): $(SRC) config.h
$(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS)
.c.o:
$(CC) -o $@ $(CFLAGS) -c $<
$(TARGET): $(OBJS) config.h
$(CC) -o $@ $(OBJS) $(LIBS)
dist:
mkdir -p $(TARGET)-$(VERSION)
cp -R README.md $(MANPAGE) $(TARGET) $(TARGET)-$(VERSION)
tar -cf $(TARGET)-$(VERSION).tar $(TARGET)-$(VERSION)
gzip $(TARGET)-$(VERSION).tar
tar -czf $(TARGET)-$(VERSION).tar.gz $(TARGET)-$(VERSION)
rm -rf $(TARGET)-$(VERSION)
install: $(TARGET)
@ -31,11 +33,11 @@ install: $(TARGET)
chmod 644 $(DESTDIR)$(MANDIR)/$(MANPAGE)
uninstall:
rm $(DESTDIR)$(BINDIR)/$(TARGET)
rm $(DESTDIR)$(MANDIR)/$(MANPAGE)
rm -f $(DESTDIR)$(BINDIR)/$(TARGET)
rm -f $(DESTDIR)$(MANDIR)/$(MANPAGE)
clean:
rm $(TARGET)
rm -f $(TARGET) *.o
all: $(TARGET)

53
ccc.c
View file

@ -196,7 +196,7 @@ int main(int argc, char **argv)
while (1) {
list_files();
keybinding();
keybinding();
}
return 0;
}
@ -220,6 +220,7 @@ void handle_sigwinch(int ignore)
void cleanup(void)
{
hashtable_free();
if (files->length != 0) {
arraylist_free(files);
}
@ -408,13 +409,12 @@ void add_file_stat(char *filename, char *path, int ftype)
{
struct stat file_stat;
if (stat(path, &file_stat) == -1) {
/* can't be triggered? */
if (errno == EACCES)
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, 0, 0);
perror("stat()");
return;
}
int type;
char icon_str[5];
int type = 0;
char icon_str[8] = {0};
filename[strlen(filename)] = '\0';
/* handle file without extension
@ -426,17 +426,20 @@ void add_file_stat(char *filename, char *path, int ftype)
}
/* add file extension */
icon *ext_icon = hashtable_search(ext ? ext : filename);
if (!ext_icon)
memcpy(icon_str, "", 4);
else
memcpy(icon_str, ext_icon->icon, 4);
if (!ext_icon) {
char ch[] = "";
memcpy(icon_str, ch, sizeof(ch));
} else {
strncpy(icon_str, ext_icon->icon, sizeof(icon_str));
}
int color = DEF_COLOR;
if (S_ISDIR(file_stat.st_mode)) {
type = DRY; /* dir */
color = DIR_COLOR;
memcpy(icon_str, "󰉋", 4);
char ch[] = "󰉋";
memcpy(icon_str, ch, sizeof(ch));
} else if (S_ISREG(file_stat.st_mode)) {
type = REG; /* regular file */
color = REG_COLOR;
@ -536,24 +539,32 @@ void add_file_stat(char *filename, char *path, int ftype)
*/
void show_file_content(void)
{
if (sel_file >= files->length) {
return;
}
file current_file = files->items[sel_file];
move_cursor(1, half_width);
if (current_file.type == DRY) {
ArrayList *files_visit;
ArrayList *files_visit = NULL;
populate_files(current_file.name, 0, &files_visit);
if (!files_visit)
return;
for (long i = 0; i < files_visit->length && i < rows - 1; i++) {
char *line = get_line(files_visit, i, 0, show_icons);
int color = files_visit->items[i].color;
move_cursor(i + 1, half_width);
bprintf("\033[K\033[%dm%s\033[m\n", color, line);
free(line);
}
arraylist_free(files_visit);
return;
}
FILE *file = fopen(current_file.path, "r");
if (!file) {
bprintf("Unable to read %s", current_file.name);
/* bprintf("Unable to read %s", current_file.name ? current_file.name : "unknown"); */
bprintf("Unable to read unknown");
return;
}
@ -565,6 +576,7 @@ void show_file_content(void)
return;
}
}
fclose(file);
int pipe_fd[2];
if (pipe(pipe_fd) == -1) {
perror("pipe");
@ -642,7 +654,7 @@ void list_files(void)
/* calculate range of files to show */
long range = files->length;
/* not highlight if no files in directory */
if (range == 0 && errno == 0) {
if (range == 0) {
for (int i = 0; i < rows - 1; i++) {
move_cursor(i + 1, 1);
bprintf("\033[K");
@ -835,6 +847,9 @@ void nav_back(const Arg *arg)
void nav_enter(const Arg *arg)
{
if (sel_file >= files->length) {
return;
}
file c_file = files->items[sel_file];
/* Check if it is directory or a regular file */
if (c_file.type == DRY) {
@ -1419,12 +1434,8 @@ int get_window_size(int *row, int *col)
*/
void bprintf(const char *fmt, ...)
{
char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
write(STDOUT_FILENO, buffer, strlen(buffer));
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}

35
file.c
View file

@ -69,7 +69,7 @@ void arraylist_remove(ArrayList *list, long index)
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
{
file new_file = { name, path, type, stats, color };
strcpy(new_file.icon, icon);
strncpy(new_file.icon, icon, sizeof(new_file.icon) / sizeof(new_file.icon[0]));
if (list->capacity != list->length) {
if (marked) {
@ -105,25 +105,18 @@ char *get_line(ArrayList *list, long index, int detail, int icons)
{
file f = list->items[index];
size_t length = strlen(f.name) + 1;
if (detail) {
length += strlen(f.stats) + 1; /* 1 for space */
}
if (icons) {
length += 5; /* 4 for icon, 1 for space */
}
size_t length = strlen(f.name) + 1;
length += detail ? strlen(f.stats) + 1 : 0;
length += icons ? strlen(f.icon) + 1 : 0;
char *line = memalloc(length);
line[0] = '\0';
if (detail) {
strcat(line, f.stats);
strcat(line, " ");
}
if (icons) {
strcat(line, f.icon);
strcat(line, " ");
}
strcat(line, f.name);
line[length] = '\0';
return line;
char *line = memalloc(length);
snprintf(line, length, "%s%s%s%s%s",
detail ? f.stats : "",
detail ? " " : "",
icons ? f.icon : "",
icons ? " " : "",
f.name);
return line;
}

2
file.h
View file

@ -19,7 +19,7 @@ typedef struct {
int type;
char *stats;
int color;
char icon[5];
char icon[8];
} file;
typedef struct {

View file

@ -187,3 +187,9 @@ icon *hashtable_search(char *name)
return NULL;
}
void hashtable_free(void)
{
for (int i = 0; i < TABLE_SIZE; i++)
free(hash_table[i]);
}

View file

@ -14,5 +14,6 @@ void hashtable_init(void);
void hashtable_print(void);
int hashtable_add(icon *p);
icon *hashtable_search(char *name);
void hashtable_free(void);
#endif