/[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.1 by sysadm, Fri Oct 22 15:21:28 2004 UTC Revision 1.8 by sysadm, Wed Mar 2 16:33:49 2005 UTC
# Line 15  Line 15 
15   *                                                                         *   *                                                                         *
16   ***************************************************************************/   ***************************************************************************/
17    
18    #include "common.h"
19  #include "io.h"  #include "io.h"
20    #include <sys/types.h>
21    #include <sys/stat.h>
22    #include <unistd.h>
23    
24    int screen_lines = 24;
25    
26  void  void
27  moveto (int row, int col)  moveto (int row, int col)
# Line 28  moveto (int row, int col) Line 34  moveto (int row, int col)
34      {      {
35        prints ("\r");        prints ("\r");
36      }      }
37      iflush ();
38  }  }
39    
40  void  void
41  clrtoeol ()  clrtoeol ()
42  {  {
43    prints ("\033[K");    prints ("\033[K");
44      iflush ();
45    }
46    
47    void
48    clrline (int line_begin, int line_end)
49    {
50      int i;
51    
52      for (i = line_begin; i <= line_end; i++)
53        {
54          moveto (i, 0);
55          prints ("\033[K");
56        }
57    
58      iflush ();
59    }
60    
61    void
62    clearscr ()
63    {
64      prints ("\33[2J");
65      moveto (0, 0);
66      iflush ();
67    }
68    
69    int
70    press_any_key ()
71    {
72      moveto (screen_lines, 0);
73      clrtoeol ();
74    
75      prints ("                           \033[1;33m按任意键继续...\033[0;37m");
76      iflush ();
77    
78      return igetch ();
79    }
80    
81    void
82    set_input_echo(int echo)
83    {
84      char temp[256];
85    
86      if (echo)
87      {
88        outc ('\x83'); // ASCII code 131
89        iflush ();
90      }
91      else
92      {
93    //    outc ('\x85'); // ASCII code 133
94        prints ("\xff\xfb\x01\xff\xfb\x03");
95        iflush ();
96        igetch ();
97        igetch ();
98      }
99  }  }
100    
101  int  int
# Line 46  str_input (char *buffer, int buffer_leng Line 108  str_input (char *buffer, int buffer_leng
108    
109    while (c = igetch ())    while (c = igetch ())
110      {      {
111        if (c == CR || c == LF)        if (c == CR)
112          break;          break;
113          if (c == LF)
114            continue;
115        if (c == BACKSPACE)        if (c == BACKSPACE)
116          {          {
117            if (offset > 0)            if (offset > 0)
# Line 64  str_input (char *buffer, int buffer_leng Line 128  str_input (char *buffer, int buffer_leng
128            continue;            continue;
129          }          }
130        if (c > 127 && c <= 255)        if (c > 127 && c <= 255)
131          {          {
132            hz = (!hz);            hz = (!hz);
133          }          }
134        if (offset >= buffer_length)        if (offset >= buffer_length)
135          {          {
136            outc ('\a');            outc ('\a');
# Line 84  str_input (char *buffer, int buffer_leng Line 148  str_input (char *buffer, int buffer_leng
148            break;            break;
149          }          }
150        if (!hz)        if (!hz)
151          {          {
152            iflush ();            iflush ();
153          }          }
154      }      }
155    
156    prints ("\r\n");    prints ("\r\n");
# Line 96  str_input (char *buffer, int buffer_leng Line 160  str_input (char *buffer, int buffer_leng
160  }  }
161    
162  int  int
163  display_file(const char* filename)  display_file (const char *filename)
164  {  {
165    char buffer[256];    char buffer[260];
166    FILE *fin;    FILE *fin;
167        int i;
   if ((fin = fopen(filename, "r")) != NULL)  
   {  
     while (fgets(buffer, 255, fin))  
     {  
       prints (buffer);  
       iflush ();  
     }  
     fclose (fin);  
168    
169      if ((fin = fopen (filename, "r")) != NULL)
170      {
171        while (fgets (buffer, 255, fin))
172          {
173            i = strlen (buffer);
174            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
175              {
176                buffer[i - 1] = '\r';
177                buffer[i] = '\n';
178                buffer[i + 1] = '\0';
179              }
180            prints (buffer);
181            iflush ();
182          }
183        fclose(fin);
184      return 0;      return 0;
185    }    }
186        
# Line 117  display_file(const char* filename) Line 188  display_file(const char* filename)
188  }  }
189    
190  int  int
191  show_top()  display_file_ex (const char *filename, int begin_line, int wait)
192  {  {
193    return 0;    char buffer[260], temp[256];
194  }    int i, ch, input_ok;
195      long int line, c_line_begin = 0, c_line_total=0;
196      long int f_line, f_size, f_offset;
197      FILE *fin;
198      struct stat f_stat;
199    
200  int    clrline (begin_line, screen_lines);
201  show_bottom()    line = begin_line;
202  {    moveto (line, 0);
203    return 0;  
204      if ((fin = fopen (filename, "r")) != NULL)
205        {
206          if (fstat (fileno (fin), &f_stat) != 0)
207            {
208              log_error ("Get file stat failed\n");
209              return -1;
210            }
211          f_size = f_stat.st_size;
212    
213          while (fgets (buffer, 255, fin))
214            c_line_total ++;
215          rewind (fin);
216    
217          while (fgets (buffer, 255, fin))
218            {
219              if (line >= screen_lines)
220                {
221                  f_offset = ftell (fin);
222    
223                  moveto (screen_lines, 0);
224                  prints
225                    ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
226                     (f_offset-strlen(buffer)) * 100 / f_size);
227                  iflush ();
228    
229                  input_ok = 0;
230                  while (!input_ok)
231                    {
232                      ch = igetch ();
233                      input_ok = 1;
234                      switch (ch)
235                        {
236                        case KEY_UP:
237                          c_line_begin--;
238                          if (c_line_begin >= 0)
239                            {
240                              rewind (fin);
241                              for (f_line = 0; f_line < c_line_begin; f_line++)
242                                {
243                                  if (fgets (buffer, 255, fin) == NULL)
244                                    goto exit;
245                                }
246                            }
247                          else
248                            {
249                              goto exit;
250                            }
251                          break;
252                        case KEY_DOWN:
253                        case CR:
254                          c_line_begin++;
255                          rewind (fin);
256                          for (f_line = 0; f_line < c_line_begin; f_line++)
257                            {
258                              if (fgets (buffer, 255, fin) == NULL)
259                                goto exit;
260                            }
261                          break;
262                        case KEY_PGUP:
263                        case Ctrl ('B'):
264                          if (c_line_begin > 0)
265                            c_line_begin -= (screen_lines - begin_line - 1);
266                          else
267                            goto exit;
268                          if (c_line_begin < 0)
269                            c_line_begin = 0;
270                          rewind (fin);
271                          for (f_line = 0; f_line < c_line_begin; f_line++)
272                            {
273                              if (fgets (buffer, 255, fin) == NULL)
274                                goto exit;
275                            }
276                          break;
277                        case KEY_RIGHT:
278                        case KEY_PGDOWN:
279                        case Ctrl ('F'):
280                        case KEY_SPACE:
281                          c_line_begin += (screen_lines - begin_line - 1);
282                          if (c_line_begin + (screen_lines - begin_line) > c_line_total)
283                            c_line_begin = c_line_total - (screen_lines - begin_line);
284                          rewind (fin);
285                          for (f_line = 0; f_line < c_line_begin; f_line++)
286                            {
287                              if (fgets (buffer, 255, fin) == NULL)
288                                goto exit;
289                            }
290                          break;
291                        case KEY_LEFT:
292                        case 'q':
293                        case 'Q':
294                          goto exit;
295                          break;
296                        case '?':
297                        case 'h':
298                        case 'H':
299                          //Display help information
300                          strcpy (temp, app_home_dir);
301                          strcat (temp, "data/read_help.txt");
302                          display_file_ex (temp, begin_line, 1);
303    
304                          //Refresh after display help information
305                          rewind (fin);
306                          for (f_line = 0; f_line < c_line_begin; f_line++)
307                            {
308                              if (fgets (buffer, 255, fin) == NULL)
309                                goto exit;
310                            }
311                          break;
312                        default:
313                          input_ok = 0;
314                          break;
315                        }
316                    }
317    
318                  clrline (begin_line, screen_lines);
319                  line = begin_line;
320                  moveto (line, 0);
321    
322                  continue;
323                }
324    
325              i = strlen (buffer);
326              if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
327                {
328                  buffer[i - 1] = '\r';
329                  buffer[i] = '\n';
330                  buffer[i + 1] = '\0';
331                }
332              prints (buffer);
333              iflush ();
334    
335              line++;
336            }
337          if (wait)
338            ch = press_any_key();
339          else
340            ch = 0;
341    
342        exit:
343          fclose (fin);
344    
345          return ch;
346        }
347    
348      return -1;
349  }  }


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

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