--- lbbs/src/screen.c 2025/05/31 14:59:18 1.86 +++ lbbs/src/screen.c 2025/06/12 03:14:23 1.95 @@ -20,6 +20,7 @@ #include "str_process.h" #include "log.h" #include "io.h" +#include "editor.h" #include "file_loader.h" #include #include @@ -231,23 +232,25 @@ 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 eof_exit, +int display_data(const void *p_data, long display_line_total, const long *p_line_offsets, int eof_exit, display_data_key_handler key_handler, const char *help_filename) { static int show_help = 1; char buffer[LINE_BUFFER_LEN]; DISPLAY_CTX ctx; int ch = 0; - int input_ok, line, max_lines; + int input_ok; + const int screen_begin_row = 1; + const int screen_row_total = SCREEN_ROWS - screen_begin_row; + int output_current_row = screen_begin_row; + int output_end_row = SCREEN_ROWS - 1; long int line_current = 0; long int len; long int percentile; int loop; int eol, display_len; - clrline(begin_line, SCREEN_ROWS); - line = begin_line; - max_lines = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); // update msg_ext with extended key handler if (key_handler(&ch, &ctx) != 0) @@ -258,7 +261,7 @@ int display_data(const void *p_data, lon loop = 1; while (!SYS_server_exit && loop) { - if (eof_exit > 0 && line_current >= line_total && line_total <= SCREEN_ROWS - 2) + if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total) { if (eof_exit == 1) { @@ -273,13 +276,13 @@ int display_data(const void *p_data, lon break; } - if (line_current >= line_total || line >= max_lines) + if (line_current >= display_line_total || output_current_row > output_end_row) { - ctx.reach_begin = (line_current < line ? 1 : 0); + ctx.reach_begin = (line_current < output_current_row ? 1 : 0); - if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) + if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total) { - percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; + percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total; ctx.reach_end = 0; } else @@ -288,8 +291,8 @@ int display_data(const void *p_data, lon 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); + ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1; + ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_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", @@ -328,78 +331,78 @@ int display_data(const void *p_data, lon case KEY_TIMEOUT: goto cleanup; case KEY_HOME: - if (line_current - line < 0) // Reach begin + if (line_current - output_current_row < 0) // Reach begin { break; } line_current = 0; - line = begin_line; - max_lines = SCREEN_ROWS - 1; - clrline(begin_line, SCREEN_ROWS); + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); break; case KEY_END: - if (line_total < SCREEN_ROWS - 2) + if (display_line_total < screen_row_total) { break; } - line_current = line_total - (SCREEN_ROWS - 2); - line = begin_line; - max_lines = SCREEN_ROWS - 1; - clrline(begin_line, SCREEN_ROWS); + line_current = display_line_total - screen_row_total; + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); break; case KEY_UP: - if (line_current - line < 0) // Reach begin + if (line_current - output_current_row < 0) // Reach begin { break; } - line_current -= line; - line = begin_line; - // max_lines = begin_line + 1; + line_current -= output_current_row; + output_current_row = screen_begin_row; + // screen_end_line = screen_begin_line; // prints("\033[T"); // Scroll down 1 line - max_lines = SCREEN_ROWS - 1; // Legacy Fterm only works with this line + output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line break; case CR: igetch_reset(); case KEY_SPACE: case KEY_DOWN: - if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach end + if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end { break; } - line_current += ((SCREEN_ROWS - 2) - (line - 1)); - line = SCREEN_ROWS - 2; - max_lines = SCREEN_ROWS - 1; + line_current += (screen_row_total - (output_current_row - screen_begin_row)); + output_current_row = screen_row_total; + output_end_row = SCREEN_ROWS - 1; moveto(SCREEN_ROWS, 0); clrtoeol(); prints("\033[S"); // Scroll up 1 line break; case KEY_PGUP: - if (line_current - line < 0) // Reach begin + if (line_current - output_current_row < 0) // Reach begin { break; } - line_current -= ((SCREEN_ROWS - 3) + (line - 1)); + line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row)); if (line_current < 0) { line_current = 0; } - line = begin_line; - max_lines = SCREEN_ROWS - 1; - clrline(begin_line, SCREEN_ROWS); + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); break; case KEY_PGDN: - if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach end + if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end { break; } - line_current += (SCREEN_ROWS - 3) - (line - 1); - if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display + line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row); + if (line_current + screen_row_total > display_line_total) // No enough lines to display { - line_current = line_total - (SCREEN_ROWS - 2); + line_current = display_line_total - screen_row_total; } - line = begin_line; - max_lines = SCREEN_ROWS - 1; - clrline(begin_line, SCREEN_ROWS); + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); break; case KEY_ESC: case KEY_LEFT: @@ -410,17 +413,45 @@ int display_data(const void *p_data, lon { break; } - // Display help information show_help = 0; - display_file(help_filename, begin_line, 1); + display_file(help_filename, 1); show_help = 1; case KEY_F5: // Refresh after display help information - line_current -= (line - 1); - line = begin_line; - max_lines = SCREEN_ROWS - 1; - clrline(begin_line, SCREEN_ROWS); + line_current -= (output_current_row - screen_begin_row); + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); + break; + case KEY_F2: // For test only + EDITOR_DATA *p_editor_data; + size_t data_new_len = strlen(p_data) + LINE_BUFFER_LEN; + + char *p_data_new = malloc(data_new_len); + if (p_data_new == NULL) + { + break; + } + p_editor_data = editor_data_load(p_data); + if (p_editor_data == NULL) + { + free(p_data_new); + break; + } + + editor_display(p_editor_data); + editor_data_save(p_editor_data, p_data_new, data_new_len); + editor_data_cleanup(p_editor_data); + p_editor_data = NULL; + free(p_data_new); + p_data_new = NULL; + + // Refresh after display editor + line_current -= (output_current_row - screen_begin_row); + output_current_row = screen_begin_row; + output_end_row = SCREEN_ROWS - 1; + clrline(output_current_row, SCREEN_ROWS); break; case 0: // Refresh bottom line break; @@ -436,21 +467,27 @@ 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) + if (len >= sizeof(buffer)) { - 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; + len = sizeof(buffer) - 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'; - moveto(line, 0); + moveto(output_current_row, 0); clrtoeol(); prints("%s", buffer); line_current++; - line++; + output_current_row++; } cleanup: @@ -472,7 +509,7 @@ static int display_file_key_handler(int return 0; } -int display_file(const char *filename, int begin_line, int eof_exit) +int display_file(const char *filename, int eof_exit) { int ret; const void *p_shm; @@ -487,7 +524,7 @@ int display_file(const char *filename, i return KEY_NULL; } - ret = display_data(p_data, line_total, p_line_offsets, begin_line, eof_exit, display_file_key_handler, DATA_READ_HELP); + ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP); if (detach_file_shm(p_shm) < 0) { @@ -541,6 +578,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)); @@ -558,13 +596,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; }