Added config file and did some refactoring

pull/2/head
mananapr 6 years ago
parent 7102a38db1
commit dae578e122

208
cf.c

@ -21,6 +21,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <pwd.h> #include <pwd.h>
#include "config.h"
////////////////////// //////////////////////
@ -367,10 +368,10 @@ void getVidPreview(char *filepath, int maxy, int maxx)
{ {
char buf[250]; char buf[250];
sprintf(temp_dir,"mediainfo \"%s\" > ~/.cache/cfiles/preview",filepath); sprintf(temp_dir,"mediainfo \"%s\" > ~/.cache/cfiles/preview",filepath);
endwin();
system(temp_dir); system(temp_dir);
sprintf(temp_dir,"%s/preview",cache_path); sprintf(temp_dir,"%s/preview",cache_path);
sprintf(buf, "less %s", temp_dir); sprintf(buf, "less %s", temp_dir);
endwin();
system(buf); system(buf);
refresh(); refresh();
} }
@ -621,6 +622,93 @@ void moveFiles(char *present_dir)
} }
/*
Creates current_win, preview_win and status_win
*/
void init_windows()
{
current_win = create_newwin(maxy, maxx/2+3, 0, 0);
preview_win = create_newwin(maxy, maxx/2 -1, 0, maxx/2 + 1);
status_win = create_newwin(2, maxx, maxy, 0);
}
/*
Displays current status in the status bar
*/
void displayStatus()
{
wmove(status_win,1,1);
wprintw(status_win, "%s@%s\t%s", getenv("USER"), getenv("HOSTNAME"), dir);
wrefresh(status_win);
}
/*
*/
void refreshWindows()
{
box(current_win,0,0);
box(preview_win,0,0);
wrefresh(current_win);
wrefresh(preview_win);
}
/*
Checks if some flags are enabled and handles them accordingly
*/
void handleFlags(char** directories)
{
// Clear the preview_win
if(clearFlag == 1)
{
wclear(preview_win);
wrefresh(preview_win);
clearFlag = 0;
}
// Select the file in `last` and set `start` accordingly
if(searchFlag == 1)
{
searchFlag = 0;
last[strlen(last)-1] = '\0';
for(i=0; i<len; i++)
if(strcmp(directories[i],last) == 0)
{
selection = i;
break;
}
if(len > maxy)
{
if(selection > maxy-3)
start = selection - maxy + 3;
}
// Resets everything (Prevents unexpected behaviour after quitting fzf)
endwin();
refresh();
}
// Select the folder in `last` and set start accordingly
if(backFlag == 1)
{
backFlag = 0;
for(i=0; i<len; i++)
if(strcmp(directories[i],last) == 0)
{
selection = i;
break;
}
if(len > maxy)
{
if(selection > maxy-3)
start = selection - maxy + 3;
}
}
}
/////////////////// ///////////////////
// MAIN FUNCTION // // MAIN FUNCTION //
/////////////////// ///////////////////
@ -637,14 +725,6 @@ int main(int argc, char* argv[])
// Main Loop // Main Loop
do do
{ {
// Clear the preview_win
if(clearFlag == 1)
{
wclear(preview_win);
wrefresh(preview_win);
clearFlag = 0;
}
// Get number of files in the home directory // Get number of files in the home directory
len = getNumberofFiles(dir); len = getNumberofFiles(dir);
@ -660,60 +740,20 @@ int main(int argc, char* argv[])
{ {
selection = len-1; selection = len-1;
} }
// Select the file in `last` and set `start` accordingly
if(searchFlag == 1)
{
searchFlag = 0;
last[strlen(last)-1] = '\0';
for(i=0; i<len; i++)
if(strcmp(directories[i],last) == 0)
{
selection = i;
break;
}
if(len > maxy)
{
if(selection > maxy-3)
start = selection - maxy + 3;
}
// Resets everything (Prevents unexpected behaviour after quitting fzf) // Check if some flag are set to true and handle them accordingly
endwin(); handleFlags(directories);
refresh();
}
// Select the folder in `last` and set start accordingly
if(backFlag == 1)
{
backFlag = 0;
for(i=0; i<len; i++)
if(strcmp(directories[i],last) == 0)
{
selection = i;
break;
}
if(len > maxy)
{
if(selection > maxy-3)
start = selection - maxy + 3;
}
}
// Get Size of terminal // Get Size of terminal
getmaxyx(stdscr, maxy, maxx); getmaxyx(stdscr, maxy, maxx);
// Save last two rows for status_win // Save last two rows for status_win
maxy = maxy - 2; maxy = maxy - 2;
// Make the two windows side-by-side // Make the two windows side-by-side and make the status window
current_win = create_newwin(maxy, maxx/2+3, 0, 0); init_windows();
preview_win = create_newwin(maxy, maxx/2 -1, 0, maxx/2 + 1);
status_win = create_newwin(2, maxx, maxy, 0);
// Display Status // Display Status
wmove(status_win,1,1); displayStatus();
wprintw(status_win, "%s@%s\t%s", getenv("USER"), getenv("HOSTNAME"), dir);
wrefresh(status_win);
// Print all the elements and highlight the selection // Print all the elements and highlight the selection
int t = 0; int t = 0;
@ -776,13 +816,11 @@ int main(int argc, char* argv[])
getPreview(next_dir,maxy,maxx/2+2); getPreview(next_dir,maxy,maxx/2+2);
} }
} }
// Draw borders and refresh windows // Disable STANDOUT attribute if enabled
wattroff(current_win, A_STANDOUT); wattroff(current_win, A_STANDOUT);
box(current_win,0,0); // Draw borders and refresh windows
box(preview_win,0,0); refreshWindows();
wrefresh(current_win);
wrefresh(preview_win);
// For fzf file search // For fzf file search
char cmd[250]; char cmd[250];
@ -790,7 +828,9 @@ int main(int argc, char* argv[])
FILE *fp; FILE *fp;
// For two key keybindings // For two key keybindings
char secondKey; char secondKey;
// For taking user confirmation
char confirm;
// Keybindings // Keybindings
switch( ch = wgetch(current_win) ) { switch( ch = wgetch(current_win) ) {
@ -857,13 +897,13 @@ int main(int argc, char* argv[])
break; break;
// Goto start // Goto start
case 'g': case KEY_START:
selection = 0; selection = 0;
start = 0; start = 0;
break; break;
// Goto end // Goto end
case 'G': case KEY_GOEND:
selection = len - 1; selection = len - 1;
if(len > maxy - 2) if(len > maxy - 2)
start = len - maxy + 2; start = len - maxy + 2;
@ -872,12 +912,12 @@ int main(int argc, char* argv[])
break; break;
// Go to top of current view // Go to top of current view
case 'H': case KEY_TOP:
selection = start; selection = start;
break; break;
// Go to the bottom of current view // Go to the bottom of current view
case 'L': case KEY_BOTTOM:
if(len >= maxy) if(len >= maxy)
selection = start + maxy - 3; selection = start + maxy - 3;
else else
@ -885,7 +925,7 @@ int main(int argc, char* argv[])
break; break;
// Go to the middle of current view // Go to the middle of current view
case 'M': case KEY_MID:
if( len >= maxy ) if( len >= maxy )
selection = start + maxy/2; selection = start + maxy/2;
else else
@ -893,7 +933,7 @@ int main(int argc, char* argv[])
break; break;
// Search using fzf // Search using fzf
case 'F': case KEY_SEARCHALL:
sprintf(temp_dir,"cd %s && fzf",info->pw_dir); sprintf(temp_dir,"cd %s && fzf",info->pw_dir);
endwin(); endwin();
if((fp = popen(temp_dir,"r")) == NULL) if((fp = popen(temp_dir,"r")) == NULL)
@ -916,7 +956,7 @@ int main(int argc, char* argv[])
break; break;
// Search in the same directory // Search in the same directory
case 'f': case KEY_SEARCHDIR:
sprintf(cmd,"cd %s && ls | fzf",dir); sprintf(cmd,"cd %s && ls | fzf",dir);
endwin(); endwin();
if((fp = popen(cmd,"r")) == NULL) if((fp = popen(cmd,"r")) == NULL)
@ -936,7 +976,7 @@ int main(int argc, char* argv[])
break; break;
// Opens bash shell in present directory // Opens bash shell in present directory
case 'S': case KEY_SHELL:
sprintf(temp_dir,"cd \"%s\" && bash",dir); sprintf(temp_dir,"cd \"%s\" && bash",dir);
endwin(); endwin();
system(temp_dir); system(temp_dir);
@ -946,7 +986,7 @@ int main(int argc, char* argv[])
break; break;
// Bulk Rename // Bulk Rename
case 'a': case KEY_RENAME:
if( access( clipboard_path, F_OK ) == -1 ) if( access( clipboard_path, F_OK ) == -1 )
{ {
strcpy(temp_dir,dir); strcpy(temp_dir,dir);
@ -958,7 +998,7 @@ int main(int argc, char* argv[])
break; break;
// Write to clipboard // Write to clipboard
case ' ': case KEY_SEL:
strcpy(temp_dir,dir); strcpy(temp_dir,dir);
strcat(temp_dir,"/"); strcat(temp_dir,"/");
strcat(temp_dir,directories[selection]); strcat(temp_dir,directories[selection]);
@ -966,39 +1006,39 @@ int main(int argc, char* argv[])
break; break;
// Empty Clipboard // Empty Clipboard
case 'u': case KEY_EMPTYSEL:
emptyClipboard(); emptyClipboard();
break; break;
// Copy clipboard contents to present directory // Copy clipboard contents to present directory
case 'y': case KEY_YANK:
copyFiles(dir); copyFiles(dir);
break; break;
// Moves clipboard contents to present directory // Moves clipboard contents to present directory
case 'v': case KEY_MV:
moveFiles(dir); moveFiles(dir);
break; break;
// Moves clipboard contents to trash // Moves clipboard contents to trash
case 'd': case KEY_REMOVEMENU:
if( fileExists(clipboard_path) == 1 ) if( fileExists(clipboard_path) == 1 )
{ {
keys_win = create_newwin(3, maxx, maxy-3, 0); keys_win = create_newwin(3, maxx, maxy-3, 0);
wprintw(keys_win,"Key\tCommand"); wprintw(keys_win,"Key\tCommand");
wprintw(keys_win,"\nd\tMove to Trash"); wprintw(keys_win,"\n%c\tMove to Trash", KEY_TRASH);
wprintw(keys_win,"\nD\tDelete"); wprintw(keys_win,"\n%c\tDelete", KEY_DELETE);
wrefresh(keys_win); wrefresh(keys_win);
secondKey = wgetch(current_win); secondKey = wgetch(current_win);
delwin(keys_win); delwin(keys_win);
if( secondKey == 'd' ) if( secondKey == KEY_TRASH )
moveFiles(trash_path); moveFiles(trash_path);
else if( secondKey == 'D' ) else if( secondKey == KEY_DELETE )
{ {
wclear(status_win); wclear(status_win);
wprintw(status_win, "\nConfirm (y/n): "); wprintw(status_win, "\nConfirm (y/n): ");
wrefresh(status_win); wrefresh(status_win);
char confirm = wgetch(status_win); confirm = wgetch(status_win);
if(confirm == 'y') if(confirm == 'y')
removeFiles(); removeFiles();
else else
@ -1020,7 +1060,7 @@ int main(int argc, char* argv[])
break; break;
// See selection list // See selection list
case '\t': case KEY_VIEWSEL:
if( access( clipboard_path, F_OK ) != -1 ) if( access( clipboard_path, F_OK ) != -1 )
{ {
sprintf(temp_dir,"less %s",clipboard_path); sprintf(temp_dir,"less %s",clipboard_path);
@ -1040,7 +1080,7 @@ int main(int argc, char* argv[])
break; break;
// Edit selection list // Edit selection list
case 'e': case KEY_EDITSEL:
if( fileExists(clipboard_path) == 1 ) if( fileExists(clipboard_path) == 1 )
{ {
sprintf(temp_dir,"vim %s",clipboard_path); sprintf(temp_dir,"vim %s",clipboard_path);
@ -1060,12 +1100,12 @@ int main(int argc, char* argv[])
break; break;
// View Preview // View Preview
case 'i': case KEY_INFO:
getVidPreview(next_dir,maxy,maxx/2+2); getVidPreview(next_dir,maxy,maxx/2+2);
break; break;
// Clear Preview Window // Clear Preview Window
case 'r': case KEY_RELOAD:
clearFlag = 1; clearFlag = 1;
break; break;
} }

@ -0,0 +1,68 @@
#ifndef CONFIG
#define CONFIG
/*
Change your keybindings in this section
*/
// Go to the end of the current directory
#define KEY_GOEND 'G'
// Go to the start of the current directory
#define KEY_START 'g'
// Go to the top of the current view
#define KEY_TOP 'H'
// Go to the middle of the current view
#define KEY_MID 'M'
// Go to the bottom of the current view
#define KEY_BOTTOM 'L'
// Search all files with current directory as base
#define KEY_SEARCHALL 'F'
// Search all files within the current directory
#define KEY_SEARCHDIR 'f'
// Add file to selection list
#define KEY_SEL ' '
// View all the selected files
#define KEY_VIEWSEL '\t'
// Edit the selection list
#define KEY_EDITSEL 'e'
// Empty the selection list
#define KEY_EMPTYSEL 'u'
// Copy files in selection list to the current directory
#define KEY_YANK 'y'
// Move files in selection list to the current directory
#define KEY_MV 'v'
// Rename files in selection list
#define KEY_RENAME 'a'
// For getting an option to either move the file to trash or delete it
#define KEY_REMOVEMENU 'd'
// For moving the file to trash after pressing KEY_REMOVEMENU
#define KEY_TRASH 'D'
// For removing the file after pressing KEY_REMOVEMENU
#define KEY_DELETE 'd'
// View file info
#define KEY_INFO 'i'
// Open Shell
#define KEY_SHELL 'S'
// Reload
#define KEY_RELOAD 'r'
#endif
Loading…
Cancel
Save