--- lbbs/src/screen.c 2025/05/30 05:28:41 1.81 +++ lbbs/src/screen.c 2025/05/31 03:37:47 1.85 @@ -40,8 +40,6 @@ #define STR_TOP_MIDDLE_MAX_LEN 20 #define STR_TOP_RIGHT_MAX_LEN 20 -#define MSG_EXT_MAX_LEN 200 - void moveto(int row, int col) { if (row >= 0) @@ -233,12 +231,12 @@ int get_data(int row, int col, char *pro return len; } -int display_data(const void *p_data, long line_total, const long *p_line_offsets, int begin_line, int wait, +int display_data(const void *p_data, long line_total, const long *p_line_offsets, int begin_line, int eof_exit, display_data_key_handler key_handler, const char *help_filename) { static int show_help = 1; char buffer[LINE_BUFFER_LEN]; - char msg_ext[MSG_EXT_MAX_LEN]; + DISPLAY_CTX ctx; int ch = 0; int input_ok, line, max_lines; long int line_current = 0; @@ -252,7 +250,7 @@ int display_data(const void *p_data, lon max_lines = SCREEN_ROWS - 1; // update msg_ext with extended key handler - if (key_handler(&ch, msg_ext, sizeof(msg_ext)) != 0) + if (key_handler(&ch, &ctx) != 0) { return ch; } @@ -260,13 +258,13 @@ int display_data(const void *p_data, lon loop = 1; while (!SYS_server_exit && loop) { - if (line_current >= line_total && line_total <= SCREEN_ROWS - 2) + if (eof_exit > 0 && line_current >= line_total && line_total <= SCREEN_ROWS - 2) { - if (wait) + if (eof_exit == 1) { ch = press_any_key(); } - else + else // if (eof_exit == 2) { iflush(); } @@ -277,20 +275,28 @@ int display_data(const void *p_data, lon if (line_current >= line_total || line >= max_lines) { + ctx.reach_begin = (line_current < line ? 1 : 0); + if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) { percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; + ctx.reach_end = 0; } else { percentile = 100; + ctx.reach_end = 1; } + ctx.line_top = line_current - (line - 1) + 1; + ctx.line_bottom = MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total); + snprintf(buffer, sizeof(buffer), "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s", - line_current - (line - 1) + 1, - MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total), - percentile, msg_ext); + ctx.line_top, + ctx.line_bottom, + percentile, + ctx.msg); len = split_line(buffer, SCREEN_COLS, &eol, &display_len); for (; display_len < SCREEN_COLS; display_len++) @@ -311,7 +317,7 @@ int display_data(const void *p_data, lon input_ok = 1; // extended key handler - if (key_handler(&ch, msg_ext, sizeof(msg_ext)) != 0) + if (key_handler(&ch, &ctx) != 0) { goto cleanup; } @@ -322,19 +328,27 @@ int display_data(const void *p_data, lon case KEY_TIMEOUT: goto cleanup; case KEY_HOME: + if (line_current - line < 0) // Reach begin + { + break; + } line_current = 0; line = begin_line; max_lines = SCREEN_ROWS - 1; clrline(begin_line, SCREEN_ROWS); break; case KEY_END: + if (line_total < SCREEN_ROWS - 2) + { + break; + } line_current = line_total - (SCREEN_ROWS - 2); line = begin_line; max_lines = SCREEN_ROWS - 1; clrline(begin_line, SCREEN_ROWS); break; case KEY_UP: - if (line_current - line < 0) // Reach top + if (line_current - line < 0) // Reach begin { break; } @@ -348,7 +362,7 @@ int display_data(const void *p_data, lon igetch_reset(); case KEY_SPACE: case KEY_DOWN: - if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom + if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach end { break; } @@ -360,7 +374,7 @@ int display_data(const void *p_data, lon prints("\033[S"); // Scroll up 1 line break; case KEY_PGUP: - if (line_current - line < 0) // Reach top + if (line_current - line < 0) // Reach begin { break; } @@ -374,7 +388,7 @@ int display_data(const void *p_data, lon clrline(begin_line, SCREEN_ROWS); break; case KEY_PGDN: - if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom + if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach end { break; } @@ -443,46 +457,22 @@ cleanup: return ch; } -static int display_file_key_handler(int *key, char *msg, size_t msg_len) +static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx) { - static int topic_view = 0; - - switch (*key) + switch (*p_key) { case 0: // Set msg - snprintf(msg, msg_len, + snprintf(p_ctx->msg, sizeof(p_ctx->msg), "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ " "帮助[\033[32mh\033[33m] |"); break; - case 'p': - break; - topic_view = !topic_view; - if (topic_view) - { - snprintf(msg, msg_len, - "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " - "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ " - "帮助[\033[32mh\033[33m] |"); - } - else - { - snprintf(msg, msg_len, - "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " - "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ " - "帮助[\033[32mh\033[33m] |"); - } - *key = 0; - break; - case 'H': - *key = 'h'; - return 0; } return 0; } -int display_file(const char *filename, int begin_line, int wait) +int display_file(const char *filename, int begin_line, int eof_exit) { int ret; const void *p_shm; @@ -497,7 +487,7 @@ int display_file(const char *filename, i return KEY_NULL; } - ret = display_data(p_data, line_total, p_line_offsets, begin_line, wait, display_file_key_handler, DATA_READ_HELP); + ret = display_data(p_data, line_total, p_line_offsets, begin_line, eof_exit, display_file_key_handler, DATA_READ_HELP); if (detach_file_shm(p_shm) < 0) {