cfiles now remembers position of parent directory,selects file from fzf. updated todo list

pull/2/head
mananapr 6 years ago
parent 5c3f6ef8c4
commit b30495ca2b

@ -31,6 +31,6 @@ a similar UI.
- [x] Add fuzzy file search using fzf - [x] Add fuzzy file search using fzf
- [x] Find a way to redraw windows after displaying image previews or running fzf - [x] Find a way to redraw windows after displaying image previews or running fzf
- [ ] Find a way to remove cursor after running fzf - [ ] Find a way to remove cursor after running fzf
- [ ] Select the file directly from fzf - [x] Select the file directly from fzf
- [x] Supress output from xdg-open - [x] Supress output from xdg-open
- [ ] Find a way to remember selection position of parent directory - [x] Find a way to remember selection position of parent directory

82
cf.c

@ -188,19 +188,34 @@ int main(int argc, char* argv[])
// Index of currently selected item in `char* directories` // Index of currently selected item in `char* directories`
int selection = 0; int selection = 0;
// For Storing user keypress
char ch; char ch;
// Index to start printing from `directories` array
int start = 0; int start = 0;
// Flag to clear preview_win
int clearFlag = 0; int clearFlag = 0;
// Flag is set to 1 when returning from `fzf`
int searchFlag = 0;
// Flag is set to 1 when user goes up a directory
int backFlag = 0;
// Stores the last token in the path. For eg, it will store 'a' is path is /b/a
char *last;
// Main Loop // Main Loop
do do
{ {
// char array to work with strtok()
char temp_dir[250];
// Clear the preview_win
if(clearFlag == 1) if(clearFlag == 1)
{ {
wclear(preview_win); wclear(preview_win);
wrefresh(preview_win); wrefresh(preview_win);
clearFlag = 0; clearFlag = 0;
} }
// Get number of files in the home directory // Get number of files in the home directory
len = getNumberofFiles(dir); len = getNumberofFiles(dir);
@ -210,6 +225,39 @@ int main(int argc, char* argv[])
// Sort files by name // Sort files by name
qsort (directories, len, sizeof (char*), compare); qsort (directories, len, sizeof (char*), compare);
// 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)
{
start = selection - maxy + 3;
}
}
// 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)
{
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
@ -291,6 +339,7 @@ int main(int argc, char* argv[])
// Keybindings // Keybindings
switch( ch = wgetch(current_win) ) { switch( ch = wgetch(current_win) ) {
//Go up
case 'k': case 'k':
selection--; selection--;
selection = ( selection < 0 ) ? 0 : selection; selection = ( selection < 0 ) ? 0 : selection;
@ -307,7 +356,8 @@ int main(int argc, char* argv[])
} }
} }
break; break;
// Go down
case 'j': case 'j':
selection++; selection++;
selection = ( selection > len-1 ) ? len-1 : selection; selection = ( selection > len-1 ) ? len-1 : selection;
@ -322,7 +372,8 @@ int main(int argc, char* argv[])
} }
} }
break; break;
// Go to child directory or open file
case 'l': case 'l':
if(len_preview != -1) if(len_preview != -1)
{ {
@ -338,17 +389,31 @@ int main(int argc, char* argv[])
} }
break; break;
// Go up a directory
case 'h': case 'h':
// Copy present directory to temp_dir to work with strtok()
strcpy(temp_dir, dir);
strcpy(dir, prev_dir); strcpy(dir, prev_dir);
selection = 0; selection = 0;
start = 0; start = 0;
backFlag = 1;
// Get the last token in `temp_dir` and store it in `last`
char *pch;
pch = strtok(temp_dir,"/");
while (pch != NULL)
{
last = pch;
pch = strtok(NULL,"/");
}
break; break;
// Goto start
case 'g': case 'g':
selection = 0; selection = 0;
start = 0; start = 0;
break; break;
// Goto end
case 'G': case 'G':
selection = len - 1; selection = len - 1;
if(len > maxy - 2) if(len > maxy - 2)
@ -357,6 +422,7 @@ int main(int argc, char* argv[])
start = 0; start = 0;
break; break;
// Search using fzf
case 'F': case 'F':
sprintf(cmd,"cd %s && fzf",info->pw_dir); sprintf(cmd,"cd %s && fzf",info->pw_dir);
if((fp = popen(cmd,"r")) == NULL) if((fp = popen(cmd,"r")) == NULL)
@ -366,15 +432,27 @@ int main(int argc, char* argv[])
while(fgets(buf,250,fp) != NULL){} while(fgets(buf,250,fp) != NULL){}
char path[250]; char path[250];
sprintf(path, "%s/%s",info->pw_dir,buf); sprintf(path, "%s/%s",info->pw_dir,buf);
// Copy `path` into `temp_dir` to work with strtok.
//Then store the last token in `temp_dir` and store it in `last`.
strcpy(temp_dir,path);
pch = strtok(temp_dir,"/");
while (pch != NULL)
{
last = pch;
pch = strtok(NULL,"/");
}
getParentPath(path); getParentPath(path);
strcpy(dir,path); strcpy(dir,path);
selection = 0; selection = 0;
start = 0; start = 0;
clearFlag = 1; clearFlag = 1;
searchFlag = 1;
break; break;
// Clear Preview Window
case 'r': case 'r':
clearFlag = 1; clearFlag = 1;
break;
} }

Loading…
Cancel
Save