/[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.22 by sysadm, Tue Apr 29 03:39:04 2025 UTC Revision 1.28 by sysadm, Sat May 3 13:41:21 2025 UTC
# Line 17  Line 17 
17    
18  #include "bbs.h"  #include "bbs.h"
19  #include "common.h"  #include "common.h"
20    #include "str_process.h"
21    #include "log.h"
22  #include "io.h"  #include "io.h"
23    #include "screen.h"
24    #include <string.h>
25    #include <ctype.h>
26  #include <sys/types.h>  #include <sys/types.h>
27  #include <sys/stat.h>  #include <sys/stat.h>
28  #include <unistd.h>  #include <unistd.h>
29    #include <stdlib.h>
30    
31  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
32    
33  int screen_lines = 24;  int screen_rows = 24;
34    int screen_cols = 80;
35    
36  void moveto(int row, int col)  void moveto(int row, int col)
37  {  {
# Line 36  void moveto(int row, int col) Line 43  void moveto(int row, int col)
43          {          {
44                  prints("\r");                  prints("\r");
45          }          }
         iflush();  
46  }  }
47    
48  void clrtoeol()  void clrtoeol()
49  {  {
50          prints("\033[K");          prints("\033[K");
         iflush();  
51  }  }
52    
53  void clrline(int line_begin, int line_end)  void clrline(int line_begin, int line_end)
# Line 54  void clrline(int line_begin, int line_en Line 59  void clrline(int line_begin, int line_en
59                  moveto(i, 0);                  moveto(i, 0);
60                  prints("\033[K");                  prints("\033[K");
61          }          }
   
         iflush();  
