Added option to customize colors

pull/19/head
mananapr 5 years ago
parent 8906b222e7
commit 0261c68f04

272
cf.c

@ -1,11 +1,11 @@
/* /*
__ _ _ __ _ _
___ / _(_) | ___ ___ ___ / _(_) | ___ ___
/ __| |_| | |/ _ \/ __| / __| |_| | |/ _ \/ __|
| (__| _| | | __/\__ \ | (__| _| | | __/\__ \
\___|_| |_|_|\___||___/ \___|_| |_|_|\___||___/
*/ */
///////////// /////////////
@ -59,10 +59,10 @@ char *pch;
struct passwd *info; struct passwd *info;
/* /*
Base directory to be used for sorting Base directory to be used for sorting
`dir` for current_win `dir` for current_win
`next_dir` for preview_win `next_dir` for preview_win
*/ */
char sort_dir[250]; char sort_dir[250];
// Stores the path for the cache directory // Stores the path for the cache directory
@ -125,9 +125,9 @@ int startx, starty, maxx, maxy;
////////////////////// //////////////////////
/* /*
Initializes the program Initializes the program
Sets the relevant file paths Sets the relevant file paths
*/ */
void init() void init()
{ {
// Get UID of user // Get UID of user
@ -163,24 +163,24 @@ void init()
/* /*
Initializes ncurses Initializes ncurses
*/ */
void curses_init() void curses_init()
{ {
initscr(); initscr();
noecho(); noecho();
curs_set(0); curs_set(0);
start_color(); start_color();
init_pair(1, 2, 0); init_pair(1, DIR_COLOR, 0);
init_pair(2, 1, 0); init_pair(2, STATUS_FILECOUNT_COLOR, 0);
init_pair(3, 6, 0); init_pair(3, STATUS_SELECTED_COLOR, 0);
init_pair(4, 0, 0); init_pair(4, 0, 0);
} }
/* /*
Checks if `path` is a file or directory Checks if `path` is a file or directory
*/ */
int is_regular_file(const char *path) int is_regular_file(const char *path)
{ {
struct stat path_stat; struct stat path_stat;
@ -190,20 +190,20 @@ 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) int fileExists(char *file)
{ {
if( access( file, F_OK ) != -1 ) if( access( file, F_OK ) != -1 )
return 1; return 1;
else else
return 0; return 0;
} }
/* /*
Gets the last token from temp_dir by using `tokenizer` as a delimeter Gets the last token from temp_dir by using `tokenizer` as a delimeter
*/ */
void getLastToken(char *tokenizer) void getLastToken(char *tokenizer)
{ {
pch = strtok(temp_dir, tokenizer); pch = strtok(temp_dir, tokenizer);
@ -216,8 +216,8 @@ void getLastToken(char *tokenizer)
/* /*
Get number of bookmarks Get number of bookmarks
*/ */
int getNumberOfBookmarks() int getNumberOfBookmarks()
{ {
FILE *fp = fopen(bookmarks_path, "r"); FILE *fp = fopen(bookmarks_path, "r");
@ -237,8 +237,8 @@ int getNumberOfBookmarks()
/* /*
Displays the bookmarks in `keys_win` Displays the bookmarks in `keys_win`
*/ */
void displayBookmarks() void displayBookmarks()
{ {
FILE *fp = fopen(bookmarks_path, "r"); FILE *fp = fopen(bookmarks_path, "r");
@ -256,8 +256,8 @@ void displayBookmarks()
/* /*
Opens the bookmark denoted by `secondKey` Opens the bookmark denoted by `secondKey`
*/ */
void openBookmarkDir(char secondKey) void openBookmarkDir(char secondKey)
{ {
FILE *fp = fopen(bookmarks_path, "r"); FILE *fp = fopen(bookmarks_path, "r");
@ -280,8 +280,8 @@ void openBookmarkDir(char secondKey)
/* /*
Checks if bookmark denoted with `bookmark` exists Checks if bookmark denoted with `bookmark` exists
*/ */
int bookmarkExists(char bookmark) int bookmarkExists(char bookmark)
{ {
FILE *fp = fopen(bookmarks_path, "r"); FILE *fp = fopen(bookmarks_path, "r");
@ -304,8 +304,8 @@ int bookmarkExists(char bookmark)
/* /*
Adds new bookmark Adds new bookmark
*/ */
void addBookmark(char bookmark, char *path) void addBookmark(char bookmark, char *path)
{ {
FILE *fp = fopen(bookmarks_path, "a+"); FILE *fp = fopen(bookmarks_path, "a+");
@ -315,8 +315,8 @@ void addBookmark(char bookmark, char *path)
/* /*
Creates a new window with dimensions `height` and `width` starting at `starty` and `startx` Creates a new window with dimensions `height` and `width` starting at `starty` and `startx`
*/ */
WINDOW *create_newwin(int height, int width, int starty, int startx) WINDOW *create_newwin(int height, int width, int starty, int startx)
{ {
WINDOW *local_win; WINDOW *local_win;
@ -326,8 +326,8 @@ WINDOW *create_newwin(int height, int width, int starty, int startx)
/* /*
For qsort For qsort
*/ */
int compare (const void * a, const void * b ) int compare (const void * a, const void * b )
{ {
// They store the full paths of the arguments // They store the full paths of the arguments
@ -348,8 +348,8 @@ int compare (const void * a, const void * b )
/* /*
Gets file MIME Gets file MIME
*/ */
void getMIME(char *filepath, char mime[10]) void getMIME(char *filepath, char mime[10])
{ {
char cmd[250]; char cmd[250];
@ -368,8 +368,8 @@ void getMIME(char *filepath, char mime[10])
/* /*
Opens a file using xdg-open Opens a file using xdg-open
*/ */
void openFile(char *filepath) void openFile(char *filepath)
{ {
char mime[10]; char mime[10];
@ -393,8 +393,8 @@ void openFile(char *filepath)
/* /*
Checks if path is in clipboard Checks if path is in clipboard
*/ */
int checkClipboard(char *filepath) int checkClipboard(char *filepath)
{ {
FILE *f = fopen(clipboard_path, "r"); FILE *f = fopen(clipboard_path, "r");
@ -418,8 +418,8 @@ int checkClipboard(char *filepath)
/* /*
Writes to clipboard Writes to clipboard
*/ */
void writeClipboard(char *filepath) void writeClipboard(char *filepath)
{ {
FILE *f = fopen(clipboard_path,"a+"); FILE *f = fopen(clipboard_path,"a+");
@ -435,8 +435,8 @@ void writeClipboard(char *filepath)
/* /*
Removes entry from clipboard Removes entry from clipboard
*/ */
void removeClipboard(char *filepath) void removeClipboard(char *filepath)
{ {
char cmd[250]; char cmd[250];
@ -447,8 +447,8 @@ void removeClipboard(char *filepath)
/* /*
Empties Clipboard Empties Clipboard
*/ */
void emptyClipboard() void emptyClipboard()
{ {
if( remove(clipboard_path) == -1) if( remove(clipboard_path) == -1)
@ -459,8 +459,8 @@ void emptyClipboard()
/* /*
Gets previews of images Gets previews of images
*/ */
void getImgPreview(char *filepath, int maxy, int maxx) void getImgPreview(char *filepath, int maxy, int maxx)
{ {
pid_t pid; pid_t pid;
@ -489,7 +489,7 @@ void getImgPreview(char *filepath, int maxy, int maxx)
// Get Dimensions from `buf` and store them `width` and `height` // Get Dimensions from `buf` and store them `width` and `height`
sscanf(buf,"%d %d", &width, &height); sscanf(buf,"%d %d", &width, &height);
// Set appropriate maxx and maxy so that image displays within the preview_win // Set appropriate maxx and maxy so that image displays within the preview_win
maxx = maxx * 5; maxx = maxx * 5;
maxy = maxy * 5; maxy = maxy * 5;
@ -514,8 +514,8 @@ void getImgPreview(char *filepath, int maxy, int maxx)
/* /*
Gets previews of text in files Gets previews of text in files
*/ */
void getTextPreview(char *filepath, int maxy, int maxx) void getTextPreview(char *filepath, int maxy, int maxx)
{ {
// Don't Generate Preview if file size > 50MB // Don't Generate Preview if file size > 50MB
@ -541,8 +541,8 @@ void getTextPreview(char *filepath, int maxy, int maxx)
/* /*
Gets previews of video files Gets previews of video files
*/ */
void getVidPreview(char *filepath, int maxy, int maxx) void getVidPreview(char *filepath, int maxy, int maxx)
{ {
char buf[250]; char buf[250];
@ -557,8 +557,8 @@ void getVidPreview(char *filepath, int maxy, int maxx)
/* /*
Gets previews of archives Gets previews of archives
*/ */
void getArchivePreview(char *filepath, int maxy, int maxx) void getArchivePreview(char *filepath, int maxy, int maxx)
{ {
char buf[250]; char buf[250];
@ -570,8 +570,8 @@ void getArchivePreview(char *filepath, int maxy, int maxx)
/* /*
Gets previews of video files (Dummy) Gets previews of video files (Dummy)
*/ */
void getDummyVidPreview(char *filepath, int maxy, int maxx) void getDummyVidPreview(char *filepath, int maxy, int maxx)
{ {
wmove(preview_win,1,2); wmove(preview_win,1,2);
@ -581,8 +581,8 @@ void getDummyVidPreview(char *filepath, int maxy, int maxx)
/* /*
Sets `temp_dir` to filepath and then stores the extension in `last` Sets `temp_dir` to filepath and then stores the extension in `last`
*/ */
void getFileType(char *filepath) void getFileType(char *filepath)
{ {
strcpy(temp_dir, filepath); strcpy(temp_dir, filepath);
@ -591,8 +591,8 @@ void getFileType(char *filepath)
/* /*
Checks `last` for extension and then calls the appropriate preview function Checks `last` for extension and then calls the appropriate preview function
*/ */
void getPreview(char *filepath, int maxy, int maxx) void getPreview(char *filepath, int maxy, int maxx)
{ {
getFileType(filepath); getFileType(filepath);
@ -611,8 +611,8 @@ void getPreview(char *filepath, int maxy, int maxx)
/* /*
Gets path of parent directory Gets path of parent directory
*/ */
void getParentPath(char *path) void getParentPath(char *path)
{ {
char *p; char *p;
@ -628,8 +628,8 @@ void getParentPath(char *path)
/* /*
Returns number of files in `char* directory` Returns number of files in `char* directory`
*/ */
int getNumberofFiles(char* directory) int getNumberofFiles(char* directory)
{ {
int len=0; int len=0;
@ -657,8 +657,8 @@ int getNumberofFiles(char* directory)
/* /*
Stores all the file names in `char* directory` to `char *target[]` Stores all the file names in `char* directory` to `char *target[]`
*/ */
int getFiles(char* directory, char* target[]) int getFiles(char* directory, char* target[])
{ {
int i = 0; int i = 0;
@ -674,10 +674,10 @@ int getFiles(char* directory, char* target[])
// Skip . and .. // Skip . and ..
if( strcmp(pDirent->d_name,".") != 0 && strcmp(pDirent->d_name,"..") != 0 ) if( strcmp(pDirent->d_name,".") != 0 && strcmp(pDirent->d_name,"..") != 0 )
{ {
if( pDirent->d_name[0] == '.' ) if( pDirent->d_name[0] == '.' )
if( hiddenFlag == 0 ) if( hiddenFlag == 0 )
continue; continue;
target[i++] = strdup(pDirent->d_name); target[i++] = strdup(pDirent->d_name);
} }
} }
@ -687,8 +687,8 @@ int getFiles(char* directory, char* target[])
/* /*
Copy files in clipboard to `present_dir` Copy files in clipboard to `present_dir`
*/ */
void copyFiles(char *present_dir) void copyFiles(char *present_dir)
{ {
FILE *f = fopen(clipboard_path, "r"); FILE *f = fopen(clipboard_path, "r");
@ -711,8 +711,8 @@ void copyFiles(char *present_dir)
} }
/* /*
Removes files in clipboard Removes files in clipboard
*/ */
void removeFiles() void removeFiles()
{ {
FILE *f = fopen(clipboard_path, "r"); FILE *f = fopen(clipboard_path, "r");
@ -735,8 +735,8 @@ void removeFiles()
/* /*
Rename files in clipboard Rename files in clipboard
*/ */
void renameFiles() void renameFiles()
{ {
// For opening clipboard and temp_clipboard // For opening clipboard and temp_clipboard
@ -793,8 +793,8 @@ void renameFiles()
/* /*
Move files in clipboard to `present_dir` Move files in clipboard to `present_dir`
*/ */
void moveFiles(char *present_dir) void moveFiles(char *present_dir)
{ {
FILE *f = fopen(clipboard_path, "r"); FILE *f = fopen(clipboard_path, "r");
@ -818,8 +818,8 @@ void moveFiles(char *present_dir)
/* /*
Creates current_win, preview_win and status_win Creates current_win, preview_win and status_win
*/ */
void init_windows() void init_windows()
{ {
current_win = create_newwin(maxy, maxx/2+3, 0, 0); current_win = create_newwin(maxy, maxx/2+3, 0, 0);
@ -829,8 +829,8 @@ void init_windows()
/* /*
Displays current status in the status bar Displays current status in the status bar
*/ */
void displayStatus() void displayStatus()
{ {
wmove(status_win,1,1); wmove(status_win,1,1);
@ -846,8 +846,8 @@ void displayStatus()
/* /*
Displays message on status bar Displays message on status bar
*/ */
void displayAlert(char *message) void displayAlert(char *message)
{ {
wclear(status_win); wclear(status_win);
@ -859,7 +859,7 @@ void displayAlert(char *message)
/* /*
*/ */
void refreshWindows() void refreshWindows()
{ {
if(SHOW_BORDERS == 1) if(SHOW_BORDERS == 1)
@ -874,7 +874,7 @@ void refreshWindows()
/* /*
Checks if some flags are enabled and handles them accordingly Checks if some flags are enabled and handles them accordingly
*/ */
void handleFlags(char** directories) void handleFlags(char** directories)
{ {
// Clear the preview_win // Clear the preview_win
@ -932,7 +932,7 @@ void handleFlags(char** directories)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Initialization // Initialization
init(); init();
curses_init(); curses_init();
@ -1078,35 +1078,35 @@ int main(int argc, char* argv[])
selection = ( selection < 0 ) ? 0 : selection; selection = ( selection < 0 ) ? 0 : selection;
// Scrolling // Scrolling
if(len >= maxy) if(len >= maxy)
if(selection <= start + maxy/2) if(selection <= start + maxy/2)
{
if(start == 0)
wclear(current_win);
else
{ {
start--; if(start == 0)
wclear(current_win); wclear(current_win);
else
{
start--;
wclear(current_win);
}
} }
}
break; break;
// Go down // Go down
case 'j': case 'j':
selection++; selection++;
selection = ( selection > len-1 ) ? len-1 : selection; selection = ( selection > len-1 ) ? len-1 : selection;
// Scrolling // Scrolling
if(len >= maxy) if(len >= maxy)
if(selection - 1 > maxy/2) if(selection - 1 > maxy/2)
{
if(start + maxy - 2 != len)
{ {
start++; if(start + maxy - 2 != len)
wclear(current_win); {
} start++;
} wclear(current_win);
}
}
break; break;
// Go to child directory or open file // Go to child directory or open file
case 'l': case 'l':
if(len_preview != -1) if(len_preview != -1)
{ {
@ -1122,7 +1122,7 @@ int main(int argc, char* argv[])
} }
break; break;
// Go up a directory // Go up a directory
case 'h': case 'h':
// Copy present directory to temp_dir to work with strtok() // Copy present directory to temp_dir to work with strtok()
strcpy(temp_dir, dir); strcpy(temp_dir, dir);
@ -1134,13 +1134,13 @@ int main(int argc, char* argv[])
getLastToken("/"); getLastToken("/");
break; break;
// Goto start // Goto start
case KEY_START: case KEY_START:
selection = 0; selection = 0;
start = 0; start = 0;
break; break;
// Goto end // Goto end
case KEY_GOEND: case KEY_GOEND:
selection = len - 1; selection = len - 1;
if(len > maxy - 2) if(len > maxy - 2)
@ -1149,12 +1149,12 @@ int main(int argc, char* argv[])
start = 0; start = 0;
break; break;
// Go to top of current view // Go to top of current view
case KEY_TOP: case KEY_TOP:
selection = start; selection = start;
break; break;
// Go to the bottom of current view // Go to the bottom of current view
case KEY_BOTTOM: case KEY_BOTTOM:
if(len >= maxy) if(len >= maxy)
selection = start + maxy - 3; selection = start + maxy - 3;
@ -1162,7 +1162,7 @@ int main(int argc, char* argv[])
selection = len - 1; selection = len - 1;
break; break;
// Go to the middle of current view // Go to the middle of current view
case KEY_MID: case KEY_MID:
if( len >= maxy ) if( len >= maxy )
selection = start + maxy/2; selection = start + maxy/2;
@ -1170,7 +1170,7 @@ int main(int argc, char* argv[])
selection = (len / 2) - 1; selection = (len / 2) - 1;
break; break;
// Search using fzf // Search using fzf
case KEY_SEARCHALL: case KEY_SEARCHALL:
sprintf(temp_dir,"cd %s && fzf",info->pw_dir); sprintf(temp_dir,"cd %s && fzf",info->pw_dir);
endwin(); endwin();
@ -1193,7 +1193,7 @@ int main(int argc, char* argv[])
searchFlag = 1; searchFlag = 1;
break; break;
// Search in the same directory // Search in the same directory
case KEY_SEARCHDIR: case KEY_SEARCHDIR:
if( hiddenFlag == 1 ) if( hiddenFlag == 1 )
sprintf(cmd,"cd %s && ls -a | fzf",dir); sprintf(cmd,"cd %s && ls -a | fzf",dir);
@ -1216,7 +1216,7 @@ int main(int argc, char* argv[])
searchFlag = 1; searchFlag = 1;
break; break;
// Opens bash shell in present directory // Opens bash shell in present directory
case KEY_SHELL: case KEY_SHELL:
sprintf(temp_dir,"cd \"%s\" && bash",dir); sprintf(temp_dir,"cd \"%s\" && bash",dir);
endwin(); endwin();
@ -1226,7 +1226,7 @@ int main(int argc, char* argv[])
refresh(); refresh();
break; break;
// Bulk Rename // Bulk Rename
case KEY_RENAME: case KEY_RENAME:
if( access( clipboard_path, F_OK ) == -1 ) if( access( clipboard_path, F_OK ) == -1 )
{ {
@ -1236,7 +1236,7 @@ int main(int argc, char* argv[])
renameFiles(); renameFiles();
break; break;
// Write to clipboard // Write to clipboard
case KEY_SEL: case KEY_SEL:
sprintf(temp_dir, "%s/%s", dir, directories[selection]); sprintf(temp_dir, "%s/%s", dir, directories[selection]);
if (checkClipboard(temp_dir) == 1) if (checkClipboard(temp_dir) == 1)
@ -1245,22 +1245,22 @@ int main(int argc, char* argv[])
writeClipboard(temp_dir); writeClipboard(temp_dir);
break; break;
// Empty Clipboard // Empty Clipboard
case KEY_EMPTYSEL: case KEY_EMPTYSEL:
emptyClipboard(); emptyClipboard();
break; break;
// Copy clipboard contents to present directory // Copy clipboard contents to present directory
case KEY_YANK: case KEY_YANK:
copyFiles(dir); copyFiles(dir);
break; break;
// Moves clipboard contents to present directory // Moves clipboard contents to present directory
case KEY_MV: case KEY_MV:
moveFiles(dir); moveFiles(dir);
break; break;
// Moves clipboard contents to trash // Moves clipboard contents to trash
case KEY_REMOVEMENU: case KEY_REMOVEMENU:
if( fileExists(clipboard_path) == 1 ) if( fileExists(clipboard_path) == 1 )
{ {
@ -1293,7 +1293,7 @@ int main(int argc, char* argv[])
} }
break; break;
// Go to bookmark // Go to bookmark
case KEY_BOOKMARK: case KEY_BOOKMARK:
len_bookmarks = getNumberOfBookmarks(); len_bookmarks = getNumberOfBookmarks();
if( len_bookmarks == -1 ) if( len_bookmarks == -1 )
@ -1311,7 +1311,7 @@ int main(int argc, char* argv[])
} }
break; break;
// Add Bookmark // Add Bookmark
case KEY_ADDBOOKMARK: case KEY_ADDBOOKMARK:
displayAlert("Enter Bookmark Key"); displayAlert("Enter Bookmark Key");
secondKey = wgetch(status_win); secondKey = wgetch(status_win);
@ -1326,7 +1326,7 @@ int main(int argc, char* argv[])
} }
break; break;
// See selection list // See selection list
case KEY_VIEWSEL: case KEY_VIEWSEL:
if( access( clipboard_path, F_OK ) != -1 ) if( access( clipboard_path, F_OK ) != -1 )
{ {
@ -1342,7 +1342,7 @@ int main(int argc, char* argv[])
} }
break; break;
// Edit selection list // Edit selection list
case KEY_EDITSEL: case KEY_EDITSEL:
if( fileExists(clipboard_path) == 1 ) if( fileExists(clipboard_path) == 1 )
{ {
@ -1358,12 +1358,12 @@ int main(int argc, char* argv[])
} }
break; break;
// View Preview // View Preview
case KEY_INFO: case KEY_INFO:
getVidPreview(next_dir,maxy,maxx/2+2); getVidPreview(next_dir,maxy,maxx/2+2);
break; break;
// Enable/Disable hidden files // Enable/Disable hidden files
case KEY_TOGGLEHIDE: case KEY_TOGGLEHIDE:
if( hiddenFlag == 1 ) if( hiddenFlag == 1 )
hiddenFlag = 0; hiddenFlag = 0;
@ -1373,7 +1373,7 @@ int main(int argc, char* argv[])
selection = 0; selection = 0;
break; break;
// Run External Script // Run External Script
case KEY_SCRIPT: case KEY_SCRIPT:
len_scripts = getNumberofFiles(scripts_path); len_scripts = getNumberofFiles(scripts_path);
if(len_scripts == 0) if(len_scripts == 0)
@ -1412,7 +1412,7 @@ int main(int argc, char* argv[])
} }
break; break;
// Clear Preview Window // Clear Preview Window
case KEY_RELOAD: case KEY_RELOAD:
clearFlag = 1; clearFlag = 1;
break; break;

@ -11,6 +11,17 @@
// Set to 0 if you want to disable borders // Set to 0 if you want to disable borders
#define SHOW_BORDERS 1 #define SHOW_BORDERS 1
/*
Color Settings
*/
// Shell Color Number to use for directories
#define DIR_COLOR 2
// Shell Color Number to use for file count which is displayed in the statusbar
#define STATUS_FILECOUNT_COLOR 1
// Shell Color Number to use for selected file which is displayed in the statusbar
#define STATUS_SELECTED_COLOR 6
/* /*
Change your keybindings in this section Change your keybindings in this section

Loading…
Cancel
Save