| 40 |
#define STR_TOP_MIDDLE_MAX_LEN 20 |
#define STR_TOP_MIDDLE_MAX_LEN 20 |
| 41 |
#define STR_TOP_RIGHT_MAX_LEN 20 |
#define STR_TOP_RIGHT_MAX_LEN 20 |
| 42 |
|
|
|
#define MSG_EXT_MAX_LEN 200 |
|
|
|
|
| 43 |
void moveto(int row, int col) |
void moveto(int row, int col) |
| 44 |
{ |
{ |
| 45 |
if (row >= 0) |
if (row >= 0) |
| 236 |
{ |
{ |
| 237 |
static int show_help = 1; |
static int show_help = 1; |
| 238 |
char buffer[LINE_BUFFER_LEN]; |
char buffer[LINE_BUFFER_LEN]; |
| 239 |
char msg_ext[MSG_EXT_MAX_LEN]; |
DISPLAY_CTX ctx; |
| 240 |
int ch = 0; |
int ch = 0; |
| 241 |
int input_ok, line, max_lines; |
int input_ok, line, max_lines; |
| 242 |
long int line_current = 0; |
long int line_current = 0; |
| 250 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 251 |
|
|
| 252 |
// update msg_ext with extended key handler |
// update msg_ext with extended key handler |
| 253 |
if (key_handler(&ch, msg_ext, sizeof(msg_ext)) != 0) |
if (key_handler(&ch, &ctx) != 0) |
| 254 |
{ |
{ |
| 255 |
return ch; |
return ch; |
| 256 |
} |
} |
| 275 |
|
|
| 276 |
if (line_current >= line_total || line >= max_lines) |
if (line_current >= line_total || line >= max_lines) |
| 277 |
{ |
{ |
| 278 |
|
ctx.reach_begin = (line_current < line ? 1 : 0); |
| 279 |
|
|
| 280 |
if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) |
if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) |
| 281 |
{ |
{ |
| 282 |
percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; |
percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; |
| 283 |
|
ctx.reach_end = 0; |
| 284 |
} |
} |
| 285 |
else |
else |
| 286 |
{ |
{ |
| 287 |
percentile = 100; |
percentile = 100; |
| 288 |
|
ctx.reach_end = 1; |
| 289 |
} |
} |
| 290 |
|
|
| 291 |
|
ctx.line_top = line_current - (line - 1) + 1; |
| 292 |
|
ctx.line_bottom = MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total); |
| 293 |
|
|
| 294 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 295 |
"\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s", |
"\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s", |
| 296 |
line_current - (line - 1) + 1, |
ctx.line_top, |
| 297 |
MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total), |
ctx.line_bottom, |
| 298 |
percentile, msg_ext); |
percentile, |
| 299 |
|
ctx.msg); |
| 300 |
|
|
| 301 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
| 302 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 317 |
input_ok = 1; |
input_ok = 1; |
| 318 |
|
|
| 319 |
// extended key handler |
// extended key handler |
| 320 |
if (key_handler(&ch, msg_ext, sizeof(msg_ext)) != 0) |
if (key_handler(&ch, &ctx) != 0) |
| 321 |
{ |
{ |
| 322 |
goto cleanup; |
goto cleanup; |
| 323 |
} |
} |
| 328 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 329 |
goto cleanup; |
goto cleanup; |
| 330 |
case KEY_HOME: |
case KEY_HOME: |
| 331 |
|
if (line_current - line < 0) // Reach begin |
| 332 |
|
{ |
| 333 |
|
break; |
| 334 |
|
} |
| 335 |
line_current = 0; |
line_current = 0; |
| 336 |
line = begin_line; |
line = begin_line; |
| 337 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 338 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 339 |
break; |
break; |
| 340 |
case KEY_END: |
case KEY_END: |
| 341 |
line_current = line_total - (SCREEN_ROWS - 2); |
if (line_total < SCREEN_ROWS - 2) |
|
if (line_current < 0) |
|
| 342 |
{ |
{ |
| 343 |
line_current = 0; |
break; |
| 344 |
} |
} |
| 345 |
|
line_current = line_total - (SCREEN_ROWS - 2); |
| 346 |
line = begin_line; |
line = begin_line; |
| 347 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 348 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 349 |
break; |
break; |
| 350 |
case KEY_UP: |
case KEY_UP: |
| 351 |
if (line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach begin |
| 352 |
{ |
{ |
| 353 |
break; |
break; |
| 354 |
} |
} |
| 362 |
igetch_reset(); |
igetch_reset(); |
| 363 |
case KEY_SPACE: |
case KEY_SPACE: |
| 364 |
case KEY_DOWN: |
case KEY_DOWN: |
| 365 |
if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom |
if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach end |
| 366 |
{ |
{ |
| 367 |
break; |
break; |
| 368 |
} |
} |
| 374 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 375 |
break; |
break; |
| 376 |
case KEY_PGUP: |
case KEY_PGUP: |
| 377 |
if (line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach begin |
| 378 |
{ |
{ |
| 379 |
break; |
break; |
| 380 |
} |
} |
| 388 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 389 |
break; |
break; |
| 390 |
case KEY_PGDN: |
case KEY_PGDN: |
| 391 |
if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom |
if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach end |
| 392 |
{ |
{ |
| 393 |
break; |
break; |
| 394 |
} |
} |
| 438 |
len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; |
len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; |
| 439 |
if (len >= LINE_BUFFER_LEN) |
if (len >= LINE_BUFFER_LEN) |
| 440 |
{ |
{ |
| 441 |
log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n", |
log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n", |
| 442 |
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
| 443 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 444 |
} |
} |
| 445 |
|
else if (len < 0) |
| 446 |
|
{ |
| 447 |
|
log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n", |
| 448 |
|
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
| 449 |
|
len = 0; |
| 450 |
|
} |
| 451 |
|
|
| 452 |
memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); |
memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); |
| 453 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 463 |
return ch; |
return ch; |
| 464 |
} |
} |
| 465 |
|
|
| 466 |
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) |
| 467 |
{ |
{ |
| 468 |
switch (*key) |
switch (*p_key) |
| 469 |
{ |
{ |
| 470 |
case 0: // Set msg |
case 0: // Set msg |
| 471 |
snprintf(msg, msg_len, |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 472 |
"| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " |
"| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | " |
| 473 |
"移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ " |
"移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | " |
| 474 |
"帮助[\033[32mh\033[33m] |"); |
"帮助[\033[32mh\033[33m] |"); |
| 475 |
break; |
break; |
|
case 'H': |
|
|
*key = 'h'; |
|
|
return 0; |
|
| 476 |
} |
} |
| 477 |
|
|
| 478 |
return 0; |
return 0; |