@@ -2,7 +2,7 @@ use std::borrow::Cow;
22use std:: collections:: VecDeque ;
33use std:: collections:: hash_map:: Entry ;
44use std:: sync:: { Arc , Mutex } ;
5- use std:: time:: Duration ;
5+ use std:: time:: { Duration , Instant } ;
66
77use console:: { Term , strip_ansi_codes} ;
88use indicatif:: { ProgressBar , ProgressStyle } ;
@@ -33,6 +33,8 @@ struct HookBar {
3333 output_bars : Vec < ProgressBar > ,
3434 /// Rolling text state rendered into `output_bars`.
3535 output_preview : OutputPreview ,
36+ /// Hook start time, used to avoid flashing output preview rows for fast hooks.
37+ started_at : Instant ,
3638 /// Result is filled by `on_run_result`; it stays `None` between completion
3739 /// and result reporting.
3840 passed : Option < bool > ,
@@ -45,6 +47,7 @@ impl HookBar {
4547 progress,
4648 output_bars : Vec :: new ( ) ,
4749 output_preview : OutputPreview :: default ( ) ,
50+ started_at : Instant :: now ( ) ,
4851 passed : None ,
4952 }
5053 }
@@ -65,6 +68,10 @@ impl HookBar {
6568 chunk : & [ u8 ] ,
6669 ) -> Option < ProgressBar > {
6770 self . output_preview . push_chunk ( chunk) ;
71+ if self . output_bars . is_empty ( ) && self . started_at . elapsed ( ) < HOOK_OUTPUT_PREVIEW_DELAY {
72+ return None ;
73+ }
74+
6875 let lines = self . output_preview . visible_lines ( ) ;
6976 let mut inserted_tail = None ;
7077
@@ -321,6 +328,7 @@ fn is_preview_char(ch: char) -> bool {
321328}
322329
323330const HOOK_OUTPUT_PREVIEW_LINES : usize = 3 ;
331+ const HOOK_OUTPUT_PREVIEW_DELAY : Duration = Duration :: from_millis ( 500 ) ;
324332const HOOK_OUTPUT_PREVIEW_PREFIX : & str = " => " ;
325333
326334fn truncate_to_width ( input : & str , width : usize ) -> Cow < ' _ , str > {
@@ -743,10 +751,31 @@ mod tests {
743751 progress : ProgressBar :: hidden ( ) ,
744752 output_bars : Vec :: new ( ) ,
745753 output_preview : OutputPreview :: default ( ) ,
754+ started_at : Instant :: now ( ) ,
746755 passed,
747756 }
748757 }
749758
759+ fn running_hook_bar ( reporter : & HookRunReporter , started_at : Instant ) -> HookBar {
760+ HookBar {
761+ hook_key : HookKey {
762+ project_idx : 0 ,
763+ hook_idx : 0 ,
764+ } ,
765+ progress : progress_bar ( reporter) ,
766+ output_bars : Vec :: new ( ) ,
767+ output_preview : OutputPreview :: default ( ) ,
768+ started_at,
769+ passed : None ,
770+ }
771+ }
772+
773+ fn elapsed_start ( ) -> Instant {
774+ Instant :: now ( )
775+ . checked_sub ( HOOK_OUTPUT_PREVIEW_DELAY + Duration :: from_millis ( 1 ) )
776+ . unwrap ( )
777+ }
778+
750779 fn hook_group ( order : usize , has_header : bool ) -> HookGroup {
751780 let header = if has_header {
752781 Some ( ProgressBar :: hidden ( ) )
@@ -840,6 +869,36 @@ mod tests {
840869 assert_eq ! ( preview. visible_lines( ) , [ "two" , "three" , "four" ] ) ;
841870 }
842871
872+ #[ test]
873+ fn hook_output_preview_is_buffered_before_delay ( ) {
874+ let reporter = HookRunReporter :: new ( Printer :: Silent , 80 , false ) ;
875+ let mut hook_bar = running_hook_bar ( & reporter, Instant :: now ( ) ) ;
876+
877+ let tail = hook_bar. push_output ( & reporter. reporter , 80 , b"first\n " ) ;
878+
879+ assert ! ( tail. is_none( ) ) ;
880+ assert ! ( hook_bar. output_bars. is_empty( ) ) ;
881+ assert_eq ! ( hook_bar. output_preview. visible_lines( ) , [ "first" ] ) ;
882+ }
883+
884+ #[ test]
885+ fn hook_output_preview_shows_buffered_lines_after_delay ( ) {
886+ let reporter = HookRunReporter :: new ( Printer :: Silent , 80 , false ) ;
887+ let mut hook_bar = running_hook_bar ( & reporter, Instant :: now ( ) ) ;
888+
889+ hook_bar. push_output ( & reporter. reporter , 80 , b"first\n " ) ;
890+ hook_bar. started_at = elapsed_start ( ) ;
891+ let tail = hook_bar. push_output ( & reporter. reporter , 80 , b"second\n " ) ;
892+
893+ assert ! ( tail. is_some( ) ) ;
894+ let messages = hook_bar
895+ . output_bars
896+ . iter ( )
897+ . map ( |bar| bar. message ( ) . clone ( ) )
898+ . collect :: < Vec < _ > > ( ) ;
899+ assert_eq ! ( messages, [ "first" , "second" ] ) ;
900+ }
901+
843902 #[ test]
844903 fn hiding_completed_bars_frees_one_line ( ) {
845904 let mut completed = CompletedBars :: default ( ) ;
0 commit comments