From 61ce077ea0b68c8e55404b6f7ee8a4e67568e91e Mon Sep 17 00:00:00 2001 From: Matuush Date: Sun, 20 Apr 2025 11:56:02 +0200 Subject: [PATCH] Enhance jumping --- main.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index c231b29..636039d 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,18 @@ struct position { position operator+(count_type offset) { return {r + offset, c}; } }; +// Utilities +count_type min(count_type a, count_type b) { + if(a <= b) + return a; + return b; +} +count_type max(count_type a, count_type b) { + if(a >= b) + return a; + return b; +} + // Global variables treap file; position cur = {0, 0}; @@ -233,17 +245,9 @@ void move_cursor(char ch) { } // Jump to line void jump(count_type r) { - count_type last_start = file.size()-LINES; - if(last_start < 0) - last_start = 0; - cur.r = 0; - file_offset = r; - if(file_offset > last_start) { - file_offset = last_start; - cur.r = r - file_offset; - if(cur.r >= LINES) - cur.r = LINES-1; - } + if(r - file_offset > LINES || r < file_offset) + file_offset = max(0, min(file.size()-LINES, r)); + cur.r = max(0, r - file_offset); jump_line_end(); } // Jump to position