From 8d9cd9a77790b43e86ac7df749af9dccba99b704 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sat, 4 Oct 2025 21:37:40 +0100 Subject: [PATCH] [Fix] Correct terminal auto-scroll --- client/src/terminal.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/client/src/terminal.cpp b/client/src/terminal.cpp index 9635b18..0e33a2e 100644 --- a/client/src/terminal.cpp +++ b/client/src/terminal.cpp @@ -75,16 +75,6 @@ void Terminal::_on_ret_press(void) { _history.push_back(_prompt + "> " + command); _network->send(command); - - /* TODO: Ugly hack. Refactor to pass window height - * We need the window height to know if we should - * auto-scroll, but we don't have it here. - * Assume the height of 500 for now. - */ - int visible_lines = (500-5.0f)/20.0f; /* Subtract padding. */ - if((int)_history.size()+1 > visible_lines) { - _scroll_offset = (_history.size()+1) - visible_lines+1; - } } void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y) { @@ -117,6 +107,15 @@ void Terminal::render(const RenderContext& context, int x, int y_screen, int y_g float line_height = 20.0f; float padding = 5.0f; + /* + * Auto-scroll to bottom if the user has not manually scrolled up. + * Ensures input line is always visible after submitting a command. + */ + int visible_lines = (height-padding) / line_height; + int max_scroll_offset = (_history.size() + 1) - visible_lines; + if(max_scroll_offset < 0) max_scroll_offset = 0; + if(_scroll_offset >= max_scroll_offset - 1) _scroll_offset = max_scroll_offset; + context.ui_renderer->begin_text(); /* Enable scissor test to clip rendering to the window content area. */