From 8ddc5e7b93e09083aa7667caee79738aeef1cce2 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg <bensberg@justemail.net> Date: Tue, 10 May 2016 21:04:33 +0200 Subject: [PATCH] browser: make the calculation of files-per-line a straightforward one There is no need to iterate through all the file names, as we already know the length of the longest. From this it's easy to calculate how many names fit on a line. --- src/browser.c | 41 ++++------------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/src/browser.c b/src/browser.c index 24495ab8..cf94cb95 100644 --- a/src/browser.c +++ b/src/browser.c @@ -416,14 +416,6 @@ void browser_init(const char *path, DIR *dir) { const struct dirent *nextdir; size_t i = 0, 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(path != NULL && path[strlen(path) - 1] == '/' && dir != NULL); @@ -477,35 +469,10 @@ void browser_init(const char *path, DIR *dir) closedir(dir); - /* 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 longest. */ - if (width == 0) - width = longest; + /* Calculate how many files fit on a line -- feigning room for two + * spaces beyond the right edge, and adding two spaces of padding + * between columns. */ + width = (COLS + 2) / (longest + 2); } /* Return the function that is bound to the given key, accepting certain -- GitLab