@@ -453,7 +453,14 @@ fn run(
453453 println ! ( "{}" , print_svg( & graph, settings) ?) ;
454454 } else {
455455 let ( g_lines, t_lines, _indices) = print_unicode ( & graph, settings) ?;
456- if pager && atty:: is ( atty:: Stream :: Stdout ) {
456+ let use_pager =
457+ // Pager is enabled
458+ pager
459+ // and in a terminal, not a pipe
460+ && atty:: is ( atty:: Stream :: Stdout )
461+ // and terminal height is not enough for all lines + the help text
462+ && ( crossterm:: terminal:: size ( ) . unwrap ( ) . 1 as usize ) < g_lines. len ( ) + 1 ;
463+ if use_pager {
457464 print_paged ( & g_lines, & t_lines) . map_err ( |err| err. to_string ( ) ) ?;
458465 } else {
459466 print_unpaged ( & g_lines, & t_lines) ;
@@ -487,14 +494,17 @@ fn print_paged(graph_lines: &[String], text_lines: &[String]) -> Result<(), Erro
487494 } ;
488495
489496 enable_raw_mode ( ) ?;
490- while start_idx + visible_lines < graph_lines . len ( ) {
497+ loop {
491498 // Print commits
492499 if should_update {
493500 should_update = false ;
501+ // Make sure that start_idx + visible_lines <= graph_lines.len()
502+ start_idx = start_idx. min ( graph_lines. len ( ) . saturating_sub ( visible_lines) ) ;
494503 stdout ( )
495504 . execute ( MoveToRow ( 0 ) ) ?
496505 . execute ( Clear ( ClearType :: CurrentLine ) ) ?;
497- for curr_idx in 0 ..visible_lines {
506+ let content_len = visible_lines. min ( graph_lines. len ( ) ) ;
507+ for curr_idx in 0 ..content_len {
498508 stdout ( )
499509 . execute ( Clear ( ClearType :: CurrentLine ) ) ?
500510 . execute ( Print ( format ! (
@@ -503,6 +513,10 @@ fn print_paged(graph_lines: &[String], text_lines: &[String]) -> Result<(), Erro
503513 text_lines[ start_idx + curr_idx]
504514 ) ) ) ?;
505515 }
516+ if content_len < visible_lines {
517+ // Exit if screen is larger than full list
518+ break ;
519+ }
506520 // Print help at the end
507521 stdout ( ) . execute ( Print ( help) ) ?;
508522 } else {
0 commit comments