@@ -460,7 +460,7 @@ struct Pager<'a> {
460460 line_count : usize ,
461461 silent : bool ,
462462 squeeze : bool ,
463- line_squeezed : usize ,
463+ lines_squeezed : usize ,
464464}
465465
466466impl < ' a > Pager < ' a > {
@@ -501,7 +501,7 @@ impl<'a> Pager<'a> {
501501 line_count,
502502 silent : options. silent ,
503503 squeeze : options. squeeze ,
504- line_squeezed : 0 ,
504+ lines_squeezed : 0 ,
505505 }
506506 }
507507
@@ -557,7 +557,7 @@ impl<'a> Pager<'a> {
557557 fn page_up ( & mut self ) {
558558 self . upper_mark = self
559559 . upper_mark
560- . saturating_sub ( self . content_rows . saturating_add ( self . line_squeezed ) ) ;
560+ . saturating_sub ( self . content_rows . saturating_add ( self . lines_squeezed ) ) ;
561561
562562 if self . squeeze {
563563 let mut line = String :: new ( ) ;
@@ -578,32 +578,64 @@ impl<'a> Pager<'a> {
578578 }
579579
580580 fn next_line ( & mut self ) {
581+ // Don't proceed if we're already at the last line
581582 if self . upper_mark >= self . line_count . saturating_sub ( 1 ) {
582583 return ;
583584 }
584585
586+ // Remove the line that's no longer visible at the top
587+ self . lines . pop_front ( ) ;
588+
589+ // Move the viewing window down by one line
585590 self . upper_mark = self . upper_mark . saturating_add ( 1 ) ;
586- self . lines . clear ( ) ;
591+
592+ let next_line_position = self . upper_mark + self . lines . len ( ) ;
593+ self . seek_to_line ( next_line_position) . unwrap ( ) ;
594+
595+ self . lines_squeezed = 0 ;
596+
597+ // Read new lines until we've filled the view
598+ let mut line = String :: new ( ) ;
599+ while self . lines . len ( ) < self . content_rows {
600+ line. clear ( ) ;
601+ if self . reader . read_line ( & mut line) . unwrap ( ) == 0 {
602+ break ; // EOF
603+ }
604+
605+ if self . should_squeeze_line ( & line) {
606+ self . lines_squeezed += 1 ;
607+ } else {
608+ self . lines . push_back ( std:: mem:: take ( & mut line) ) ;
609+ }
610+ }
587611 }
588612
589613 fn prev_line ( & mut self ) {
614+ // Don't proceed if we're already at the first line
590615 if self . upper_mark == 0 {
591616 return ;
592617 }
593618
619+ // Remove the line that's no longer visible at the bottom
620+ self . lines . pop_back ( ) ;
621+
622+ // Move the viewing window up by one line
594623 self . upper_mark = self . upper_mark . saturating_sub ( 1 ) ;
624+
595625 self . seek_to_line ( self . upper_mark ) . unwrap ( ) ;
596626
597- let mut line = String :: new ( ) ;
598- self . reader . read_line ( & mut line) . unwrap ( ) ;
627+ self . lines_squeezed = 0 ;
599628
600- if !self . should_squeeze_line ( & line) {
601- if self . lines . len ( ) >= self . content_rows {
602- self . lines . pop_back ( ) ;
629+ // Read new lines until we've filled the view
630+ let mut line = String :: new ( ) ;
631+ while self . lines . len ( ) < self . content_rows {
632+ line. clear ( ) ;
633+ self . reader . read_line ( & mut line) . unwrap ( ) ;
634+ if self . should_squeeze_line ( & line) {
635+ self . lines_squeezed += 1 ;
636+ } else {
637+ self . lines . push_front ( std:: mem:: take ( & mut line) ) ;
603638 }
604- self . lines . push_front ( line) ;
605- } else {
606- self . line_squeezed += 1 ;
607639 }
608640 }
609641
@@ -666,21 +698,19 @@ impl<'a> Pager<'a> {
666698 }
667699
668700 fn load_visible_lines ( & mut self ) -> UResult < ( ) > {
669- let lines_needed = self . content_rows - self . lines . len ( ) ;
701+ self . lines_squeezed = 0 ;
670702
671703 self . seek_to_line ( self . upper_mark + self . lines . len ( ) ) ?;
672704
673- self . line_squeezed = 0 ;
674-
675705 let mut line = String :: new ( ) ;
676- while self . lines . len ( ) < lines_needed {
706+ while self . lines . len ( ) < self . content_rows {
677707 line. clear ( ) ;
678708 if self . reader . read_line ( & mut line) ? == 0 {
679709 break ; // EOF
680710 }
681711
682712 if self . should_squeeze_line ( & line) {
683- self . line_squeezed += 1 ;
713+ self . lines_squeezed += 1 ;
684714 } else {
685715 self . lines . push_back ( std:: mem:: take ( & mut line) ) ;
686716 }
0 commit comments