@@ -119,6 +119,7 @@ use crate::srcref::ns_populate_srcref;
119119use crate :: srcref:: resource_loaded_namespaces;
120120use crate :: startup;
121121use crate :: sys:: console:: console_to_utf8;
122+ use crate :: sys:: interface:: complete_r_init;
122123
123124/// An enum representing the different modes in which the R session can run.
124125#[ derive( PartialEq , Clone ) ]
@@ -455,6 +456,9 @@ impl RMain {
455456 log:: info!( "Sending kernel info: {version}" ) ;
456457 self . kernel_init_tx . broadcast ( kernel_info) ;
457458
459+ // Set post-init REPL handlers like `write_console()`
460+ complete_r_init ( ) ;
461+
458462 // Thread-safe initialisation flag for R
459463 R_INIT . set ( ( ) ) . expect ( "`R_INIT` can only be set once" ) ;
460464 }
@@ -1318,12 +1322,6 @@ This is a Positron limitation we plan to fix. In the meantime, you can:
13181322 Stream :: Stderr
13191323 } ;
13201324
1321- if !RMain :: is_initialized ( ) {
1322- // During init, consider all output to be part of the startup banner
1323- self . banner_output . push_str ( & content) ;
1324- return ;
1325- }
1326-
13271325 // If active execution request is silent don't broadcast
13281326 // any output
13291327 if let Some ( ref req) = self . active_request {
@@ -1354,6 +1352,18 @@ This is a Positron limitation we plan to fix. In the meantime, you can:
13541352 self . iopub_tx . send ( message) . unwrap ( ) ;
13551353 }
13561354
1355+ /// Invoked by R to write output to the console.
1356+ /// Only used during initialization to capture the startup banner.
1357+ fn write_console_init ( & mut self , buf : * const c_char ) {
1358+ let content = match console_to_utf8 ( buf) {
1359+ Ok ( content) => content,
1360+ Err ( err) => panic ! ( "Failed to read from R buffer: {err:?}" ) ,
1361+ } ;
1362+
1363+ // During init, consider all output to be part of the startup banner
1364+ self . banner_output . push_str ( & content) ;
1365+ }
1366+
13571367 /// Invoked by R to change busy state
13581368 fn busy ( & mut self , which : i32 ) {
13591369 // Ensure signal handlers are initialized.
@@ -1679,6 +1689,12 @@ pub extern "C" fn r_write_console(buf: *const c_char, buflen: i32, otype: i32) {
16791689 main. write_console ( buf, buflen, otype) ;
16801690}
16811691
1692+ #[ no_mangle]
1693+ pub extern "C" fn r_write_console_init ( buf : * const c_char , _buflen : i32 , _otype : i32 ) {
1694+ let main = RMain :: get_mut ( ) ;
1695+ main. write_console_init ( buf) ;
1696+ }
1697+
16821698#[ no_mangle]
16831699pub extern "C" fn r_show_message ( buf : * const c_char ) {
16841700 let main = RMain :: get ( ) ;
0 commit comments