@@ -336,7 +336,7 @@ fn more(
336336 }
337337
338338 // Initial display
339- pager. draw ( & mut stdout, None ) ?;
339+ pager. draw ( & mut stdout, true , None ) ?;
340340
341341 // Reset multi-file settings after initial display
342342 if multiple_file {
@@ -446,7 +446,7 @@ fn process_events(pager: &mut Pager, stdout: &mut Stdout, options: &Options) ->
446446 _ => continue ,
447447 }
448448 update_display ( stdout, options) ?;
449- pager. draw ( stdout, wrong_key) ?;
449+ pager. draw ( stdout, false , wrong_key) ?;
450450 }
451451}
452452
@@ -532,7 +532,7 @@ impl<'a> Pager<'a> {
532532 lines_squeezed : 0 ,
533533 } ;
534534
535- // Load initial content
535+ // Start from the specified line
536536 pager. read_until_line ( pager. upper_mark ) ?;
537537
538538 // Handle pattern search if specified
@@ -549,24 +549,25 @@ impl<'a> Pager<'a> {
549549 Ok ( pager)
550550 }
551551
552- fn read_until_line ( & mut self , line_num : usize ) -> UResult < ( ) > {
553- // Read until we reach the specified line number or EOF
552+ fn read_until_line ( & mut self , target_line : usize ) -> UResult < ( ) > {
553+ // Check if already read enough lines
554+ if self . lines . len ( ) > target_line {
555+ return Ok ( ( ) ) ;
556+ }
557+ // Read lines until we reach the target line or EOF
554558 let mut line = String :: new ( ) ;
555- while self . lines . len ( ) <= line_num {
559+ while self . lines . len ( ) <= target_line {
556560 line. clear ( ) ;
557561 let bytes_read = self . input . read_line ( & mut line) ?;
558562 if bytes_read == 0 {
559563 self . eof_reached = true ;
560- // Adjust upper mark to ensure content fills the screen
561- // without going beyond available lines
562- self . upper_mark = self . lines . len ( ) . saturating_sub ( self . content_rows ) ;
563564 break ;
564565 }
565566 // Track cumulative byte position
566567 let current_position =
567568 self . cumulative_line_sizes . last ( ) . unwrap_or ( & 0 ) + bytes_read as u64 ;
568569 self . cumulative_line_sizes . push ( current_position) ;
569- // Use mem::take to avoid unnecessary clone
570+ // Store the line (using mem::take to avoid clone)
570571 self . lines . push ( std:: mem:: take ( & mut line) ) ;
571572 }
572573 Ok ( ( ) )
@@ -637,14 +638,19 @@ impl<'a> Pager<'a> {
637638 } ;
638639 }
639640
640- fn draw ( & mut self , stdout : & mut Stdout , wrong_key : Option < char > ) -> UResult < ( ) > {
641- self . draw_lines ( stdout) ?;
641+ fn draw (
642+ & mut self ,
643+ stdout : & mut Stdout ,
644+ first_paint : bool ,
645+ wrong_key : Option < char > ,
646+ ) -> UResult < ( ) > {
647+ self . draw_lines ( stdout, first_paint) ?;
642648 self . draw_prompt ( stdout, wrong_key) ;
643649 stdout. flush ( ) ?;
644650 Ok ( ( ) )
645651 }
646652
647- fn draw_lines ( & mut self , stdout : & mut impl Write ) -> UResult < ( ) > {
653+ fn draw_lines ( & mut self , stdout : & mut impl Write , first_paint : bool ) -> UResult < ( ) > {
648654 // Clear current prompt line
649655 stdout. queue ( Clear ( ClearType :: CurrentLine ) ) ?;
650656
@@ -665,6 +671,10 @@ impl<'a> Pager<'a> {
665671 index += 1 ;
666672 continue ;
667673 }
674+ // Stop if we reach the end of the file and not in first paint
675+ if !first_paint && index >= self . lines . len ( ) {
676+ break ;
677+ }
668678 // Display the line
669679 let line = self . lines . get ( index) . unwrap_or ( & fallback) ;
670680 stdout. write_all ( format ! ( "\r {}" , line) . as_bytes ( ) ) ?;
0 commit comments