/[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.26 by sysadm, Sat May 3 06:24:54 2025 UTC Revision 1.34 by sysadm, Mon May 5 14:27:57 2025 UTC
# Line 76  void clearscr() Line 76  void clearscr()
76    
77  int press_any_key()  int press_any_key()
78  {  {
         igetch(1);  
   
79          moveto(screen_rows, 0);          moveto(screen_rows, 0);
80          clrtoeol();          clrtoeol();
81    
# Line 106  void set_input_echo(int echo) Line 104  void set_input_echo(int echo)
104    
105  static 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;          int c, offset = 0, i, hz = 0;
         int c, offset = 0, len, loop = 1, i, hz = 0;  
108    
109          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
110          {          {
111                  offset++;                  offset++;
112          }          }
113    
114          igetch(1);          while ((c = igetch_t(60)))
   
         while (c = igetch_t(60))  
115          {          {
116                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
117                  {                  {
118                            igetch(1); // Cleanup remaining '\n' in the buffer
119                          break;                          break;
120                  }                  }
121                  if (c == LF)                  if (c == LF)
# Line 130  static int _str_input(char *buffer, int Line 126  static int _str_input(char *buffer, int
126                  {                  {
127                          if (offset > 0)                          if (offset > 0)
128                          {                          {
129                                  buffer[--offset] = '\0';                                  offset--;
130                                  prints("\b \b");                                  if (buffer[offset] < 0 || buffer[offset] > 127)
131                                  //            clrtoeol ();                                  {
132                                            prints("\033[D \033[D");
133                                            offset--;
134                                            if (offset < 0) // should not happen
135                                            {
136                                                    log_error("Offset of buffer is negative\n");
137                                                    offset = 0;
138                                            }
139                                    }
140                                    buffer[offset] = '\0';
141                                    prints("\033[D \033[D");
142                                  iflush();                                  iflush();
143                          }                          }
144                          continue;                          continue;
# Line 143  static int _str_input(char *buffer, int Line 149  static int _str_input(char *buffer, int
149                  }                  }
150                  if (c > 127 && c <= 255)                  if (c > 127 && c <= 255)
151                  {                  {
152                            if (!hz && offset + 2 > buffer_length) // No enough space for Chinese character
153                            {
154                                    igetch(1); // Cleanup remaining input
155                                    outc('\a');
156                                    iflush();
157                                    continue;
158                            }
159                          hz = (!hz);                          hz = (!hz);
160                  }                  }
161                  if (offset >= buffer_length)                  if (offset >= buffer_length)
162                  {                  {
163                          outc('\a');                          outc('\a');
164                            iflush();
165                          continue;                          continue;
166                  }                  }
167                  buffer[offset++] = (char)c;                  buffer[offset++] = (char)c;
# Line 167  static int _str_input(char *buffer, int Line 181  static int _str_input(char *buffer, int
181                  }                  }
182          }          }
183    
         prints("\r\n");  
         iflush();  
   
