--- lbbs/src/screen.c 2025/05/30 05:28:41 1.81 +++ lbbs/src/screen.c 2025/06/05 11:48:35 1.88 @@ -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; } @@ -424,10 +438,16 @@ int display_data(const void *p_data, lon len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; if (len >= LINE_BUFFER_LEN) { - log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n", + log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n", len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); len = LINE_BUFFER_LEN - 1; } + else if (len < 0) + { + log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n", + len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); + len = 0; + } memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); buffer[len] = '\0'; @@ -443,46 +463,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, - "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " - "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ " + 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 +493,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) { @@ -551,6 +547,7 @@ int show_bottom(const char *msg) int msg_len; int len; int len_username; + char str_tm_online[LINE_BUFFER_LEN]; get_time_str(str_time, sizeof(str_time)); @@ -568,13 +565,23 @@ int show_bottom(const char *msg) time_online = time(0) - BBS_login_tm; tm_online = gmtime(&time_online); + if (tm_online->tm_mday > 1) + { + snprintf(str_tm_online, sizeof(str_tm_online), + "\033[36m%2d\033[33m天\033[36m%2d\033[33m时", + tm_online->tm_mday - 1, tm_online->tm_hour); + } + else + { + snprintf(str_tm_online, sizeof(str_tm_online), + "\033[36m%2d\033[33m时\033[36m%2d\033[33m分", + tm_online->tm_hour, tm_online->tm_min); + } moveto(SCREEN_ROWS, 0); clrtoeol(); - prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m]" - "[\033[36m%1d\033[33m天\033[36m%2d\033[33m时\033[36m%2d\033[33m分]\033[m", - str_time, msg_f, 35 - msg_len - len_username, "", BBS_username, - tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min); + prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m", + str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online); return 0; }