62  }  }
63    
64  void clrtobot(int line_begin)  void clrtobot(int line_begin)
65  {  {
66          clrline(line_begin, screen_lines);          moveto(line_begin, 0);
67            prints("\033[J");
68          moveto(line_begin, 0);          moveto(line_begin, 0);
69  }  }
70    
71  void clearscr()  void clearscr()
72  {  {
73          prints("\33[2J");          prints("\033[2J");
74          moveto(0, 0);          moveto(0, 0);
         iflush();  
75  }  }
76    
77  int press_any_key()  int press_any_key()
78  {  {
79          moveto(screen_lines, 0);          moveto(screen_rows, 0);
80          clrtoeol();          clrtoeol();
81    
82          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
# Line 94  void set_input_echo(int echo) Line 97  void set_input_echo(int echo)
97                  //    outc ('\x85'); // ASCII code 133                  //    outc ('\x85'); // ASCII code 133
98                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
99                  iflush();                  iflush();
100                  igetch_t(60);                  igetch(0);
101                  igetch_t(60);                  igetch(1);
102          }          }
103  }  }
104    
105  int _str_input(char *buffer, int buffer_length, int echo_mode)  static int _str_input(char *buffer, int buffer_length, int echo_mode)
106  {  {
107          char buf[256], ch;          char buf[256], ch;
108          int c, offset = 0, len, loop = 1, i, hz = 0;          int c, offset = 0, len, loop = 1, i, hz = 0;
# Line 113  int _str_input(char *buffer, int buffer_ Line 116  int _str_input(char *buffer, int buffer_
116          {          {
117                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
118                  {                  {
119                            igetch(1); // Cleanup remaining '\n' in the buffer
120                          break;                          break;
121                  }                  }
122                  if (c == LF)                  if (c == LF)
# Line 182  int get_data(int row, int col, char *pro Line 186  int get_data(int row, int col, char *pro
186          int len;          int len;
187    
188          moveto(row, col);          moveto(row, col);
         iflush();  
189          prints(prompt);          prints(prompt);
190          prints(buffer);          prints(buffer);
191          iflush();          iflush();
# Line 194  int get_data(int row, int col, char *pro Line 197  int get_data(int row, int col, char *pro
197    
198  int display_file(const char *filename)  int display_file(const char *filename)
199  {  {
200          char buffer[260];          char buffer[LINE_BUFFER_LEN];
201          FILE *fin;          FILE *fin;
202          int i;          int i;
203    
# Line 203  int display_file(const char *filename) Line 206  int display_file(const char *filename)
206                  return -1;                  return -1;
207          }          }
208    
209          while (fgets(buffer, 255, fin))          while (fgets(buffer, sizeof(buffer) - 1, fin))
210          {          {
211                  i = strlen(buffer);                  i = strnlen(buffer, sizeof(buffer) - 1);
212                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
213                  {                  {
214                          buffer[i - 1] = '\r';                          buffer[i - 1] = '\r';
# Line 222  int display_file(const char *filename) Line 225  int display_file(const char *filename)
225    
226  int display_file_ex(const char *filename, int begin_line, int wait)  int display_file_ex(const char *filename, int begin_line, int wait)
227  {  {
228          char buffer[260], temp[256];          char buffer[LINE_BUFFER_LEN];
229          int i, ch, input_ok, max_lines;          char temp[LINE_BUFFER_LEN];
230          long int line, c_line_begin = 0, c_line_total = 0;          int ch = 0;
231          long int f_line, f_size, f_offset;          int input_ok, line, max_lines;
232            long int c_line_current = 0, c_line_total = 0;
233          FILE *fin;          FILE *fin;
234          struct stat f_stat;          struct stat f_stat;
235            long *p_line_offsets;
236            int len;
237            int percentile;
238            int loop = 1;
239    
240            if ((fin = fopen(filename, "r")) == NULL)
241            {
242                    log_error("Unable to open file %s\n", filename);
243                    return -1;
244            }
245    
246            p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
247    
248            c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
249    
250          max_lines = screen_lines - 1;          clrline(begin_line, screen_rows);
         clrline(begin_line, screen_lines);  
251          line = begin_line;          line = begin_line;
252          moveto(line, 0);          max_lines = screen_rows - 1;
253    
254          if ((fin = fopen(filename, "r")) != NULL)          while (loop)
255          {          {
256                  if (fstat(fileno(fin), &f_stat) != 0)                  if (c_line_current >= c_line_total)
257                  {                  {
258                          log_error("Get file stat failed\n");                          if (wait)
259                          return -1;                          {
260                  }                                  ch = press_any_key();
261                  f_size = f_stat.st_size;                          }
262                            else
263                            {
264                                    iflush();
265                            }
266    
267                  while (fgets(buffer, 255, fin))                          loop = 0;
268                          c_line_total++;                          break;
269                  rewind(fin);                  }
270    
271                  while (fgets(buffer, 255, fin))                  if (line >= max_lines)
272                  {                  {
273                          if (line >= max_lines)                          if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
274                          {                          {
275                                  f_offset = ftell(fin);                                  percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
276                            }
277                            else
278                            {
279                                    log_error("P100 reached\n");
280                                    percentile = 100;
281                            }
282    
283                                  moveto(screen_lines, 0);                          moveto(screen_rows, 0);
284                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
285                                             (f_offset - strlen(buffer)) * 100 / f_size);                                     percentile);
286                                  iflush();                          iflush();
287    
288                                  input_ok = 0;                          input_ok = 0;
289                                  while (!input_ok)                          while (!input_ok)
290                            {
291                                    ch = igetch_t(MAX_DELAY_TIME);
292                                    input_ok = 1;
293                                    switch (ch)
294                                  {                                  {
295                                          ch = igetch_t(MAX_DELAY_TIME);                                  case KEY_UP:
296                                          input_ok = 1;                                          if (c_line_current - line < 0) // Reach top
                                         switch (ch)  
297                                          {                                          {
                                         case KEY_UP:  
                                                 c_line_begin--;  
                                                 if (c_line_begin >= 0)  
                                                 {  
                                                         rewind(fin);  
                                                         for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                         {  
                                                                 if (fgets(buffer, 255, fin) == NULL)  
                                                                         goto exit;  
                                                         }  
                                                 }  
                                                 else  
                                                 {  
                                                         goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_DOWN:  
                                         case CR:  
                                                 c_line_begin++;  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
298                                                  break;                                                  break;
299                                          case KEY_PGUP:                                          }
300                                          case Ctrl('B'):                                          c_line_current -= line;
301                                                  if (c_line_begin > 0)                                          line = begin_line;
302                                                          c_line_begin -= (max_lines - begin_line - 1);                                          max_lines = begin_line + 1;
303                                                  else                                          prints("\033[T"); // Scroll down 1 line
304                                                          goto exit;                                          //max_lines = screen_rows - 1; // Legacy Fterm only works with this line
305                                                  if (c_line_begin < 0)                                          break;
306                                                          c_line_begin = 0;                                  case KEY_DOWN:
307                                                  rewind(fin);                                  case CR:
308                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
309                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_RIGHT:  
                                         case KEY_PGDN:  
                                         case Ctrl('F'):  
                                         case KEY_SPACE:  
                                                 c_line_begin += (max_lines - begin_line - 1);  
                                                 if (c_line_begin + (max_lines - begin_line) >  
                                                         c_line_total)  
                                                         c_line_begin =  
                                                                 c_line_total - (max_lines - begin_line);  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_NULL:  
                                         case KEY_TIMEOUT:  
                                         case KEY_LEFT:  
                                         case 'q':  
                                         case 'Q':  
                                                 goto exit;  
310                                                  break;                                                  break;
311                                          case '?':                                          }
312                                          case 'h':                                          c_line_current += ((screen_rows - 2) - (line - 1));
313                                          case 'H':                                          line = screen_rows - 2;
314                                                  // Display help information                                          max_lines = screen_rows - 1;
315                                                  strcpy(temp, app_home_dir);                                          moveto(screen_rows, 0);
316                                                  strcat(temp, "data/read_help.txt");                                          clrtoeol();
317                                                  display_file_ex(temp, begin_line, 1);                                          prints("\033[S"); // Scroll up 1 line
318                                            break;
319                                                  // Refresh after display help information                                  case KEY_PGUP:
320                                                  rewind(fin);                                  case Ctrl('B'):
321                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current - line < 0) // Reach top
322                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
323                                                  break;                                                  break;
324                                          default:                                          }
325                                                  input_ok = 0;                                          c_line_current -= ((screen_rows - 3) + (line - 1));
326                                            if (c_line_current < 0)
327                                            {
328                                                    c_line_current = 0;
329                                            }
330                                            line = begin_line;
331                                            max_lines = screen_rows - 1;
332                                            clrline(begin_line, screen_rows);
333                                            break;
334                                    case KEY_RIGHT:
335                                    case KEY_PGDN:
336                                    case Ctrl('F'):
337                                    case KEY_SPACE:
338                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
339                                            {
340                                                  break;                                                  break;
341                                          }                                          }
342                                            c_line_current += (screen_rows - 3) - (line - 1);
343                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
344                                            {
345                                                    c_line_current = c_line_total - (screen_rows - 2);
346                                            }
347                                            line = begin_line;
348                                            max_lines = screen_rows - 1;
349                                            clrline(begin_line, screen_rows);
350                                            break;
351                                    case KEY_NULL:
352                                    case KEY_TIMEOUT:
353                                    case KEY_LEFT:
354                                    case 'q':
355                                    case 'Q':
356                                            loop = 0;
357                                            break;
358                                    case '?':
359                                    case 'h':
360                                    case 'H':
361                                            // Display help information
362                                            strcpy(temp, app_home_dir);
363                                            strcat(temp, "data/read_help.txt");
364                                            display_file_ex(temp, begin_line, 1);
365    
366                                            // Refresh after display help information
367                                            c_line_current -= (line - 1);
368                                            line = begin_line;
369                                            max_lines = screen_rows - 1;
370                                            clrline(begin_line, screen_rows);
371                                            break;
372                                    default:
373                                            input_ok = 0;
374                                            break;
375                                  }                                  }
   
                                 clrline(begin_line, screen_lines);  
                                 line = begin_line;  
                                 moveto(line, 0);  
   
                                 continue;  
                         }  
   
                         i = strlen(buffer);  
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
376                          }                          }
                         prints(buffer);  
                         iflush();  
377    
378                          line++;                          continue;
379                  }                  }
                 if (wait)  
                         ch = press_any_key();  
                 else  
                         ch = 0;  
380    
381          exit:                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
382                  fclose(fin);                  len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
383                    if (len >= LINE_BUFFER_LEN)
384                  return ch;                  {
385                            log_error("Error length exceeds buffer size: %d\n", len);
386                            len = LINE_BUFFER_LEN - 1;
387                    }
388                    if (fgets(buffer, len + 1, fin) == NULL)
389                    {
390                            log_error("Reach EOF\n");
391                            break;
392                    }
393                    moveto(line, 0);
394                    clrtoeol();
395                    prints("%s", buffer);
396                    c_line_current++;
397                    line++;
398          }          }
399    
400          return -1;          free(p_line_offsets);
401            fclose(fin);
402    
403            return ch;
404  }  }
405    
406  int show_top(char *status)  int show_top(char *status)
407  {  {
408          char buffer[256];          char buffer[LINE_BUFFER_LEN];
409    
410          str_space(buffer, 20 - strlen(BBS_current_section_name));          str_space(buffer, 20 - strlen(BBS_current_section_name));
411    
412          moveto(1, 0);          moveto(1, 0);
413            clrtoeol();
414          prints("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%-20s \033[37m%20s"
415                     "         %s\033[33m讨论区 [%s]\033[m",                     "         %s\033[33m讨论区 [%s]\033[m",
416                     status, BBS_name, buffer, BBS_current_section_name);                     status, BBS_name, buffer, BBS_current_section_name);
# Line 401  int show_top(char *status) Line 421  int show_top(char *status)
421    
422  int show_bottom(char *msg)  int show_bottom(char *msg)
423  {  {
424          char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
425            char str_time_onine[20];
426            char buffer[LINE_BUFFER_LEN];
427          time_t time_online;          time_t time_online;
428          struct tm *tm_online;          struct tm *tm_online;
429    
430          get_time_str(str_time, 256);          get_time_str(str_time, sizeof(str_time));
431          str_space(buffer, 33 - strlen(BBS_username));          str_space(buffer, 33 - strlen(BBS_username));
432    
433          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
434          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
435    
436          moveto(screen_lines, 0);          moveto(screen_rows, 0);
437            clrtoeol();
438          prints("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
439                     "%s帐号[\033[36m%s\033[33m]"                     "%s帐号[\033[36m%s\033[33m]"
440                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
# Line 424  int show_bottom(char *msg) Line 447  int show_bottom(char *msg)
447    
448  int show_active_board()  int show_active_board()
449  {  {
450          char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
451            char buffer[LINE_BUFFER_LEN];
452          FILE *fin;          FILE *fin;
453          int i, j;          static int line;
454          static long int line;          int len;
455            int end_of_line;
456    
457          sprintf(filename, "%sdata/active_board.txt", app_home_dir);          sprintf(filename, "%sdata/active_board.txt", app_home_dir);
458    
459          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
460    
461          moveto(3, 0);          if ((fin = fopen(filename, "r")) == NULL)
462            {
463                    log_error("Unable to open file %s\n", filename);
464                    return -1;
465            }
466    
467          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < line; i++)
468          {          {
469                  for (j = 0; j < line; j++)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
470                  {                  {
471                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
472                          {                          rewind(fin);
473                                  line = 0;                          break;
                                 rewind(fin);  
                                 break;  
                         }  
474                  }                  }
475            }
476    
477                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
478            {
479                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
480                  {                  {
481                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
482                          {                          break;
                                 line = 0;  
                                 if (j == 0)  
                                 {  
                                         rewind(fin);  
                                         if (fgets(buffer, 255, fin) == NULL)  
                                         {  
                                                 break;  
                                         }  
                                 }  
                                 else  
                                 {  
                                         break;  
                                 }  
                         }  
                         line++;  
                         i = strlen(buffer);  
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
483                  }                  }
484                  fclose(fin);                  line++;
485                    len = split_line(buffer, screen_cols, &end_of_line);
486                    buffer[len] = '\0'; // Truncate over-length line
487                    moveto(3 + i, 0);
488                    prints("%s", buffer);
489          }          }
490            iflush();
491    
492            fclose(fin);
493    
494          return 0;          return 0;
495  }  }


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

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