/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.80 by sysadm, Fri May 30 00:20:53 2025 UTC Revision 1.86 by sysadm, Sat May 31 14:59:18 2025 UTC
# Line 231  int get_data(int row, int col, char *pro Line 231  int get_data(int row, int col, char *pro
231          return len;          return len;
232  }  }
233    
234  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,
235                                     display_data_key_handler key_handler, const char *help_filename)
236  {  {
237          static int show_help = 1;          static int show_help = 1;
238          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
239          int ch = KEY_NULL;          DISPLAY_CTX ctx;
240            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;
243          long int len;          long int len;
# Line 247  int display_data(const void *p_data, lon Line 249  int display_data(const void *p_data, lon
249          line = begin_line;          line = begin_line;
250          max_lines = SCREEN_ROWS - 1;          max_lines = SCREEN_ROWS - 1;
251    
252            // update msg_ext with extended key handler
253            if (key_handler(&ch, &ctx) != 0)
254            {
255                    return ch;
256            }
257    
258          loop = 1;          loop = 1;
259          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
260          {          {
261                  if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)                  if (eof_exit > 0 && line_current >= line_total && line_total <= SCREEN_ROWS - 2)
262                  {                  {
263                          if (wait)                          if (eof_exit == 1)
264                          {                          {
265                                  ch = press_any_key();                                  ch = press_any_key();
266                          }                          }
267                          else                          else // if (eof_exit == 2)
268                          {                          {
269                                  iflush();                                  iflush();
270                          }                          }
# Line 267  int display_data(const void *p_data, lon Line 275  int display_data(const void *p_data, lon
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) │ "                                           "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",
296                                           "返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ 移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "                                           ctx.line_top,
297                                           "帮助[\033[32mh\033[33m] │",                                           ctx.line_bottom,
298                                           line_current - (line - 1) + 1,                                           percentile,
299                                           MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total),                                           ctx.msg);
                                          percentile);  
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++)
303                          {                          {
304                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
305                          }                          }
# Line 301  int display_data(const void *p_data, lon Line 315  int display_data(const void *p_data, lon
315                          {                          {
316                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(MAX_DELAY_TIME);
317                                  input_ok = 1;                                  input_ok = 1;
318    
319                                    // extended key handler
320                                    if (key_handler(&ch, &ctx) != 0)
321                                    {
322                                            goto cleanup;
323                                    }
324    
325                                  switch (ch)                                  switch (ch)
326                                  {                                  {
327                                  case KEY_NULL:                                  case KEY_NULL:
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                                            if (line_total < SCREEN_ROWS - 2)
342                                            {
343                                                    break;
344                                            }
345                                          line_current = line_total - (SCREEN_ROWS - 2);                                          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                                          }                                          }
# Line 333  int display_data(const void *p_data, lon Line 362  int display_data(const void *p_data, lon
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                                          }                                          }
# Line 345  int display_data(const void *p_data, lon Line 374  int display_data(const void *p_data, lon
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                                          }                                          }
# Line 359  int display_data(const void *p_data, lon Line 388  int display_data(const void *p_data, lon
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                                          }                                          }
# Line 377  int display_data(const void *p_data, lon Line 406  int display_data(const void *p_data, lon
406                                          loop = 0;                                          loop = 0;
407                                          break;                                          break;
408                                  case 'h':                                  case 'h':
409                                          if (!show_help)                                          if (!show_help) // Not reentrant
410                                          {                                          {
411                                                  break;                                                  break;
412                                          }                                          }
413    
414                                          // Display help information                                          // Display help information
415                                          show_help = 0;                                          show_help = 0;
416                                          display_file(DATA_READ_HELP, begin_line, 1);                                          display_file(help_filename, begin_line, 1);
417                                          show_help = 1;                                          show_help = 1;
418                                    case KEY_F5:
419                                          // Refresh after display help information                                          // Refresh after display help information
420                                          line_current -= (line - 1);                                          line_current -= (line - 1);
421                                          line = begin_line;                                          line = begin_line;
422                                          max_lines = SCREEN_ROWS - 1;                                          max_lines = SCREEN_ROWS - 1;
423                                          clrline(begin_line, SCREEN_ROWS);                                          clrline(begin_line, SCREEN_ROWS);
424                                          break;                                          break;
425                                    case 0: // Refresh bottom line
426                                            break;
427                                  default:                                  default:
428                                          input_ok = 0;                                          input_ok = 0;
429                                          break;                                          break;
# Line 426  cleanup: Line 457  cleanup:
457          return ch;          return ch;
458  }  }
459    
460  int display_file(const char *filename, int begin_line, int wait)  static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
461  {  {
462          int ch = KEY_NULL;          switch (*p_key)
463            {
464            case 0: // Set msg
465                    snprintf(p_ctx->msg, sizeof(p_ctx->msg),
466                                     "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
467                                     "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
468                                     "帮助[\033[32mh\033[33m] |");
469                    break;
470            }
471    
472            return 0;
473    }
474    
475    int display_file(const char *filename, int begin_line, int eof_exit)
476    {
477            int ret;
478          const void *p_shm;          const void *p_shm;
479          size_t data_len;          size_t data_len;
480          long line_total;          long line_total;
# Line 441  int display_file(const char *filename, i Line 487  int display_file(const char *filename, i
487                  return KEY_NULL;                  return KEY_NULL;
488          }          }
489    
490          ch = display_data(p_data, line_total, p_line_offsets, begin_line, wait);          ret = display_data(p_data, line_total, p_line_offsets, begin_line, eof_exit, display_file_key_handler, DATA_READ_HELP);
491    
492          if (detach_file_shm(p_shm) < 0)          if (detach_file_shm(p_shm) < 0)
493          {          {
494                  log_error("detach_file_shm(%s) error\n", filename);                  log_error("detach_file_shm(%s) error\n", filename);
495          }          }
496    
497          return ch;          return ret;
498  }  }
499    
500  int show_top(const char *str_left, const char *str_middle, const char *str_right)  int show_top(const char *str_left, const char *str_middle, const char *str_right)


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1