calculate width in browser_init(), as it's more consistent
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3741 35c25a1d-7b9e-4130-9fde-d3aeb78583b8master
parent
b520d509cc
commit
2f7c1a0613
|
@ -20,12 +20,8 @@ CVS code -
|
||||||
browser_init()
|
browser_init()
|
||||||
- Fix off-by-one error when calculating longest that kept the
|
- Fix off-by-one error when calculating longest that kept the
|
||||||
rightmost column of the screen from being used. (DLR)
|
rightmost column of the screen from being used. (DLR)
|
||||||
- browser_set_width()
|
- Calculate width here instead of in browser_refresh(), as it's
|
||||||
- New function used to calculate width independently of
|
more consistent. (DLR)
|
||||||
browser_refresh(). This eliminates the need for do_browser()
|
|
||||||
to call browser_refresh() in one place if the initially
|
|
||||||
selected file is not at the beginning of the list, and in
|
|
||||||
another place if it is. (DLR)
|
|
||||||
browser_refresh()
|
browser_refresh()
|
||||||
- Simplify. (DLR)
|
- Simplify. (DLR)
|
||||||
- Fix problems where translated versions of "(dir)" could be
|
- Fix problems where translated versions of "(dir)" could be
|
||||||
|
|
134
src/browser.c
134
src/browser.c
|
@ -35,9 +35,7 @@ static char **filelist = NULL;
|
||||||
static size_t filelist_len = 0;
|
static size_t filelist_len = 0;
|
||||||
/* The number of files in the list. */
|
/* The number of files in the list. */
|
||||||
static int width = 0;
|
static int width = 0;
|
||||||
/* The number of files that we can display per line. This is
|
/* The number of files that we can display per line. */
|
||||||
* calculated via browser_set_width(), which should be called
|
|
||||||
* before doing anything that uses width. */
|
|
||||||
static int longest = 0;
|
static int longest = 0;
|
||||||
/* The number of columns in the longest filename in the list. */
|
/* The number of columns in the longest filename in the list. */
|
||||||
static size_t selected = 0;
|
static size_t selected = 0;
|
||||||
|
@ -74,15 +72,14 @@ char *do_browser(char *path, DIR *dir)
|
||||||
change_browser_directory:
|
change_browser_directory:
|
||||||
/* We go here after the user selects a new directory. */
|
/* We go here after the user selects a new directory. */
|
||||||
|
|
||||||
|
/* Start with no key pressed. */
|
||||||
kbinput = ERR;
|
kbinput = ERR;
|
||||||
width = 0;
|
|
||||||
selected = 0;
|
|
||||||
|
|
||||||
path = mallocstrassn(path, get_full_path(path));
|
path = mallocstrassn(path, get_full_path(path));
|
||||||
|
|
||||||
assert(path != NULL && path[strlen(path) - 1] == '/');
|
assert(path != NULL && path[strlen(path) - 1] == '/');
|
||||||
|
|
||||||
/* Get the file list, and set longest in the process. */
|
/* Get the file list, and set longest and width in the process. */
|
||||||
browser_init(path, dir);
|
browser_init(path, dir);
|
||||||
|
|
||||||
assert(filelist != NULL);
|
assert(filelist != NULL);
|
||||||
|
@ -97,19 +94,23 @@ char *do_browser(char *path, DIR *dir)
|
||||||
|
|
||||||
free(prev_dir);
|
free(prev_dir);
|
||||||
prev_dir = NULL;
|
prev_dir = NULL;
|
||||||
}
|
/* Otherwise, select the first file or directory in the list. */
|
||||||
|
} else
|
||||||
|
selected = 0;
|
||||||
|
|
||||||
titlebar(path);
|
titlebar(path);
|
||||||
|
|
||||||
while (!abort) {
|
while (!abort) {
|
||||||
size_t fileline;
|
struct stat st;
|
||||||
|
int i;
|
||||||
|
size_t fileline = selected / width;
|
||||||
/* The line number the selected file is on. */
|
/* The line number the selected file is on. */
|
||||||
size_t old_selected = (size_t)-1;
|
size_t old_selected = (size_t)-1;
|
||||||
/* The selected file we had before the current selected
|
/* The selected file we had before the current selected
|
||||||
* file. */
|
* file. */
|
||||||
struct stat st;
|
|
||||||
int i;
|
|
||||||
char *new_path;
|
char *new_path;
|
||||||
|
/* The path we switch to at the "Go to Directory"
|
||||||
|
* prompt. */
|
||||||
|
|
||||||
/* Display the file list if we don't have a key, or if the
|
/* Display the file list if we don't have a key, or if the
|
||||||
* selected file has changed, and set width in the process. */
|
* selected file has changed, and set width in the process. */
|
||||||
|
@ -119,9 +120,6 @@ char *do_browser(char *path, DIR *dir)
|
||||||
kbinput = get_kbinput(edit, &meta_key, &func_key);
|
kbinput = get_kbinput(edit, &meta_key, &func_key);
|
||||||
parse_browser_input(&kbinput, &meta_key, &func_key);
|
parse_browser_input(&kbinput, &meta_key, &func_key);
|
||||||
|
|
||||||
/* Get the line number of the selected file. */
|
|
||||||
fileline = selected / width;
|
|
||||||
|
|
||||||
switch (kbinput) {
|
switch (kbinput) {
|
||||||
#ifndef DISABLE_MOUSE
|
#ifndef DISABLE_MOUSE
|
||||||
case KEY_MOUSE:
|
case KEY_MOUSE:
|
||||||
|
@ -435,18 +433,29 @@ char *do_browse_from(const char *inpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set filelist to the list of files contained in the directory path,
|
/* Set filelist to the list of files contained in the directory path,
|
||||||
* set filelist_len to the number of files in that list, and set longest
|
* set filelist_len to the number of files in that list, set longest to
|
||||||
* to the width in columns of the longest filename in that list, at
|
* the width in columns of the longest filename in that list (at least
|
||||||
* least 15 and at most COLS. We need at least 15 columns to display
|
* 15 and at most COLS), and set width to the number of files that we
|
||||||
* ".. (parent dir)", as Pico does. Assume path exists and is a
|
* can display per line. longest needs to be at least 15 columns in
|
||||||
* directory. */
|
* order to hold ".. (parent dir)", as Pico does. Assume path exists
|
||||||
|
* and is a directory. */
|
||||||
void browser_init(const char *path, DIR *dir)
|
void browser_init(const char *path, DIR *dir)
|
||||||
{
|
{
|
||||||
const struct dirent *nextdir;
|
const struct dirent *nextdir;
|
||||||
size_t i = 0, path_len;
|
size_t i = 0;
|
||||||
|
size_t path_len = strlen(path);
|
||||||
|
int col = 0;
|
||||||
|
/* The maximum number of columns that the filenames will take
|
||||||
|
* up. */
|
||||||
|
int line = 0;
|
||||||
|
/* The maximum number of lines that the filenames will take
|
||||||
|
* up. */
|
||||||
|
int filesperline = 0;
|
||||||
|
/* The number of files that we can display per line. */
|
||||||
|
|
||||||
assert(dir != NULL);
|
assert(path != NULL && path[strlen(path) - 1] == '/' && dir != NULL);
|
||||||
|
|
||||||
|
/* Set longest to zero, just before we initialize it. */
|
||||||
longest = 0;
|
longest = 0;
|
||||||
|
|
||||||
while ((nextdir = readdir(dir)) != NULL) {
|
while ((nextdir = readdir(dir)) != NULL) {
|
||||||
|
@ -456,14 +465,15 @@ void browser_init(const char *path, DIR *dir)
|
||||||
if (strcmp(nextdir->d_name, ".") == 0)
|
if (strcmp(nextdir->d_name, ".") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
i++;
|
|
||||||
|
|
||||||
d_len = strlenpt(nextdir->d_name);
|
d_len = strlenpt(nextdir->d_name);
|
||||||
if (d_len > longest)
|
if (d_len > longest)
|
||||||
longest = (d_len > COLS) ? COLS : d_len;
|
longest = (d_len > COLS) ? COLS : d_len;
|
||||||
|
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
filelist_len = i;
|
filelist_len = i;
|
||||||
|
|
||||||
rewinddir(dir);
|
rewinddir(dir);
|
||||||
|
|
||||||
/* Put 10 columns' worth of blank space between columns of filenames
|
/* Put 10 columns' worth of blank space between columns of filenames
|
||||||
|
@ -472,8 +482,6 @@ void browser_init(const char *path, DIR *dir)
|
||||||
|
|
||||||
filelist = (char **)nmalloc(filelist_len * sizeof(char *));
|
filelist = (char **)nmalloc(filelist_len * sizeof(char *));
|
||||||
|
|
||||||
path_len = strlen(path);
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while ((nextdir = readdir(dir)) != NULL && i < filelist_len) {
|
while ((nextdir = readdir(dir)) != NULL && i < filelist_len) {
|
||||||
|
@ -483,6 +491,7 @@ void browser_init(const char *path, DIR *dir)
|
||||||
|
|
||||||
filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
|
filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
|
||||||
sprintf(filelist[i], "%s%s", path, nextdir->d_name);
|
sprintf(filelist[i], "%s%s", path, nextdir->d_name);
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -490,6 +499,7 @@ void browser_init(const char *path, DIR *dir)
|
||||||
* first time we scanned and the second. i is the actual length of
|
* first time we scanned and the second. i is the actual length of
|
||||||
* filelist, so record it. */
|
* filelist, so record it. */
|
||||||
filelist_len = i;
|
filelist_len = i;
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
/* Make sure longest is between 15 and COLS. */
|
/* Make sure longest is between 15 and COLS. */
|
||||||
|
@ -497,6 +507,36 @@ void browser_init(const char *path, DIR *dir)
|
||||||
longest = 15;
|
longest = 15;
|
||||||
if (longest > COLS)
|
if (longest > COLS)
|
||||||
longest = COLS;
|
longest = COLS;
|
||||||
|
|
||||||
|
/* Set width to zero, just before we initialize it. */
|
||||||
|
width = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < filelist_len && line < editwinrows; i++) {
|
||||||
|
/* Calculate the number of columns one filename will take up. */
|
||||||
|
col += longest;
|
||||||
|
filesperline++;
|
||||||
|
|
||||||
|
/* Add some space between the columns. */
|
||||||
|
col += 2;
|
||||||
|
|
||||||
|
/* If the next entry isn't going to fit on the current line,
|
||||||
|
* move to the next line. */
|
||||||
|
if (col > COLS - longest) {
|
||||||
|
line++;
|
||||||
|
col = 0;
|
||||||
|
|
||||||
|
/* If width isn't initialized yet, and we've taken up more
|
||||||
|
* than one line, it means that width is equal to
|
||||||
|
* filesperline. */
|
||||||
|
if (width == 0)
|
||||||
|
width = filesperline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If width isn't initialized yet, and we've taken up only one line,
|
||||||
|
* it means that width is equal to (COLS % longest). */
|
||||||
|
if (width == 0)
|
||||||
|
width = COLS % longest;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Determine the shortcut key corresponding to the values of kbinput
|
/* Determine the shortcut key corresponding to the values of kbinput
|
||||||
|
@ -542,49 +582,6 @@ void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the number of files that we can display per line, and set
|
|
||||||
* width to it. It will always be at least one. */
|
|
||||||
void browser_set_width(void)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
int col = 0;
|
|
||||||
/* The maximum number of columns that the filenames will take
|
|
||||||
* up. */
|
|
||||||
int line = 0;
|
|
||||||
/* The maximum number of lines that the filenames will take
|
|
||||||
* up. */
|
|
||||||
int filesperline = 0;
|
|
||||||
/* The number of files that we can display per line. */
|
|
||||||
|
|
||||||
width = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < filelist_len && line < editwinrows; i++) {
|
|
||||||
/* Calculate the number of columns one filename will take up. */
|
|
||||||
col += longest;
|
|
||||||
filesperline++;
|
|
||||||
|
|
||||||
/* Add some space between the columns. */
|
|
||||||
col += 2;
|
|
||||||
|
|
||||||
/* If the next entry isn't going to fit on the current line,
|
|
||||||
* move to the next line. */
|
|
||||||
if (col > COLS - longest) {
|
|
||||||
line++;
|
|
||||||
col = 0;
|
|
||||||
|
|
||||||
/* We've taken up at least one line, which means that width
|
|
||||||
* is equivalent to filesperline, so set it. */
|
|
||||||
if (width == 0)
|
|
||||||
width = filesperline;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We haven't taken up at least one line, which means that width is
|
|
||||||
* equivalent to (COLS % longest), so set it. */
|
|
||||||
if (width == 0)
|
|
||||||
width = COLS % longest;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set width to the number of files that we can display per line, if
|
/* Set width to the number of files that we can display per line, if
|
||||||
* necessary, and display the list of files. */
|
* necessary, and display the list of files. */
|
||||||
void browser_refresh(void)
|
void browser_refresh(void)
|
||||||
|
@ -603,9 +600,6 @@ void browser_refresh(void)
|
||||||
if (uimax_digits == -1)
|
if (uimax_digits == -1)
|
||||||
uimax_digits = digits(UINT_MAX);
|
uimax_digits = digits(UINT_MAX);
|
||||||
|
|
||||||
if (width == 0)
|
|
||||||
browser_set_width();
|
|
||||||
|
|
||||||
blank_edit();
|
blank_edit();
|
||||||
|
|
||||||
wmove(edit, 0, 0);
|
wmove(edit, 0, 0);
|
||||||
|
|
|
@ -147,7 +147,6 @@ char *do_browser(char *path, DIR *dir);
|
||||||
char *do_browse_from(const char *inpath);
|
char *do_browse_from(const char *inpath);
|
||||||
void browser_init(const char *path, DIR *dir);
|
void browser_init(const char *path, DIR *dir);
|
||||||
void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key);
|
void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key);
|
||||||
void browser_set_width(void);
|
|
||||||
void browser_refresh(void);
|
void browser_refresh(void);
|
||||||
bool browser_select_filename(const char *needle);
|
bool browser_select_filename(const char *needle);
|
||||||
int filesearch_init(void);
|
int filesearch_init(void);
|
||||||
|
|
Loading…
Reference in New Issue