Fixed segfaults, added number of files in statusbar, improved file opening

pull/2/head
mananapr 5 years ago
parent 08026df90c
commit dc8e5f0284

@ -33,7 +33,7 @@ To install, simply move the generated executable to a directory that is in your
| <kbd>f</kbd> | Search using fzf |
| <kbd>F</kbd> | Search using fzf in the present directory |
| <kbd>S</kbd> | Open Shell in present directory |
| <kbd>space</kbd> | Add to selection list |
| <kbd>space</kbd> | Add/Remove to/from selection list |
| <kbd>tab</kbd> | View selection list |
| <kbd>e</kbd> | Edit selection list |
| <kbd>u</kbd> | Empty selection list |
@ -83,9 +83,9 @@ a similar UI.
- [x] Improve file previews
- [x] Add config file for easy user customizability
- [x] Add more options in config file
- [x] Fix random segfaults
- [x] Show more info in the statusbar
- [ ] Add bookmarks
- [ ] Add ability to run external scripts
- [ ] Refactor Code
- [ ] Fix random segfaults
- [ ] Show more info in the statusbar
- [ ] Add color support

226
cf.c

@ -110,8 +110,8 @@ int startx, starty, maxx, maxy;
//////////////////////
/*
Initializes the program
Sets the relevant file paths
Initializes the program
Sets the relevant file paths
*/
void init()
{
@ -140,7 +140,7 @@ void init()
/*
Initializes ncurses
Initializes ncurses
*/
void curses_init()
{
@ -176,7 +176,7 @@ int is_regular_file(const char *path)
/*
Checks if a file exists or not
Checks if a file exists or not
*/
int fileExists(char *file)
{
@ -193,16 +193,12 @@ int fileExists(char *file)
int compare (const void * a, const void * b )
{
// They store the full paths of the arguments
char temp_filepath1[250]="";
char temp_filepath2[250]="";
char temp_filepath1[250];
char temp_filepath2[250];
// Generate full paths
strcat(temp_filepath1,sort_dir);
strcat(temp_filepath1,"/");
strcat(temp_filepath1,*(char **)a);
strcat(temp_filepath2,sort_dir);
strcat(temp_filepath2,"/");
strcat(temp_filepath2,*(char **)b);
sprintf(temp_filepath1,"%s/%s", sort_dir, *(char **)a);
sprintf(temp_filepath2,"%s/%s", sort_dir, *(char **)b);
if(is_regular_file(temp_filepath1) == 0 && is_regular_file(temp_filepath2) == 1)
return -1;
@ -213,11 +209,41 @@ int compare (const void * a, const void * b )
}
/*
Gets file MIME
*/
void getMIME(char *filepath, char mime[10])
{
char cmd[250];
char buf[20];
FILE *fp;
sprintf(cmd, "xdg-mime query filetype %s", filepath);
if((fp = popen(cmd,"r")) == NULL)
{
exit(0);
}
while(fgets(buf,250,fp) != NULL){}
strtok(buf,"/");
strcpy(mime,buf);
}
/*
Opens a file using xdg-open
*/
void openFile(char *filepath)
{
char mime[10];
getMIME(filepath, mime);
if(strcmp(mime,"text") == 0)
{
char cmd[250];
sprintf(cmd,"vim %s",filepath);
endwin();
system(cmd);
return;
}
pid_t pid;
pid = fork();
if (pid == 0)
@ -258,8 +284,6 @@ int checkClipboard(char *filepath)
*/
void writeClipboard(char *filepath)
{
if( checkClipboard(filepath) == 1 )
return;
FILE *f = fopen(clipboard_path,"a+");
if (f == NULL)
{
@ -272,6 +296,18 @@ void writeClipboard(char *filepath)
}
/*
Removes entry from clipboard
*/
void removeClipboard(char *filepath)
{
char cmd[250];
filepath[strlen(filepath)-1] = '\0';
sprintf(cmd,"sed -i '\\|^%s|d' %s", filepath, clipboard_path);
system(cmd);
}
/*
Empties Clipboard
*/
@ -347,9 +383,11 @@ void getTextPreview(char *filepath, int maxy, int maxx)
// Don't Generate Preview if file size > 50MB
struct stat st;
stat(filepath, &st);
if(st.st_size > 50000000)
if(st.st_size > 10000000)
return;
FILE *fp = fopen(filepath,"r");
if(fp == NULL)
return;
char buf[250];
int t=0;
while(fgets(buf, 250, (FILE*) fp))
@ -462,15 +500,16 @@ int getNumberofFiles(char* directory)
}
while ((pDirent = readdir(pDir)) != NULL) {
// Skip . and ..
if( strcmp(pDirent->d_name,".") != 0 && strcmp(pDirent->d_name,"..") != 0 )
{
if( pDirent->d_name[0] == '.' )
if( hiddenFlag == 0 )
continue;
len++;
}
// Skip . and ..
if( strcmp(pDirent->d_name,".") != 0 && strcmp(pDirent->d_name,"..") != 0 )
{
if( pDirent->d_name[0] == '.' )
if( hiddenFlag == 0 )
continue;
len++;
}
}
closedir (pDir);
return len;
}
@ -478,7 +517,7 @@ int getNumberofFiles(char* directory)
/*
Stores all the file names in `char* directory` to `char *target[]`
*/
void getFiles(char* directory, char* target[])
int getFiles(char* directory, char* target[])
{
int i = 0;
DIR *pDir;
@ -486,7 +525,7 @@ void getFiles(char* directory, char* target[])
pDir = opendir (directory);
if (pDir == NULL) {
return;
return -1;
}
while ((pDirent = readdir(pDir)) != NULL) {
@ -501,6 +540,7 @@ void getFiles(char* directory, char* target[])
}
closedir (pDir);
return 1;
}
@ -652,7 +692,7 @@ void init_windows()
void displayStatus()
{
wmove(status_win,1,1);
wprintw(status_win, "%s@%s\t%s", getenv("USER"), getenv("HOSTNAME"), dir);
wprintw(status_win, "(%d/%d)\t%s@%s\t%s", selection+1, len, getenv("USER"), getenv("HOSTNAME"), dir);
wrefresh(status_win);
}
@ -743,16 +783,17 @@ int main(int argc, char* argv[])
// Array of all the files in the current directory
char* directories[len];
getFiles(dir, directories);
int status;
status = getFiles(dir, directories);
// Sort files by name
strcpy(sort_dir,dir);
qsort(directories, len, sizeof (char*), compare);
// In case the last file is selected and it get's removed or moved
if(selection > len-1)
{
selection = len-1;
}
// In case the last file is selected and it get's removed or moved
if(selection > len-1)
{
selection = len-1;
}
// Check if some flag are set to true and handle them accordingly
handleFlags(directories);
@ -772,9 +813,7 @@ int main(int argc, char* argv[])
int t = 0;
for( i=start; i<len; i++ )
{
strcpy(temp_dir,dir);
strcat(temp_dir,"/");
strcat(temp_dir,directories[i]);
sprintf(temp_dir,"%s/%s",dir,directories[i]);
if(i==selection)
wattron(current_win, A_STANDOUT);
else
@ -795,18 +834,16 @@ int main(int argc, char* argv[])
char prev_dir[250] = "";
// Get path of parent directory
strcat(prev_dir, dir);
sprintf(prev_dir,"%s",dir);
getParentPath(prev_dir);
// Get path of child directory
strcat(next_dir, dir);
strcat(next_dir, "/");
strcat(next_dir, directories[selection]);
sprintf(next_dir,"%s/%s", dir, directories[selection]);
// Stores number of files in the child directory
len_preview = getNumberofFiles(next_dir);
// Stores files in the child directory
char* next_directories[len_preview];
getFiles(next_dir, next_directories);
status = getFiles(next_dir, next_directories);
// Selection is a directory
strcpy(sort_dir,next_dir);
@ -816,19 +853,19 @@ int main(int argc, char* argv[])
// Selection is not a directory
if(len != 0)
{
if(len_preview != -1)
for( i=0; i<len_preview; i++ )
{
wmove(preview_win,i+1,2);
wprintw(preview_win, "%.*s\n", maxx/2 - 2, next_directories[i]);
}
// Get Preview of File
else
{
getPreview(next_dir,maxy,maxx/2+2);
}
}
if(len_preview != -1)
for( i=0; i<len_preview; i++ )
{
wmove(preview_win,i+1,2);
wprintw(preview_win, "%.*s\n", maxx/2 - 2, next_directories[i]);
}
// Get Preview of File
else
{
getPreview(next_dir,maxy,maxx/2+2);
}
}
// Disable STANDOUT attribute if enabled
wattroff(current_win, A_STANDOUT);
@ -1005,9 +1042,7 @@ int main(int argc, char* argv[])
case KEY_RENAME:
if( access( clipboard_path, F_OK ) == -1 )
{
strcpy(temp_dir,dir);
strcat(temp_dir,"/");
strcat(temp_dir,directories[selection]);
sprintf(temp_dir, "%s/%s",dir,directories[selection]);
writeClipboard(temp_dir);
}
renameFiles();
@ -1015,10 +1050,11 @@ int main(int argc, char* argv[])
// Write to clipboard
case KEY_SEL:
strcpy(temp_dir,dir);
strcat(temp_dir,"/");
strcat(temp_dir,directories[selection]);
writeClipboard(temp_dir);
sprintf(temp_dir, "%s/%s", dir, directories[selection]);
if (checkClipboard(temp_dir) == 1)
removeClipboard(temp_dir);
else
writeClipboard(temp_dir);
break;
// Empty Clipboard
@ -1038,41 +1074,41 @@ int main(int argc, char* argv[])
// Moves clipboard contents to trash
case KEY_REMOVEMENU:
if( fileExists(clipboard_path) == 1 )
{
keys_win = create_newwin(3, maxx, maxy-3, 0);
wprintw(keys_win,"Key\tCommand");
wprintw(keys_win,"\n%c\tMove to Trash", KEY_TRASH);
wprintw(keys_win,"\n%c\tDelete", KEY_DELETE);
wrefresh(keys_win);
secondKey = wgetch(current_win);
delwin(keys_win);
if( secondKey == KEY_TRASH )
moveFiles(trash_path);
else if( secondKey == KEY_DELETE )
{
wclear(status_win);
wprintw(status_win, "\nConfirm (y/n): ");
wrefresh(status_win);
confirm = wgetch(status_win);
if(confirm == 'y')
removeFiles();
else
{
wclear(status_win);
wprintw(status_win, "\nABORTED");
wrefresh(status_win);
sleep(1);
}
}
}
else
{
wclear(status_win);
wprintw(status_win,"\nSelect some files first!");
wrefresh(status_win);
sleep(1);
}
if( fileExists(clipboard_path) == 1 )
{
keys_win = create_newwin(3, maxx, maxy-3, 0);
wprintw(keys_win,"Key\tCommand");
wprintw(keys_win,"\n%c\tMove to Trash", KEY_TRASH);
wprintw(keys_win,"\n%c\tDelete", KEY_DELETE);
wrefresh(keys_win);
secondKey = wgetch(current_win);
delwin(keys_win);
if( secondKey == KEY_TRASH )
moveFiles(trash_path);
else if( secondKey == KEY_DELETE )
{
wclear(status_win);
wprintw(status_win, "\nConfirm (y/n): ");
wrefresh(status_win);
confirm = wgetch(status_win);
if(confirm == 'y')
removeFiles();
else
{
wclear(status_win);
wprintw(status_win, "\nABORTED");
wrefresh(status_win);
sleep(1);
}
}
}
else
{
wclear(status_win);
wprintw(status_win,"\nSelect some files first!");
wrefresh(status_win);
sleep(1);
}
break;
// See selection list

Loading…
Cancel
Save