From: David Lawrence Ramsey Date: Fri, 30 Jun 2006 02:44:11 +0000 (+0000) Subject: in browser_refresh(), fix problems where translated versions of "(dir)" X-Git-Tag: v1.9.99pre1~167 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=99c87ccb0c42b027e63309565d49c772ae07fcb3;p=nano.git in browser_refresh(), fix problems where translated versions of "(dir)" could be truncated, and where file sizes could be too long git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3694 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 02c9ec45..ac75f5a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,8 @@ CVS code - - Remove unneeded call to blank_edit(). (DLR) browser_refresh() - Simplify. (DLR) + - Fix problems where translated versions of "(dir)" could be + truncated, and where file sizes could be too long. (DLR) - doc/syntax/c.nanorc: - Since .i and .ii are preprocessed C and C++ output, colorize them here. (Mike Frysinger) diff --git a/src/browser.c b/src/browser.c index a7d11241..94959b52 100644 --- a/src/browser.c +++ b/src/browser.c @@ -522,11 +522,13 @@ void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key) * files. */ void browser_refresh(void) { - struct stat st; + static int uimax_digits = -1; size_t i; int col = 0, line = 0, filecols = 0; - size_t foo_len = mb_cur_max() * 7; - char *foo = charalloc(foo_len + 1); + char *foo; + + if (uimax_digits == -1) + uimax_digits = digits(UINT_MAX); blank_edit(); @@ -536,6 +538,7 @@ void browser_refresh(void) editwinrows) : 0; for (; i < filelist_len && line < editwinrows; i++) { + struct stat st; char *disp = display_string(tail(filelist[i]), 0, longest, FALSE); @@ -550,36 +553,49 @@ void browser_refresh(void) col += longest; filecols++; - /* Show file info also. We don't want to report file sizes for - * links, so we use lstat(). Also, stat() and lstat() return an - * error if, for example, the file is deleted while the file - * browser is open. In that case, we report "--" as the file - * info. */ + /* Show information about the file. We don't want to report + * file sizes for links, so we use lstat(). */ if (lstat(filelist[i], &st) == -1 || S_ISLNK(st.st_mode)) { - /* Aha! It's a symlink! Now, is it a dir? If so, mark it - * as such. */ - if (stat(filelist[i], &st) == 0 && S_ISDIR(st.st_mode)) { - strncpy(foo, _("(dir)"), foo_len); - foo[foo_len] = '\0'; - } else - strcpy(foo, "--"); - } else if (S_ISDIR(st.st_mode)) { - strncpy(foo, _("(dir)"), foo_len); - foo[foo_len] = '\0'; - } else if (st.st_size < (1 << 10)) /* less than 1 k. */ - sprintf(foo, "%4u B", (unsigned int)st.st_size); - else if (st.st_size < (1 << 20)) /* less than 1 meg. */ - sprintf(foo, "%4u KB", (unsigned int)(st.st_size >> 10)); - else if (st.st_size < (1 << 30)) /* less than 1 gig. */ - sprintf(foo, "%4u MB", (unsigned int)(st.st_size >> 20)); - else - sprintf(foo, "%4u GB", (unsigned int)(st.st_size >> 30)); - - mvwaddnstr(edit, line, col - strlen(foo), foo, foo_len); + /* If the file doesn't exist (i.e, it's been deleted while + * the file browser is open), or it's a symlink that doesn't + * point to a directory, display "--". */ + if (stat(filelist[i], &st) == -1 || !S_ISDIR(st.st_mode)) + foo = mallocstrcpy(NULL, "--"); + /* If the file is a symlink that points to a directory, + * display it as a directory. */ + else + foo = mallocstrcpy(NULL, _("(dir)")); + } else if (S_ISDIR(st.st_mode)) + /* If the file is a directory, display it as such. */ + foo = mallocstrcpy(NULL, _("(dir)")); + else { + foo = charalloc(uimax_digits + 4); + + /* Bytes. */ + if (st.st_size < (1 << 10)) + sprintf(foo, "%4u B", (unsigned int)st.st_size); + /* Kilobytes. */ + else if (st.st_size < (1 << 20)) + sprintf(foo, "%4u KB", + (unsigned int)(st.st_size >> 10)); + /* Megabytes. */ + else if (st.st_size < (1 << 30)) + sprintf(foo, "%4u MB", + (unsigned int)(st.st_size >> 20)); + /* Gigabytes. */ + else + sprintf(foo, "%4u GB", + (unsigned int)(st.st_size >> 30)); + } + + mvwaddnstr(edit, line, col - strlenpt(foo), foo, + actual_x(foo, 7)); if (i == selected) wattroff(edit, reverse_attr); + free(foo); + /* Add some space between the columns. */ col += 2; @@ -598,8 +614,6 @@ void browser_refresh(void) wmove(edit, line, col); } - free(foo); - wnoutrefresh(edit); }