From eaa29ac66488b68c95146129371f4a21126e4ded Mon Sep 17 00:00:00 2001 From: mananapr Date: Sun, 24 Mar 2019 11:28:33 +0530 Subject: [PATCH] Added selection count in statusbar --- cf.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++--------- config.h | 3 +++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/cf.c b/cf.c index 2d923a9..2b96e6a 100644 --- a/cf.c +++ b/cf.c @@ -44,6 +44,9 @@ int len_bookmarks=0; // To store number of scripts int len_scripts=0; +// To store number of selected files +int selectedFiles = 0; + // Counter variable int i = 0; @@ -146,6 +149,40 @@ int startx, starty, maxx, maxy; // HELPER FUNCTIONS // ////////////////////// + +/* + Checks if `path` is a file or directory +*/ +int is_regular_file(const char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + return S_ISREG(path_stat.st_mode); +} + + +/* + Sets value of `selectedFiles` +*/ +void setSelectionCount() +{ + FILE *fp = fopen(clipboard_path, "r"); + if( fp == NULL ) + { + selectedFiles = 0; + return; + } + char buf[PATH_MAX]; + int num = 0; + while(fgets(buf, PATH_MAX, (FILE*) fp)) + { + num++; + } + selectedFiles = num; + fclose(fp); +} + + /* Initializes the program Sets the relevant file paths @@ -331,6 +368,12 @@ void init(int argc, char* argv[]) printf("Incorrect Usage!\n"); exit(1); } + + // Set value of selectedFiles + if( SHOW_SELECTION_COUNT == 1 ) + { + setSelectionCount(); + } } @@ -349,17 +392,6 @@ void curses_init() } -/* - Checks if `path` is a file or directory -*/ -int is_regular_file(const char *path) -{ - struct stat path_stat; - stat(path, &path_stat); - return S_ISREG(path_stat.st_mode); -} - - /* Checks if a file exists or not */ @@ -1512,6 +1544,10 @@ void displayStatus() { wmove(status_win,1,0); wattron(status_win, COLOR_PAIR(2)); + if( SHOW_SELECTION_COUNT == 1 ) + { + wprintw(status_win,"[%d] ", selectedFiles); + } wprintw(status_win, "(%d/%d)", selection+1, len); wprintw(status_win, " %s", dir); wattroff(status_win, COLOR_PAIR(2)); @@ -2385,6 +2421,7 @@ int main(int argc, char* argv[]) free(temp); } renameFiles(); + selectedFiles = 0; break; // Write to clipboard @@ -2408,9 +2445,15 @@ int main(int argc, char* argv[]) exit(1); } if (checkClipboard(temp_dir) == 1) + { removeClipboard(replace(temp,"\n","//")); + selectedFiles--; + } else + { writeClipboard(replace(temp,"\n","//")); + selectedFiles++; + } free(temp); scrollDown(); break; @@ -2418,6 +2461,7 @@ int main(int argc, char* argv[]) // Empty Clipboard case KEY_EMPTYSEL: emptyClipboard(); + selectedFiles = 0; break; // Copy clipboard contents to present directory @@ -2428,6 +2472,7 @@ int main(int argc, char* argv[]) // Moves clipboard contents to present directory case KEY_MV: moveFiles(dir); + selectedFiles = 0; break; // Moves clipboard contents to trash @@ -2449,7 +2494,10 @@ int main(int argc, char* argv[]) displayAlert("Confirm (y/n): "); confirm = wgetch(status_win); if(confirm == 'y') + { removeFiles(); + selectedFiles = 0; + } else { displayAlert("ABORTED"); @@ -2546,6 +2594,7 @@ int main(int argc, char* argv[]) { int status; waitpid(pid, &status, 0); + setSelectionCount(); } refresh(); } diff --git a/config.h b/config.h index 099621b..2afba1b 100644 --- a/config.h +++ b/config.h @@ -12,6 +12,9 @@ // Set to 0 if you want to disable borders #define SHOW_BORDERS 1 +// Set to 0 if you don't want to see number of selected files in the statusbar +#define SHOW_SELECTION_COUNT 1 + // Program used to open non-text file (Eg: `xdg-open` or `thunar`) #define FILE_OPENER "xdg-open"