[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 3/5] less: add a function to detect when display is at end of file
From:       Ron Yorston <rmy () pobox ! com>
Date:       2015-07-24 13:28:50
Message-ID: 55b23d92.MTkQn1BG0CZaByCY%rmy () pobox ! com
[Download RAW message or body]

Add a function to package the test that detects whether enough has
been read from the file to allow a screenful to be displayed.

Also use this to determine when to display '(END)' in the status
line.  The previous code was incomplete and didn't handle truncated
lines (-S flag) properly.

function                                             old     new   delta
at_end                                                 -      63     +63
status_print                                         111     109      -2
read_lines                                           819     764     -55
getch_nowait                                         319     264     -55
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/3 up/down: 63/-112)           Total: -49 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 miscutils/less.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index 93d09ee..3e37794 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -406,6 +406,14 @@ static void fill_match_lines(unsigned pos);
 #define fill_match_lines(pos) ((void)0)
 #endif
 
+static int at_end(void)
+{
+	return (option_mask32 & FLAG_S) ?
+		!(cur_fline <= max_fline &&
+			max_lineno > LINENO(flines[cur_fline]) + max_displayed_line) :
+		!(max_fline > cur_fline + max_displayed_line);
+}
+
 /* Devilishly complex routine.
  *
  * Has to deal with EOF and EPIPE on input,
@@ -552,11 +560,7 @@ static void read_lines(void)
 			eof_error = 0; /* Pretend we saw EOF */
 			break;
 		}
-		if (!(option_mask32 & FLAG_S)
-		  ? (max_fline > cur_fline + max_displayed_line)
-		  : (max_fline >= cur_fline
-		     && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
-		) {
+		if (!at_end()) {
 #if !ENABLE_FEATURE_LESS_REGEXP
 			break;
 #else
@@ -662,7 +666,7 @@ static void m_status_print(void)
 	if (num_lines >= 0)
 		printf("/%i", num_lines);
 
-	if (cur_fline >= (int)(max_fline - max_displayed_line)) {
+	if (at_end()) {
 		printf(" (END)");
 		if (num_files > 1 && current_file != num_files)
 			printf(" - next: %s", files[current_file]);
@@ -692,7 +696,7 @@ static void status_print(void)
 #endif
 
 	clear_line();
-	if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
+	if (cur_fline && !at_end()) {
 		bb_putchar(':');
 		return;
 	}
@@ -1009,12 +1013,7 @@ static int64_t getch_nowait(void)
 	 */
 	rd = 1;
 	/* Are we interested in stdin? */
-//TODO: reuse code for determining this
-	if (!(option_mask32 & FLAG_S)
-	   ? !(max_fline > cur_fline + max_displayed_line)
-	   : !(max_fline >= cur_fline
-	       && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
-	) {
+	if (at_end()) {
 		if (eof_error > 0) /* did NOT reach eof yet */
 			rd = 0; /* yes, we are interested in stdin */
 	}
-- 
2.4.3

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic