From 14a501bd04d20cfdb79308648065114eff7857aa Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Thu, 10 May 2018 17:02:16 +1000 Subject: [PATCH] Add window_columns() function to determine number of columns used for splitting window output Replaces two open-coded versions of the same pattern. --- include/window.h | 1 + source/functions.c | 7 ++----- source/screen.c | 5 +---- source/window.c | 12 ++++++++++++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/window.h b/include/window.h index f2f7872..aaba34a 100644 --- a/include/window.h +++ b/include/window.h @@ -37,6 +37,7 @@ Window *BX_new_window (struct ScreenStru *); void BX_delete_window (Window *); void BX_add_to_invisible_list (Window *); +int window_columns(Window *window); Window *BX_add_to_window_list (struct ScreenStru *, Window *); void BX_remove_from_window_from_screen (Window *); void BX_recalculate_window_positions (struct ScreenStru *); diff --git a/source/functions.c b/source/functions.c index d38b72b..1bcdbd3 100644 --- a/source/functions.c +++ b/source/functions.c @@ -6308,12 +6308,9 @@ BUILT_IN_FUNCTION(function_numlines, input) char *s = NULL; if (input && *input) { - int cols; + int cols = window_columns(current_window); + s = LOCAL_COPY(input); - if (current_window->screen) - cols = current_window->screen->co; - else - cols = current_window->columns; for (lines = split_up_line(s, cols + 1); *lines; lines++) count++; } diff --git a/source/screen.c b/source/screen.c index 72b1fb6..f7d63db 100644 --- a/source/screen.c +++ b/source/screen.c @@ -218,10 +218,7 @@ void BX_add_to_window(Window *window, const char *str) display_standout(OFF); display_bold(OFF); - if (window->screen) - cols = window->screen->co; - else - cols = window->columns; + cols = window_columns(window); for (lines = split_up_line(str, cols/* + 1*/); *lines; lines++) { diff --git a/source/window.c b/source/window.c index 27b4468..15c3e31 100644 --- a/source/window.c +++ b/source/window.c @@ -427,6 +427,18 @@ void BX_add_to_invisible_list(Window *window) window->screen = NULL; } +/* window_columns() + * + * Returns the number of columns in the window for wrapping output. For + * visible windows, this is the number of columns on the screen; for + * invisible windows, this is the saved size the window had when it was last + * visible. + */ +int window_columns(Window *window) +{ + return window->screen ? window->screen->co : window->columns; +} + /* * add_to_window_list: This inserts the given window into the visible window * list (and thus adds it to the displayed windows on the screen). The