@@ -89,6 +89,7 @@ typedef struct {
8989 }output_handle ;
9090bool out_isatty ;
9191SignalWatcher winch_handle ,cont_handle ;
92+ uv_timer_t initial_resize_timer ;
9293bool cont_received ;
9394UGrid grid ;
9495kvec_t (Rect )invalid_regions ;
@@ -170,6 +171,8 @@ UI *tui_start(void)
170171ui -> screenshot = tui_screenshot ;
171172ui -> option_set = tui_option_set ;
172173ui -> raw_line = tui_raw_line ;
174+ ui -> height = 0 ;
175+ ui -> width = 0 ;
173176
174177memset (ui -> ui_ext ,0 ,sizeof (ui -> ui_ext ));
175178ui -> ui_ext [kUILinegrid ]= true;
@@ -227,6 +230,9 @@ static void terminfo_start(UI *ui)
227230data -> unibi_ext .set_underline_color = -1 ;
228231data -> out_fd = STDOUT_FILENO ;
229232data -> out_isatty = os_isatty (data -> out_fd );
233+ // Resizing at startup
234+ uv_timer_init (& data -> loop -> uv ,& data -> initial_resize_timer );
235+ uv_handle_set_data ((uv_handle_t * )& data -> initial_resize_timer ,ui );
230236
231237const char * term = os_getenv ("TERM" );
232238#ifdef WIN32
@@ -474,6 +480,7 @@ static void tui_main(UIBridgeData *bridge, UI *ui)
474480signal_watcher_stop (& data -> cont_handle );
475481signal_watcher_close (& data -> cont_handle ,NULL );
476482signal_watcher_close (& data -> winch_handle ,NULL );
483+ uv_close ((uv_handle_t * )& data -> initial_resize_timer ,NULL );
477484loop_close (& tui_loop , false);
478485kv_destroy (data -> invalid_regions );
479486kv_destroy (data -> attrs );
@@ -496,16 +503,40 @@ static void sigcont_cb(SignalWatcher *watcher, int signum, void *data)
496503}
497504#endif
498505
499- static void sigwinch_cb (SignalWatcher * watcher ,int signum ,void * data )
506+ static void resize_timer_cb (uv_timer_t * timer )
507+ {
508+ static int counter = 0 ;
509+ counter ++ ;
510+ if (handle_sigwinch (timer -> data )|| counter >=2 ) {
511+ // Don't try again if the size changed.
512+ uv_timer_stop (timer );
513+ }
514+ }
515+
516+ /// Resize and conditionally refresh
517+ ///
518+ /// @return true if the size changed and refresh was scheduled
519+ static bool handle_sigwinch (void * data )
500520{
501521got_winch = true;
502522UI * ui = data ;
503523if (tui_is_stopped (ui )) {
504- return ;
524+ return false ;
505525 }
506526
507- tui_guess_size (ui );
508- ui_schedule_refresh ();
527+ if (tui_guess_size (ui )) {
528+ ui_schedule_refresh ();
529+ return true;
530+ }else {
531+ // tui_grid_resize() won't be called, take care of resetting got_winch
532+ got_winch = false;
533+ return false;
534+ }
535+ }
536+
537+ static void sigwinch_cb (SignalWatcher * watcher ,int signum ,void * data )
538+ {
539+ handle_sigwinch (data );
509540}
510541
511542static bool attrs_differ (UI * ui ,int id1 ,int id2 ,bool rgb )
@@ -935,10 +966,17 @@ static void reset_scroll_region(UI *ui, bool fullwidth)
935966
936967static void tui_grid_resize (UI * ui ,Integer g ,Integer width ,Integer height )
937968{
969+ static bool did_first_resize = true;
938970TUIData * data = ui -> data ;
939971UGrid * grid = & data -> grid ;
940972ugrid_resize (grid , (int )width , (int )height );
941973
974+ // When the UI starts, it might not be possible to resize correctly #15044
975+ if (did_first_resize ) {
976+ did_first_resize = false;
977+ uv_timer_start (& data -> initial_resize_timer ,resize_timer_cb ,500 ,500 );
978+ }
979+
942980xfree (data -> space_buf );
943981data -> space_buf = xmalloc ((size_t )width * sizeof (* data -> space_buf ));
944982memset (data -> space_buf ,' ' , (size_t )width );
@@ -1467,9 +1505,14 @@ static void invalidate(UI *ui, int top, int bot, int left, int right)
14671505
14681506/// Tries to get the user's wanted dimensions (columns and rows) for the entire
14691507/// application (i.e., the host terminal).
1470- static void tui_guess_size (UI * ui )
1508+ ///
1509+ /// @param ui The UI.
1510+ /// @return true if the dimensions changed
1511+ static bool tui_guess_size (UI * ui )
14711512{
14721513TUIData * data = ui -> data ;
1514+ int pre_width = ui -> width ;
1515+ int pre_height = ui -> height ;
14731516int width = 0 ,height = 0 ;
14741517
14751518// 1 - look for non-default 'columns' and 'lines' options during startup
@@ -1511,6 +1554,10 @@ static void tui_guess_size(UI *ui)
15111554
15121555data -> bridge -> bridge .width = ui -> width = width ;
15131556data -> bridge -> bridge .height = ui -> height = height ;
1557+ if (width != pre_width || height != pre_height ) {
1558+ return true;
1559+ }
1560+ return false;
15141561}
15151562
15161563static void unibi_goto (UI * ui ,int row ,int col )