From 362554d2aca3158ba05579b42068813a57af0c44 Mon Sep 17 00:00:00 2001 From: yorhel Date: Thu, 26 Jul 2007 12:56:24 +0000 Subject: [PATCH] Added 'r' key to refresh the current directory git-svn-id: svn://blicky.net/ncdu/trunk@11 ce56bc8d-f834-0410-b703-f827bd498a76 --- src/browser.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/calc.c | 16 +++++++++++++++- src/main.c | 1 + src/util.c | 1 + 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/browser.c b/src/browser.c index f3bded6..5dbccc3 100644 --- a/src/browser.c +++ b/src/browser.c @@ -283,7 +283,8 @@ struct dir * selected(void) { void showBrowser(void) { int ch, change; - struct dir *n; + char tmp[PATH_MAX]; + struct dir *n, *t, *d; bcur = dat->sub; bgraph = 1; @@ -355,6 +356,54 @@ void showBrowser(void) { } break; + /* refresh */ + case 'r': + if((n = showCalc(getpath(bcur, tmp))) != NULL) { + /* free current items */ + t = bcur; + bcur = bcur->parent; + while(t->prev != NULL) + t = t->prev; + d = t; + while(d != NULL) { + t = d; + d = t->next; + freedir(t); + } + + /* update parent dir */ + bcur->sub = n->sub; + bcur->files = n->files; + bcur->dirs = n->dirs; + bcur->size = n->size; + for(t = bcur->sub; t != NULL; t = t->next) + t->parent = bcur; + + /* update sizes of parent dirs */ + for(t = bcur; (t = t->parent) != NULL; ) { + t->size += bcur->size; + t->files += bcur->files; + t->dirs += bcur->dirs+1; + } + + /* add reference to parent dir */ + if(bcur->parent) { + t = calloc(sizeof(struct dir), 1); + t->name = malloc(3); + t->flags |= FF_PAR; + strcpy(t->name, ".."); + t->parent = bcur; + t->next = bcur->sub; + t->next->prev = t; + bcur->sub = t; + } + + bcur = bcur->sub; + free(n->name); + free(n); + } + break; + /* and other stuff */ case KEY_RESIZE: ncresize(); diff --git a/src/calc.c b/src/calc.c index 9143ae5..1430c48 100644 --- a/src/calc.c +++ b/src/calc.c @@ -142,7 +142,7 @@ static void drawProgress(char *cdir) { prg = newwin(10, 60, winrows/2-3, wincols/2-30); box(prg, 0, 0); wattron(prg, A_BOLD); - mvwaddstr(prg, 0, 4, "Calculating..."); + mvwaddstr(prg, 0, 4, dat == NULL ? "Calculating..." : "Recalculating..."); wattroff(prg, A_BOLD); mvwprintw(prg, 2, 2, "Total files: %-8d dirs: %-8d size: %s", @@ -209,6 +209,8 @@ int updateProgress(char *path) { return(0); if(ch == KEY_RESIZE) { ncresize(); + if(dat != NULL) + drawBrowser(0); drawProgress(path); } } @@ -368,6 +370,7 @@ int calcDir(struct dir *dest, char *path) { struct dir *showCalc(char *path) { char tmp[PATH_MAX]; struct stat fs; + struct dir *t; /* init/reset global vars */ *lasterr = '\0'; @@ -378,12 +381,15 @@ struct dir *showCalc(char *path) { if(rpath(path, tmp) == NULL || lstat(tmp, &fs) != 0) { do { ncresize(); + if(dat != NULL) + drawBrowser(0); drawError(path); } while (getch() == KEY_RESIZE); return(NULL); } parent = calloc(sizeof(struct dir), 1); parent->size = sflags & SF_AS ? fs.st_size : fs.st_blocks * 512; + parent->flags |= FF_DIR; curdev = fs.st_dev; parent->name = malloc(strlen(tmp)+1); strcpy(parent->name, tmp); @@ -393,6 +399,14 @@ struct dir *showCalc(char *path) { freedir(parent); return(NULL); } + + /* remove reference to parent dir if we are in the parent dir */ + t = parent->sub; + parent->sub = t->next; + parent->sub->prev = NULL; + free(t->name); + free(t); + return(parent); } diff --git a/src/main.c b/src/main.c index 2edae42..11e0b8e 100644 --- a/src/main.c +++ b/src/main.c @@ -46,6 +46,7 @@ int main(int argc, char **argv) { if(gd && settingsWin()) goto mainend; + dat = NULL; while((dat = showCalc(sdir)) == NULL) if(settingsWin()) goto mainend; diff --git a/src/util.c b/src/util.c index 46f0f92..6557155 100644 --- a/src/util.c +++ b/src/util.c @@ -90,6 +90,7 @@ void ncresize(void) { if(ch == 'i') sflags |= SF_IGNS; } + erase(); }