opening files using mimetypes defined in config.h
This commit is contained in:
@ -14,6 +14,14 @@ extern unsigned int file_modifiers;
|
|||||||
extern unsigned int color_count;
|
extern unsigned int color_count;
|
||||||
extern color *colors;
|
extern color *colors;
|
||||||
|
|
||||||
|
char* concat(const char *s1, const char *s2){
|
||||||
|
const size_t len1 = strlen(s1);
|
||||||
|
const size_t len2 = strlen(s2);
|
||||||
|
char *result = malloc(len1 + len2 + 1);
|
||||||
|
memcpy(result, s1, len1);
|
||||||
|
memcpy(result + len1, s2, len2 + 1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long get_dir_size(char *path){
|
unsigned long get_dir_size(char *path){
|
||||||
DIR *dir = opendir(path);
|
DIR *dir = opendir(path);
|
||||||
|
4
config.h
4
config.h
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
static mimetype mimetype_default_cmd[] = {
|
static mimetype mimetype_default_cmd[] = {
|
||||||
/* mimetype shell command */
|
/* mimetype shell command */
|
||||||
{ "text", "$EDITOR" }
|
{ "text", "$EDITOR" },
|
||||||
|
{ "image", "feh" },
|
||||||
|
{ "video", "mpv" }
|
||||||
};
|
};
|
||||||
|
|
||||||
static binding key_binding[] = {
|
static binding key_binding[] = {
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
#define STATUS_UPDATE_SCREEN_MASK 24 /* 11000 */
|
#define STATUS_UPDATE_SCREEN_MASK 24 /* 11000 */
|
||||||
#define STATUS_UPDATE_SCREEN_0 8
|
#define STATUS_UPDATE_SCREEN_0 8
|
||||||
#define STATUS_UPDATE_SCREEN_RESIZE 16
|
#define STATUS_UPDATE_SCREEN_RESIZE 16
|
||||||
#define STATUS_USER_ROOT 32
|
#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32
|
||||||
|
#define STATUS_USER_ROOT 64
|
||||||
|
|
||||||
#define SETTINGS_HAS_COLOR 1
|
#define SETTINGS_HAS_COLOR 1
|
||||||
|
|
||||||
|
@ -4,15 +4,12 @@
|
|||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
char* text(char *path, unsigned long *file_size);
|
char* text(char *path, unsigned long *file_size);
|
||||||
char* preview_file(char *path, unsigned long file_size){
|
|
||||||
/* this calls "file" on path */
|
|
||||||
|
|
||||||
char *file_buffer;
|
|
||||||
unsigned long cmd_size = 0;
|
|
||||||
|
|
||||||
|
char* get_mimetype(char *path){
|
||||||
static char *cmd_str = "file --mime-type -b ./\"";
|
static char *cmd_str = "file --mime-type -b ./\"";
|
||||||
unsigned long cmd_len = strlen(cmd_str);
|
unsigned long cmd_len = strlen(cmd_str);
|
||||||
unsigned int path_len = strlen(path);
|
unsigned int path_len = strlen(path);
|
||||||
|
|
||||||
char *cmd = malloc((cmd_len + path_len) + 2);
|
char *cmd = malloc((cmd_len + path_len) + 2);
|
||||||
memset(cmd, ' ', cmd_len + path_len);
|
memset(cmd, ' ', cmd_len + path_len);
|
||||||
memcpy(cmd, cmd_str, cmd_len);
|
memcpy(cmd, cmd_str, cmd_len);
|
||||||
@ -23,23 +20,37 @@ char* preview_file(char *path, unsigned long file_size){
|
|||||||
FILE *cmd_open = popen(cmd, "r");
|
FILE *cmd_open = popen(cmd, "r");
|
||||||
char *line;
|
char *line;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
char *tmp = getline(&line, &size, cmd_open);
|
getline(&line, &size, cmd_open);
|
||||||
cmd_size = strlen(line);
|
|
||||||
pclose(cmd_open);
|
pclose(cmd_open);
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
char* preview_file(char *path, unsigned long file_size){
|
||||||
|
/* this calls "file" on path */
|
||||||
|
|
||||||
if (strstr(line, "text")) {
|
char *file_buffer;
|
||||||
|
|
||||||
|
|
||||||
|
char *mime = get_mimetype(path);
|
||||||
|
|
||||||
|
unsigned int mime_len = strlen(mime);
|
||||||
|
|
||||||
|
if (strstr(mime, "text")) {
|
||||||
file_buffer = text(path, &file_size);
|
file_buffer = text(path, &file_size);
|
||||||
} else {
|
} else {
|
||||||
file_buffer = malloc(cmd_size + 1);
|
|
||||||
memset(file_buffer, ' ', cmd_size);
|
file_buffer = malloc(mime_len + 1);
|
||||||
memcpy(file_buffer, line, cmd_size);
|
memset(file_buffer, ' ', mime_len);
|
||||||
file_buffer[cmd_size] = '\0';
|
memcpy(file_buffer, mime, mime_len);
|
||||||
|
file_buffer[mime_len] = '\0';
|
||||||
}
|
}
|
||||||
free(cmd);
|
free(mime);
|
||||||
return file_buffer;
|
return file_buffer;
|
||||||
|
|
||||||
}
|
}
|
||||||
char* text(char *path, unsigned long *file_size) {
|
|
||||||
|
|
||||||
|
|
||||||
|
char* text(char *path, unsigned long *file_size){
|
||||||
|
|
||||||
char *file_buffer = malloc(*file_size + 1);
|
char *file_buffer = malloc(*file_size + 1);
|
||||||
FILE *fp = fopen(path, "r");
|
FILE *fp = fopen(path, "r");
|
||||||
@ -48,11 +59,3 @@ char* text(char *path, unsigned long *file_size) {
|
|||||||
|
|
||||||
return file_buffer;
|
return file_buffer;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
FILE *cmd_open = popen(cmd, "r");
|
|
||||||
memset(file_buffer, ' ', file_size);
|
|
||||||
fread(file_buffer, file_size, 1, cmd_open);
|
|
||||||
file_buffer[file_size] = '\0';
|
|
||||||
pclose(cmd_open);
|
|
||||||
return file_buffer;
|
|
||||||
*/
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
#include "file_previews.c"
|
#include "file_previews.c"
|
||||||
|
|
||||||
char* preview_file(char *path, unsigned long file_size);
|
char* preview_file(char *path, unsigned long file_size);
|
||||||
|
char* get_mimetype(char *path);
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@ -10,12 +12,28 @@ extern unsigned int file_modifiers;
|
|||||||
unsigned long selected_file_current;
|
unsigned long selected_file_current;
|
||||||
unsigned long selected_file_last;
|
unsigned long selected_file_last;
|
||||||
extern pthread_mutex_t mutex_selection;
|
extern pthread_mutex_t mutex_selection;
|
||||||
|
extern pthread_mutex_t mutex_rgt;
|
||||||
|
extern pthread_mutex_t mutex_mid;
|
||||||
extern file *mid_content;
|
extern file *mid_content;
|
||||||
extern file *lft_content;
|
extern file *lft_content;
|
||||||
extern file *rgt_content;
|
extern file *rgt_content;
|
||||||
|
extern file file_current;
|
||||||
|
|
||||||
|
extern char *rgt_buffer;
|
||||||
|
extern char *btm_buffer;
|
||||||
|
|
||||||
extern unsigned int status;
|
extern unsigned int status;
|
||||||
|
|
||||||
|
void user_interactions(char *input) {
|
||||||
|
void (*func_ptr)();
|
||||||
|
unsigned long i = 0;
|
||||||
|
for (i = 0; i < binding_count; i++) {
|
||||||
|
if (*input == key_binding[i].key) {
|
||||||
|
func_ptr = key_binding[i].func;
|
||||||
|
func_ptr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
void quit_program(){
|
void quit_program(){
|
||||||
status = STATUS_QUIT_PROGRAM;
|
status = STATUS_QUIT_PROGRAM;
|
||||||
}
|
}
|
||||||
@ -39,7 +57,30 @@ void move_right(){
|
|||||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||||
}
|
}
|
||||||
void move_left(){
|
void move_left(){
|
||||||
chdir(mid_content[selected_file_current].file_name);
|
if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) {
|
||||||
|
chdir(file_current.file_name);
|
||||||
|
} else {
|
||||||
|
unsigned long i = 0;
|
||||||
|
char *mime = get_mimetype(file_current.file_name);
|
||||||
|
for (i = 0; i < mimetype_default_count; i++) {
|
||||||
|
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
|
||||||
|
|
||||||
|
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
|
||||||
|
cmd = concat(cmd, file_current.file_name);
|
||||||
|
cmd = concat(cmd, "\"");
|
||||||
|
btm_buffer = malloc(strlen(cmd));
|
||||||
|
|
||||||
|
strcpy(btm_buffer, cmd);
|
||||||
|
|
||||||
|
|
||||||
|
system(cmd);
|
||||||
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(mime);
|
||||||
|
}
|
||||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||||
}
|
}
|
||||||
void toggle_hidden_files(){
|
void toggle_hidden_files(){
|
||||||
@ -59,15 +100,3 @@ void jump_top(){
|
|||||||
pthread_mutex_unlock(&mutex_selection);
|
pthread_mutex_unlock(&mutex_selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void user_interactions(char *input) {
|
|
||||||
void (*func_ptr)();
|
|
||||||
unsigned long i = 0;
|
|
||||||
for (i = 0; i < binding_count; i++) {
|
|
||||||
if (*input == key_binding[i].key) {
|
|
||||||
func_ptr = key_binding[i].func;
|
|
||||||
func_ptr();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#ifndef CONFIG_GUARD
|
#ifndef INTERACTIONS_GUARD
|
||||||
#define CONFIG_GUARD
|
#define INTERACTIONS_GUARD
|
||||||
#include "interactions.c"
|
#include "interactions.c"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
15
main.c
15
main.c
@ -20,6 +20,7 @@ unsigned int status;
|
|||||||
unsigned int timeout_time = 0;
|
unsigned int timeout_time = 0;
|
||||||
char input = 0;
|
char input = 0;
|
||||||
|
|
||||||
|
|
||||||
void render_pass(WINDOW *wint, WINDOW *winb, WINDOW *winl, WINDOW *winm, WINDOW *winr);
|
void render_pass(WINDOW *wint, WINDOW *winb, WINDOW *winl, WINDOW *winm, WINDOW *winr);
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
@ -96,9 +97,15 @@ int main(){
|
|||||||
pthread_join(thread_b, NULL);
|
pthread_join(thread_b, NULL);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
curs_set(1);
|
delwin(win_l);
|
||||||
|
delwin(win_m);
|
||||||
|
delwin(win_r);
|
||||||
|
delwin(win_t);
|
||||||
|
delwin(win_b);
|
||||||
endwin();
|
endwin();
|
||||||
refresh();
|
noraw();
|
||||||
|
curs_set(1);
|
||||||
|
echo();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +113,10 @@ int main(){
|
|||||||
void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){
|
void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){
|
||||||
|
|
||||||
if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) {
|
if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) {
|
||||||
|
if (status & STATUS_UPDATE_SCREEN_RELOAD_FULL) {
|
||||||
|
clear();
|
||||||
|
status &= ~STATUS_UPDATE_SCREEN_RELOAD_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*TODO: check if deallocation of window and reallocation is faster than this or not */
|
/*TODO: check if deallocation of window and reallocation is faster than this or not */
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ file *rgt_content;
|
|||||||
file *mid_content;
|
file *mid_content;
|
||||||
file *lft_content;
|
file *lft_content;
|
||||||
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
||||||
|
char *btm_buffer;
|
||||||
|
|
||||||
file file_current;
|
file file_current;
|
||||||
|
|
||||||
@ -184,6 +185,7 @@ void threading_init(){
|
|||||||
|
|
||||||
top_content = malloc(sizeof(char));
|
top_content = malloc(sizeof(char));
|
||||||
rgt_buffer = malloc(sizeof(char));
|
rgt_buffer = malloc(sizeof(char));
|
||||||
|
btm_buffer = malloc(sizeof(char));
|
||||||
|
|
||||||
file_current.file_type = 0;
|
file_current.file_type = 0;
|
||||||
file_current.file_size_bytes = 1;
|
file_current.file_size_bytes = 1;
|
||||||
|
8
window.c
8
window.c
@ -14,6 +14,8 @@ extern file *lft_content;
|
|||||||
extern file *rgt_content;
|
extern file *rgt_content;
|
||||||
extern char *top_content;
|
extern char *top_content;
|
||||||
extern char *rgt_buffer;
|
extern char *rgt_buffer;
|
||||||
|
extern char *btm_buffer;
|
||||||
|
|
||||||
|
|
||||||
extern unsigned long lft_file_count;
|
extern unsigned long lft_file_count;
|
||||||
extern unsigned long mid_file_count;
|
extern unsigned long mid_file_count;
|
||||||
@ -44,9 +46,15 @@ void window_top(WINDOW *win){
|
|||||||
}
|
}
|
||||||
void window_btm(WINDOW *win){
|
void window_btm(WINDOW *win){
|
||||||
werase(win);
|
werase(win);
|
||||||
|
unsigned long local_width;
|
||||||
|
unsigned long local_height;
|
||||||
|
getmaxyx(win, local_height, local_width);
|
||||||
if (pthread_mutex_trylock(&mutex_btm)) {
|
if (pthread_mutex_trylock(&mutex_btm)) {
|
||||||
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
pthread_mutex_unlock(&mutex_rgt);
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
} else {
|
} else {
|
||||||
|
mvwprintw(win, 0, 0, "%s", btm_buffer);
|
||||||
pthread_mutex_unlock(&mutex_btm);
|
pthread_mutex_unlock(&mutex_btm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user