/[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.2 by sysadm, Fri Oct 22 18:47:51 2004 UTC Revision 1.6 by sysadm, Sat Oct 23 18:41:41 2004 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();    iflush ();
38  }  }
39    
40  void  void
41  clrtoeol ()  clrtoeol ()
42  {  {
43    prints ("\033[K");    prints ("\033[K");
44    iflush();    iflush ();
45  }  }
46    
47  void  void
48  clearscr()  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");    prints ("\33[2J");
65    moveto (0,0);    moveto (0, 0);
66    iflush();    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  int  int
# Line 74  str_input (char *buffer, int buffer_leng Line 106  str_input (char *buffer, int buffer_leng
106            continue;            continue;
107          }          }
108        if (c > 127 && c <= 255)        if (c > 127 && c <= 255)
109          {          {
110            hz = (!hz);            hz = (!hz);
111          }          }
112        if (offset >= buffer_length)        if (offset >= buffer_length)
113          {          {
114            outc ('\a');            outc ('\a');
# Line 94  str_input (char *buffer, int buffer_leng Line 126  str_input (char *buffer, int buffer_leng
126            break;            break;
127          }          }
128        if (!hz)        if (!hz)
129          {          {
130            iflush ();            iflush ();
131          }          }
132      }      }
133    
134    prints ("\r\n");    prints ("\r\n");
# Line 106  str_input (char *buffer, int buffer_leng Line 138  str_input (char *buffer, int buffer_leng
138  }  }
139    
140  int  int
141  display_file(const char* filename)  display_file (const char *filename)
142  {  {
143    char buffer[256];    char buffer[260];
144    FILE *fin;    FILE *fin;
145        int i;
   if ((fin = fopen(filename, "r")) != NULL)  
   {  
     while (fgets(buffer, 255, fin))  
     {  
       prints (buffer);  
       iflush ();  
     }  
     fclose (fin);  
146    
147      if ((fin = fopen (filename, "r")) != NULL)
148      {
149        while (fgets (buffer, 255, fin))
150          {
151            i = strlen (buffer);
152            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
153              {
154                buffer[i - 1] = '\r';
155                buffer[i] = '\n';
156                buffer[i + 1] = '\0';
157              }
158            prints (buffer);
159            iflush ();
160          }
161        fclose(fin);
162      return 0;      return 0;
163    }    }
164        
# Line 127  display_file(const char* filename) Line 166  display_file(const char* filename)
166  }  }
167    
168  int  int
169  show_top()  display_file_ex (const char *filename, int begin_line, int wait)
170  {  {
171    return 0;    char buffer[260], temp[256];
172      int i, ch, input_ok;
173      long int line, c_line_begin = 0, c_line_total=0;
174      long int f_line, f_size, f_offset;
175      FILE *fin;
176      struct stat f_stat;
177    
178      clrline (begin_line, screen_lines);
179      line = begin_line;
180      moveto (line, 0);
181    
182      if ((fin = fopen (filename, "r")) != NULL)
183        {
184          if (fstat (fileno (fin), &f_stat) != 0)
185            {
186              log_error ("Get file stat failed\n");
187              return -1;
188            }
189          f_size = f_stat.st_size;
190    
191          while (fgets (buffer, 255, fin))
192            c_line_total ++;
193          rewind (fin);
194    
195          while (fgets (buffer, 255, fin))
196            {
197              if (line >= screen_lines)
198                {
199                  f_offset = ftell (fin);
200    
201                  moveto (screen_lines, 0);
202                  prints
203                    ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
204                     (f_offset-strlen(buffer)) * 100 / f_size);
205                  iflush ();
206    
207                  input_ok = 0;
208                  while (!input_ok)
209                    {
210                      ch = igetch ();
211                      input_ok = 1;
212                      switch (ch)
213                        {
214                        case KEY_UP:
215                          c_line_begin--;
216                          if (c_line_begin >= 0)
217                            {
218                              rewind (fin);
219                              for (f_line = 0; f_line < c_line_begin; f_line++)
220                                {
221                                  if (fgets (buffer, 255, fin) == NULL)
222                                    goto exit;
223                                }
224                            }
225                          else
226                            {
227                              goto exit;
228                            }
229                          break;
230                        case KEY_DOWN:
231                        case CR:
232                          c_line_begin++;
233                          rewind (fin);
234                          for (f_line = 0; f_line < c_line_begin; f_line++)
235                            {
236                              if (fgets (buffer, 255, fin) == NULL)
237                                goto exit;
238                            }
239                          break;
240                        case KEY_PGUP:
241                        case Ctrl ('B'):
242                          if (c_line_begin > 0)
243                            c_line_begin -= (screen_lines - begin_line - 1);
244                          else
245                            goto exit;
246                          if (c_line_begin < 0)
247                            c_line_begin = 0;
248                          rewind (fin);
249                          for (f_line = 0; f_line < c_line_begin; f_line++)
250                            {
251                              if (fgets (buffer, 255, fin) == NULL)
252                                goto exit;
253                            }
254                          break;
255                        case KEY_RIGHT:
256                        case KEY_PGDOWN:
257                        case Ctrl ('F'):
258                        case KEY_SPACE:
259                          c_line_begin += (screen_lines - begin_line - 1);
260                          if (c_line_begin + (screen_lines - begin_line) > c_line_total)
261                            c_line_begin = c_line_total - (screen_lines - begin_line);
262                          rewind (fin);
263                          for (f_line = 0; f_line < c_line_begin; f_line++)
264                            {
265                              if (fgets (buffer, 255, fin) == NULL)
266                                goto exit;
267                            }
268                          break;
269                        case KEY_LEFT:
270                        case 'q':
271                        case 'Q':
272                          goto exit;
273                          break;
274                        case '?':
275                        case 'h':
276                        case 'H':
277                          //Display help information
278                          strcpy (temp, app_home_dir);
279                          strcat (temp, "data/read_help.txt");
280                          display_file_ex (temp, begin_line, 1);
281    
282                          //Refresh after display help information
283                          rewind (fin);
284                          for (f_line = 0; f_line < c_line_begin; f_line++)
285                            {
286                              if (fgets (buffer, 255, fin) == NULL)
287                                goto exit;
288                            }
289                          break;
290                        default:
291                          input_ok = 0;
292                          break;
293                        }
294                    }
295    
296                  clrline (begin_line, screen_lines);
297                  line = begin_line;
298                  moveto (line, 0);
299    
300                  continue;
301                }
302    
303              i = strlen (buffer);
304              if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
305                {
306                  buffer[i - 1] = '\r';
307                  buffer[i] = '\n';
308                  buffer[i + 1] = '\0';
309                }
310              prints (buffer);
311              iflush ();
312    
313              line++;
314            }
315          if (wait)
316            ch = press_any_key();
317          else
318            ch = 0;
319    
320        exit:
321          fclose (fin);
322    
323          return ch;
324        }
325    
326      return -1;
327  }  }
328    
329  int  int
330  show_bottom()  show_top ()
331  {  {
332    return 0;    return 0;
333  }  }
334    
335  int  int
336  press_any_key()  show_bottom ()
337  {  {
338    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m                       \r\n");    return 0;
   iflush();  
   
   return igetch ();  
339  }  }


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

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