184          return offset;          return offset;
185  }  }
186    
187  int str_input(char *buffer, int buffer_length, int echo_mode)  int str_input(char *buffer, int buffer_length, int echo_mode)
188  {  {
189          int offset;          int len;
190    
191          memset(buffer, '\0', buffer_length);          buffer[0] = '\0';
192    
193          offset = _str_input(buffer, buffer_length, echo_mode);          len = _str_input(buffer, buffer_length, echo_mode);
194    
195          return offset;          prints("\r\n");
196            iflush();
197    
198            return len;
199  };  };
200    
201  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
# Line 202  int display_file(const char *filename) Line 216  int display_file(const char *filename)
216  {  {
217          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
218          FILE *fin;          FILE *fin;
219          int i;          size_t i;
220    
221          if ((fin = fopen(filename, "r")) == NULL)          if ((fin = fopen(filename, "r")) == NULL)
222          {          {
# Line 229  int display_file(const char *filename) Line 243  int display_file(const char *filename)
243  int display_file_ex(const char *filename, int begin_line, int wait)  int display_file_ex(const char *filename, int begin_line, int wait)
244  {  {
245          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
         char temp[LINE_BUFFER_LEN];  
246          int ch = 0;          int ch = 0;
247          int input_ok, line, max_lines;          int input_ok, line, max_lines;
248          long int c_line_current = 0, c_line_total = 0;          long int c_line_current = 0;
249            long int c_line_total = 0;
250          FILE *fin;          FILE *fin;
         struct stat f_stat;  
251          long *p_line_offsets;          long *p_line_offsets;
252          int len;          long int len;
253          int percentile;          long int percentile;
254            int loop = 1;
255    
256          if ((fin = fopen(filename, "r")) == NULL)          if ((fin = fopen(filename, "r")) == NULL)
257          {          {
# Line 253  int display_file_ex(const char *filename Line 267  int display_file_ex(const char *filename
267          line = begin_line;          line = begin_line;
268          max_lines = screen_rows - 1;          max_lines = screen_rows - 1;
269    
270          while (c_line_current < c_line_total)          while (loop)
271          {          {
272                  if (line >= max_lines)                  if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
273                  {                  {
274                          percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;                          if (wait)
275                            {
276                                    ch = press_any_key();
277                            }
278                            else
279                            {
280                                    iflush();
281                            }
282    
283                            loop = 0;
284                            break;
285                    }
286    
287                    if (c_line_current >= c_line_total || line >= max_lines)
288                    {
289                            if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
290                            {
291                                    percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
292                            }
293                            else
294                            {
295                                    percentile = 100;
296                            }
297    
298                          moveto(screen_rows, 0);                          moveto(screen_rows, 0);
299                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m%s (%d%%)%s\033[33m  │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
300                                     percentile);                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
301                                       (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
302                          iflush();                          iflush();
303    
304                          input_ok = 0;                          input_ok = 0;
# Line 279  int display_file_ex(const char *filename Line 316  int display_file_ex(const char *filename
316                                          c_line_current -= line;                                          c_line_current -= line;
317                                          line = begin_line;                                          line = begin_line;
318                                          max_lines = begin_line + 1;                                          max_lines = begin_line + 1;
319                                          prints("\033[1T"); // Scroll down 1 line                                          prints("\033[T"); // Scroll down 1 line
320                                            // max_lines = screen_rows - 1; // Legacy Fterm only works with this line
321                                          break;                                          break;
322                                  case KEY_DOWN:                                  case KEY_DOWN:
323                                  case CR:                                  case CR:
# Line 292  int display_file_ex(const char *filename Line 330  int display_file_ex(const char *filename
330                                          max_lines = screen_rows - 1;                                          max_lines = screen_rows - 1;
331                                          moveto(screen_rows, 0);                                          moveto(screen_rows, 0);
332                                          clrtoeol();                                          clrtoeol();
333                                          prints("\033[1S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
334                                          break;                                          break;
335                                  case KEY_PGUP:                                  case KEY_PGUP:
336                                  case Ctrl('B'):                                  case Ctrl('B'):
# Line 331  int display_file_ex(const char *filename Line 369  int display_file_ex(const char *filename
369                                  case KEY_LEFT:                                  case KEY_LEFT:
370                                  case 'q':                                  case 'q':
371                                  case 'Q':                                  case 'Q':
372                                          c_line_current = c_line_total;                                          loop = 0;
                                         wait = 0;  
373                                          break;                                          break;
374                                  case '?':                                  case '?':
375                                  case 'h':                                  case 'h':
376                                  case 'H':                                  case 'H':
377                                          // Display help information                                          // Display help information
378                                          strcpy(temp, app_home_dir);                                          display_file_ex(DATA_READ_HELP, begin_line, 1);
                                         strcat(temp, "data/read_help.txt");  
                                         display_file_ex(temp, begin_line, 1);  
379    
380                                          // Refresh after display help information                                          // Refresh after display help information
381                                          c_line_current -= (line - 1);                                          c_line_current -= (line - 1);
# Line 364  int display_file_ex(const char *filename Line 399  int display_file_ex(const char *filename
399                          log_error("Error length exceeds buffer size: %d\n", len);                          log_error("Error length exceeds buffer size: %d\n", len);
400                          len = LINE_BUFFER_LEN - 1;                          len = LINE_BUFFER_LEN - 1;
401                  }                  }
402                  if (fgets(buffer, len + 1, fin) == NULL)                  if (fgets(buffer, (int)len + 1, fin) == NULL)
403                  {                  {
404                          log_error("Reach EOF\n");                          log_error("Reach EOF\n");
405                          break;                          break;
# Line 376  int display_file_ex(const char *filename Line 411  int display_file_ex(const char *filename
411                  line++;                  line++;
412          }          }
413    
         iflush();  
   
         if (wait)  
         {  
                 ch = press_any_key();  
         }  
   
414          free(p_line_offsets);          free(p_line_offsets);
415          fclose(fin);          fclose(fin);
416    
# Line 393  int show_top(char *status) Line 421  int show_top(char *status)
421  {  {
422          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
423    
424          str_space(buffer, 20 - strlen(BBS_current_section_name));          str_space(buffer, 20 - (int)strnlen(BBS_current_section_name, sizeof(BBS_current_section_name)));
425    
426          moveto(1, 0);          moveto(1, 0);
427          clrtoeol();          clrtoeol();
# Line 408  int show_top(char *status) Line 436  int show_top(char *status)
436  int show_bottom(char *msg)  int show_bottom(char *msg)
437  {  {
438          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
         char str_time_onine[20];  
439          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
440          time_t time_online;          time_t time_online;
441          struct tm *tm_online;          struct tm *tm_online;
442    
443          get_time_str(str_time, sizeof(str_time));          get_time_str(str_time, sizeof(str_time));
444          str_space(buffer, 33 - strlen(BBS_username));          str_space(buffer, 33 - (int)strnlen(BBS_username, sizeof(BBS_username)));
445    
446          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
447          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
# Line 437  int show_active_board() Line 464  int show_active_board()
464          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
465          FILE *fin;          FILE *fin;
466          static int line;          static int line;
467          int len;          unsigned int len;
468          int end_of_line;          int end_of_line;
469    
         sprintf(filename, "%sdata/active_board.txt", app_home_dir);  
   
470          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
471    
472          if ((fin = fopen(filename, "r")) == NULL)          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
473          {          {
474                  log_error("Unable to open file %s\n", filename);                  log_error("Unable to open file %s\n", filename);
475                  return -1;                  return -1;


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

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