Compare commits

..

3 Commits

Author SHA1 Message Date
nova
bc6fb162c5 accounting for files "." and ".." 2025-07-08 00:43:19 +02:00
nova
b77c9a2a29 made configs constant 2025-07-08 00:20:14 +02:00
nova
3f8fdc9e17 improvenments to file previews 2025-07-07 19:28:34 +02:00
7 changed files with 47 additions and 37 deletions

View File

@@ -38,6 +38,10 @@ unsigned long get_dir_size(char *path){
}
}
closedir(dir);
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
/* removes files "." and ".." */
entry_count -= 2;
}
return entry_count;
}
@@ -45,7 +49,7 @@ unsigned long get_dir_size(char *path){
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
struct dirent **entry;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
scandir(path, &entry, NULL, alphasort);
scandir(path, &entry, skip_dot, alphasort);
} else {
scandir(path, &entry, skip_hidden_files, alphasort);
}

View File

@@ -2,7 +2,7 @@
#include "interactions.h"
#include "sorting.h"
static mimetype mimetype_default_cmd[] = {
static const mimetype mimetype_default_cmd[] = {
/* mimetype shell command
* ^ substring of "file --mime-type -b ./hovered"
* this does mean that this list is checked for completely linear.
@@ -17,7 +17,7 @@ static mimetype mimetype_default_cmd[] = {
{ "audio", "mpv" }
};
static binding key_binding[] = {
static const binding key_binding[] = {
/*key action blackmagic comment*/
/*you cannot add bindings that include other bindings in its entirety
* possible: mk, mf
@@ -40,7 +40,7 @@ static binding key_binding[] = {
* this means that it does not delete the hovered files if files are already selected */
{ "G", jump_bottom, NULL, "jump to last file in dir" },
{ "gg", jump_top, NULL, "jump to file 0" },
{ "gg", jump_top, NULL, "jump to first file in dir" },
{ "gh", jump_to_dir, "$HOME", "jump to $HOME" },
{ "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" },
@@ -70,5 +70,5 @@ static binding key_binding[] = {
{ "a", toggle_hidden_files, NULL, "toggle hidden files" },
};
static unsigned long binding_count = sizeof(key_binding) / sizeof(binding);
static unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype);
static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding);
static const unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype);

View File

@@ -3,7 +3,9 @@
#include <string.h>
#include "defines.h"
char* text(char *path, unsigned long *file_size);
char* generic(char *path);
char* get_mimetype(char *path){
static char *cmd_str = "file --mime-type -b ./\"";
@@ -24,32 +26,23 @@ char* get_mimetype(char *path){
pclose(cmd_open);
return line;
}
char* preview_file(char *path, unsigned long file_size){
char* preview_file(file *file_current){
/* this calls "file" on path */
char *file_buffer;
char *mime = get_mimetype(path);
unsigned int mime_len = strlen(mime);
char *mime = get_mimetype(file_current->file_name);
if (strstr(mime, "text")) {
file_buffer = text(path, &file_size);
file_buffer = text(file_current->file_name, &file_current->file_size);
} else {
file_buffer = malloc(mime_len + 1);
memset(file_buffer, ' ', mime_len);
memcpy(file_buffer, mime, mime_len);
file_buffer[mime_len] = '\0';
file_buffer = generic(file_current->file_name);
}
free(mime);
return file_buffer;
}
char* text(char *path, unsigned long *file_size){
char *file_buffer = malloc(*file_size + 1);
@@ -59,3 +52,14 @@ char* text(char *path, unsigned long *file_size){
return file_buffer;
}
char* generic(char *path){
char *cmd = concat("file ./\"", path);
cmd = concat(cmd, "\"");
FILE *cmd_open = popen(cmd, "r");
char *line;
size_t size = 0;
getline(&line, &size, cmd_open);
pclose(cmd_open);
return line;
}

View File

@@ -1,4 +1,5 @@
#include "file_previews.c"
#include "defines.h"
char* preview_file(char *path, unsigned long file_size);
char* preview_file(file *file_current);
char* get_mimetype(char *path);

View File

@@ -2,6 +2,7 @@
#include <dirent.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"
extern unsigned int settings;
@@ -13,6 +14,12 @@ int skip_hidden_files(const struct dirent *entry){
}
return 1;
}
int skip_dot(const struct dirent *entry){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
return 0;
}
return 1;
}
int sort_natural(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
@@ -49,8 +56,6 @@ int sort_alpha(const void *file0, const void *file1){
int sort_random(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
static int seed = 0;
static int random = 0;
@@ -60,9 +65,6 @@ int sort_random(const void *file0, const void *file1){
if (random == 0) {
random = seed;
}
if (strcmp(file_name0, ".") == 0 || strcmp(file_name0, "..") == 0) {
return -1;
}
char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1;
@@ -129,9 +131,6 @@ int sort_size(const void *file0, const void *file1){
unsigned long file_size1 = ((file*)file1)->file_size;
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
if (strcmp(file_name0, ".") == 0 || strcmp(file_name0, "..") == 0) {
return -1;
}
if (file_type0 == file_type1) {
if (file_size0 > file_size1) {
return -1;

View File

@@ -6,6 +6,8 @@
#include "sorting.c"
#endif
int skip_hidden_files(const struct dirent *entry);
int skip_dot(const struct dirent *entry);
void sort_dir(unsigned long *dir_length_width, char *dir_content);
void sort_dir(unsigned long *dir_length_width, char *dir_content);
int sort_natural(const void *file0, const void *file1);

View File

@@ -139,11 +139,11 @@ void *thread_rgt(void *data){
free(rgt_content);
rgt_content = malloc(sizeof(file));
rgt_content[0].file_name = malloc(file_current->file_name_width + 1);
strcpy(rgt_content[0].file_name, file_current->file_name);
rgt_content[0].file_name_width = file_current->file_name_width;
rgt_content[0].file_size = file_current->file_size;
rgt_content[0].file_type = file_current->file_type;
rgt_content->file_name = malloc(file_current->file_name_width + 1);
strcpy(rgt_content->file_name, file_current->file_name);
rgt_content->file_name_width = file_current->file_name_width;
rgt_content->file_size = file_current->file_size;
rgt_content->file_type = file_current->file_type;
pthread_mutex_unlock(&mutex_mid);
@@ -161,10 +161,10 @@ void *thread_rgt(void *data){
rgt_file_count = 1;
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
rgt_content[0].status = FILE_STATUS_HOVER;
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
free(rgt_buffer);
rgt_buffer = preview_file(rgt_content[0].file_name, rgt_content[0].file_size);
rgt_buffer = preview_file(rgt_content);
}