@@ -162,6 +162,7 @@ static void text_set_cursor_pos(struct flanterm_context *_ctx, size_t x, size_t
162162 }
163163 }
164164 ctx -> cursor_offset = y * VD_COLS + x * 2 ;
165+ ctx -> cursor_overflow = false;
165166}
166167
167168static uint8_t ansi_colours [] = { 0 , 4 , 2 , 6 , 1 , 5 , 3 , 7 };
@@ -219,17 +220,32 @@ static void text_set_text_bg_default_bright(struct flanterm_context *_ctx) {
219220static void text_putchar (struct flanterm_context * _ctx , uint8_t c ) {
220221 struct textmode_context * ctx = (void * )_ctx ;
221222
222- ctx -> back_buffer [ ctx -> cursor_offset ] = c ;
223- ctx -> back_buffer [ ctx -> cursor_offset + 1 ] = ctx -> text_palette ;
224- if ( ctx -> cursor_offset / VD_COLS == _ctx -> scroll_bottom_margin - 1
225- && ctx -> cursor_offset % VD_COLS == VD_COLS - 2 ) {
226- if ( _ctx -> scroll_enabled ) {
227- text_scroll ( _ctx );
223+ // Handle overflow from previous putchar
224+ if ( ctx -> cursor_overflow ) {
225+ ctx -> cursor_overflow = false;
226+ if ( _ctx -> wrap_enabled
227+ && ( ctx -> cursor_offset / VD_COLS < _ctx -> scroll_bottom_margin - 1
228+ || _ctx -> scroll_enabled )) {
228229 ctx -> cursor_offset -= ctx -> cursor_offset % VD_COLS ;
230+ ctx -> cursor_offset += VD_COLS ;
231+ if (ctx -> cursor_offset / VD_COLS == _ctx -> scroll_bottom_margin ) {
232+ ctx -> cursor_offset -= VD_COLS ;
233+ text_scroll (_ctx );
234+ }
235+ if (ctx -> cursor_offset >= VD_ROWS * VD_COLS ) {
236+ ctx -> cursor_offset = (VD_ROWS - 1 ) * VD_COLS ;
237+ }
238+ } else {
239+ ctx -> cursor_offset = ctx -> cursor_offset - (ctx -> cursor_offset % VD_COLS ) + VD_COLS - 2 ;
229240 }
230- } else if (ctx -> cursor_offset >= (VIDEO_BOTTOM - 1 )) {
231- ctx -> cursor_offset -= ctx -> cursor_offset % VD_COLS ;
232- } else {
241+ }
242+
243+ ctx -> back_buffer [ctx -> cursor_offset ] = c ;
244+ ctx -> back_buffer [ctx -> cursor_offset + 1 ] = ctx -> text_palette ;
245+ if (ctx -> cursor_offset % VD_COLS == VD_COLS - 2 ) {
246+ // At last column - flag overflow for next putchar
247+ ctx -> cursor_overflow = true;
248+ } else if (ctx -> cursor_offset < (VIDEO_BOTTOM - 1 )) {
233249 ctx -> cursor_offset += 2 ;
234250 }
235251}
@@ -285,6 +301,7 @@ void vga_textmode_init(bool managed) {
285301 }
286302
287303 ctx -> cursor_offset = 0 ;
304+ ctx -> cursor_overflow = false;
288305 ctx -> text_palette = 0x07 ;
289306
290307 ctx -> video_mem = (volatile uint8_t * )0xb8000 ;
0 commit comments