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

Diff of /lbbs/src/section_list_display.c

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

Revision 1.4 by sysadm, Wed May 28 13:08:41 2025 UTC Revision 1.16 by sysadm, Sun Jun 1 14:05:09 2025 UTC
# Line 16  Line 16 
16    
17  #include "section_list_display.h"  #include "section_list_display.h"
18  #include "section_list_loader.h"  #include "section_list_loader.h"
19    #include "article_cache.h"
20  #include "common.h"  #include "common.h"
21  #include "io.h"  #include "io.h"
22  #include "screen.h"  #include "screen.h"
23  #include "log.h"  #include "log.h"
24  #include "str_process.h"  #include "str_process.h"
25  #include <time.h>  #include <time.h>
26    #include <sys/param.h>
27  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
28  #include <string.h>  #include <string.h>
29    
30    static int section_topic_view_mode = 0;
31    static int section_topic_view_tid = -1;
32    
33  enum select_cmd_t  enum select_cmd_t
34  {  {
35          EXIT_SECTION = 0,          EXIT_SECTION = 0,
# Line 34  enum select_cmd_t Line 39  enum select_cmd_t
39          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
40  };  };
41    
42  static int section_list_draw_items(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)
43  {  {
44          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
45          struct tm tm_sub;          struct tm tm_sub;
# Line 84  static int section_list_draw_items(SECTI Line 89  static int section_list_draw_items(SECTI
89                  }                  }
90    
91                  moveto(4 + i, 1);                  moveto(4 + i, 1);
92                  prints("  %7d %c %s%*s %s %s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
93                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
94                             p_articles[i]->aid,                             p_articles[i]->aid,
95                             article_flag,                             article_flag,
96                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
# Line 92  static int section_list_draw_items(SECTI Line 98  static int section_list_draw_items(SECTI
98                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
99                             "",                             "",
100                             str_time,                             str_time,
101                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
102                             title_f);                             title_f);
103          }          }
104    
105          return 0;          return 0;
106  }  }
107    
108  static int section_list_draw_screen(SECTION_LIST *p_section, int display_nickname)  static int section_list_draw_screen(const char *sname, const char *stitle, const char *master_list, int display_nickname)
109  {  {
110          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
111          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
112    
113          if (p_section->master_list[0] != '\0')          if (master_list[0] != '\0')
114          {          {
115                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", p_section->master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
116          }          }
117          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", p_section->sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
118    
119          clearscr();          clearscr();
120          show_top(str_section_master, p_section->stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
121          moveto(2, 0);          moveto(2, 0);
122          prints("离开[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] 阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m 昵称[\033[1;32mn\033[0;37m]\033[m");          prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] "
123                       "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",
124                       (display_nickname ? "显示用户名" : "显示昵称"));
125          moveto(3, 0);          moveto(3, 0);
126          if (display_nickname)          if (display_nickname)
127          {          {
128                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者 昵 称       日  期  文 章 标 题                            \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
129          }          }
130          else          else
131          {          {
132                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文 章 标 题                                    \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
133          }          }
         show_bottom("");  
134    
135          return 0;          return 0;
136  }  }
# Line 241  static enum select_cmd_t section_list_se Line 249  static enum select_cmd_t section_list_se
249          return EXIT_SECTION;          return EXIT_SECTION;
250  }  }
251    
252    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
253    {
254            switch (*p_key)
255            {
256            case 'p':
257                    section_topic_view_mode = !section_topic_view_mode;
258            case 0: // Set msg
259                    if (section_topic_view_mode)
260                    {
261                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
262                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
263                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] | "
264                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
265                    }
266                    else
267                    {
268                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
269                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
270                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
271                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
272                    }
273                    *p_key = 0;
274                    break;
275            case KEY_UP:
276            case KEY_PGUP:
277            case KEY_HOME:
278                    if (p_ctx->reach_begin)
279                    {
280                            if (section_topic_view_mode)
281                            {
282                                    *p_key = KEY_PGUP;
283                            }
284                            else
285                            {
286                                    *p_key = KEY_UP;
287                            }
288                            return 1;
289                    }
290                    break;
291            case KEY_DOWN:
292            case KEY_PGDN:
293            case KEY_END:
294                    if (p_ctx->reach_end)
295                    {
296                            if (section_topic_view_mode)
297                            {
298                                    *p_key = KEY_PGDN;
299                            }
300                            else
301                            {
302                                    *p_key = KEY_DOWN;
303                            }
304                            return 1;
305                    }
306                    break;
307            }
308    
309            return 0;
310    }
311    
312  int section_list_display(const char *sname)  int section_list_display(const char *sname)
313  {  {
314          static int display_nickname = 0;          static int display_nickname = 0;
315    
316          SECTION_LIST *p_section;          SECTION_LIST *p_section;
317            char stitle[BBS_section_title_max_len + 1];
318            char master_list[(BBS_username_max_len + 1) * 3 + 1];
319            char page_info_str[LINE_BUFFER_LEN];
320          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
321          int article_count;          int article_count;
322            int page_count;
323          int page_id = 0;          int page_id = 0;
324          int selected_index = 0;          int selected_index = 0;
325            ARTICLE_CACHE cache;
326          int ret;          int ret;
327            int loop;
328            int direction;
329    
330          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
331          if (p_section == NULL)          if (p_section == NULL)
# Line 259  int section_list_display(const char *sna Line 334  int section_list_display(const char *sna
334                  return -1;                  return -1;
335          }          }
336    
337          if (section_list_draw_screen(p_section, display_nickname) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
338            {
339                    log_error("section_list_rd_lock(sid = 0) error\n");
340                    return -2;
341            }
342    
343            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
344            stitle[sizeof(stitle) - 1] = '\0';
345            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
346            master_list[sizeof(master_list) - 1] = '\0';
347    
348            if ((ret = section_list_rd_unlock(p_section)) < 0)
349            {
350                    log_error("section_list_rd_unlock(sid = 0) error\n");
351                    return -2;
352            }
353    
354            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
355          {          {
356                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
357                  return -2;                  return -2;
358          }          }
359    
360          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
361            if (ret < 0)
362          {          {
363                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
364                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
365          }          }
366          else // empty section  
367            if (article_count == 0) // empty section
368          {          {
                 article_count = 0;  
369                  selected_index = 0;                  selected_index = 0;
370          }          }
371    
372          while (!SYS_server_exit)          while (!SYS_server_exit)
373          {          {
374                  ret = section_list_draw_items(p_section, page_id, p_articles, article_count, display_nickname);                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname);
375                  if (ret < 0)                  if (ret < 0)
376                  {                  {
377                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
378                          return -4;                          return -4;
379                  }                  }
380    
381                    snprintf(page_info_str, sizeof(page_info_str),
382                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
383                                     page_id + 1, MAX(page_count, 1));
384    
385                    show_bottom(page_info_str);
386                  iflush();                  iflush();
387    
388                  ret = section_list_select(p_section->page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
389                  switch (ret)                  switch (ret)
390                  {                  {
391                  case EXIT_SECTION:                  case EXIT_SECTION:
392                          return 0;                          return 0;
393                  case CHANGE_PAGE:                  case CHANGE_PAGE:
394                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
395                            if (ret < 0)
396                          {                          {
397                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
398                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
399                          }                          }
400                          else // empty section                          if (article_count == 0) // empty section
401                          {                          {
                                 article_count = 0;  
402                                  selected_index = 0;                                  selected_index = 0;
403                          }                          }
404                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
405                          {                          {
406                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
407                          }                          }
408                          break;                          break;
409                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
410                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          do
411                            {
412                                    loop = 0;
413    
414                                    if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
415                                    {
416                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
417                                            break;
418                                    }
419    
420                                    ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,
421                                                                       display_article_key_handler, DATA_READ_HELP);
422    
423                                    if (article_cache_unload(&cache) < 0)
424                                    {
425                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
426                                            break;
427                                    }
428    
429                                    switch (ret)
430                                    {
431                                    case KEY_UP:
432                                            if (selected_index <= 0)
433                                            {
434                                                    if (page_id > 0)
435                                                    {
436                                                            page_id--;
437                                                            selected_index = BBS_article_limit_per_page - 1;
438    
439                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
440                                                            if (ret < 0)
441                                                            {
442                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
443                                                                    return -3;
444                                                            }
445    
446                                                            if (article_count != BBS_article_limit_per_page) // page is not full
447                                                            {
448                                                                    selected_index = MAX(0, article_count - 1);
449                                                            }
450                                                            else
451                                                            {
452                                                                    loop = 1;
453                                                            }
454                                                    }
455                                            }
456                                            else
457                                            {
458                                                    selected_index--;
459                                                    loop = 1;
460                                            }
461                                            break;
462                                    case KEY_DOWN:
463                                            if (selected_index + 1 >= article_count) // next page
464                                            {
465                                                    if (page_id + 1 < page_count)
466                                                    {
467                                                            page_id++;
468                                                            selected_index = 0;
469    
470                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
471                                                            if (ret < 0)
472                                                            {
473                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
474                                                                    return -3;
475                                                            }
476    
477                                                            if (article_count == 0) // empty page
478                                                            {
479                                                                    selected_index = 0;
480                                                            }
481                                                            else
482                                                            {
483                                                                    loop = 1;
484                                                            }
485                                                    }
486                                            }
487                                            else
488                                            {
489                                                    selected_index++;
490                                                    loop = 1;
491                                            }
492                                            break;
493                                    case KEY_PGUP:
494                                    case KEY_PGDN:
495                                            direction = (ret == KEY_PGUP ? -1 : 1);
496                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
497                                            if (ret < 0)
498                                            {
499                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
500                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
501                                                    return -3;
502                                            }
503                                            else if (ret > 0)
504                                            {
505                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
506                                                    if (ret < 0)
507                                                    {
508                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
509                                                            return -3;
510                                                    }
511                                                    loop = 1;
512                                            }
513                                            break;
514                                    }
515                            } while (loop);
516    
517                            // Update current topic
518                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
519                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
520                          if (section_list_draw_screen(p_section, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
521                          {                          {
522                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
523                                  return -2;                                  return -2;
# Line 326  int section_list_display(const char *sna Line 525  int section_list_display(const char *sna
525                          break;                          break;
526                  case CHANGE_NAME_DISPLAY:                  case CHANGE_NAME_DISPLAY:
527                          display_nickname = !display_nickname;                          display_nickname = !display_nickname;
528                          if (section_list_draw_screen(p_section, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
529                          {                          {
530                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
531                                  return -2;                                  return -2;


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

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