⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winio.c

📁 P2P NAP的C实现 P2P在网络应用中越来越流行
💻 C
📖 第 1 页 / 共 3 页
字号:
      free(msg);    } /* if-else-else */    free(command);    break;  case 128+KEY_BACKSPACE:               /* META-backspace */  case 128+127:                         /* META-delete */  case 128+8:                           /* META-backspace */    curx--;    while (curx >= 0 && cbuf[curx] == ' ') {      memmove(cbuf+curx, cbuf+curx+1, strlen(cbuf+curx+1));      cbuf[strlen(cbuf)-1] = 0;      curx--;    }    while (curx >= 0 && cbuf[curx] != ' ') {      memmove(cbuf+curx, cbuf+curx+1, strlen(cbuf+curx+1));      cbuf[strlen(cbuf)-1] = 0;      curx--;    }    if (curx < 0) {      cbuf[0] = 0;      curx = 0;    } else {      curx++;    }    if (curx == 0)      curr = 0;    indraw();    break;  case KEY_BACKSPACE:                   /* backspace */  case 127:                             /* delete */  case 8:                               /* backspace */    if (curx) {      for (j=curx-1; cbuf[j]; j++)        cbuf[j] = cbuf[j+1];      curx--;      indraw();    }    ebuf = 0;    break;  case 23:                              /* Ctrl-W */    cbuf[curx] = 0;    curx--;    while (curx >= 0 && cbuf[curx] == ' ')     {       cbuf[curx] = 0;       curx--;     }     while (curx >= 0 && cbuf[curx] != ' ')    {      cbuf[curx] = 0;      curx--;    }    curx++;    indraw();    break;      case 24:                              /* Ctrl-X */    nextchan();    indraw();    /* If in window mode, redraw the screen */    if (wmode)    {      dscr(wchan);      drw(wchan);    }    dstatus();    ebuf = 0;    break;  case 21:                              /* Ctrl-U */    cbuf[0] = 0;    curx = 0;    curr = 0;    indraw();    ebuf = 0;    break;  case 11:                              /* Ctrl-K */    cbuf[curx] = 0;    indraw();    ebuf = 0;    break;      case 1:                               /* Ctrl-A */  case KEY_HOME:                        /* Home */  case KEY_FIND:                        /* Find - a kind of home key */    curx = 0;    indraw();    ebuf = 0;    break;      case 5:                               /* Ctrl-E */  case KEY_END:                         /* End */  case KEY_SELECT:                      /* Select - a kind of end key */    curx = strlen(cbuf);    indraw();    ebuf = 0;    break;  case 4:                               /* Ctrl-D */  case 330:                             /* Delete */    if (cbuf[curx]) {      memmove(cbuf+curx, cbuf+curx+1, strlen(cbuf+curx+1)+1);    }    indraw();    ebuf = 0;    break;  case 154:                             /* Ctrl-Meta-Z ??? */    ebuf = 0;    break;  case '\t':                            /* Tab */    ebuf = 0;        for (g=curx; cbuf[g]!=' ' && g>=0; g--);        if (cbuf[g] != ' ')      g = 0;              if (*cbuf == '/' && !g)    {      alias_t *al;            for (al=alhead;al;al=al->next)        if (!strncasecmp(cbuf+1, al->nm, strlen(cbuf+1)))        {          memset(cbuf, 0, sizeof(cbuf));          cbuf[0] = '/';          strcpy(cbuf+1, al->nm);          strcat(cbuf, " ");          curx = strlen(cbuf);          indraw();          break;        }      for (j=0;out[j].func;j++)        if (!strncasecmp(cbuf+1, out[j].nm, strlen(cbuf+1)) && out[j].help)        {          memset(cbuf, 0, sizeof(cbuf));          cbuf[0] = '/';          strcpy(cbuf+1, out[j].nm);          strcat(cbuf, " ");          curx = strlen(cbuf);          indraw();          break;        }    }    else if (curchan && curchan->users)    {      user_t *usr;      int k, z;            for (k=curx;cbuf[k]!=' '&&k>=0;k--);            if (cbuf[k] == ' ')        z = k+1;      else        z = 0;            for (usr=curchan->users;usr;usr=usr->next)        if (!strncasecmp(cbuf+z, usr->nm, strlen(cbuf+z)))        {          for (k=z;k<=curx;k++)            cbuf[k] = 0;          strcpy(cbuf+z, usr->nm);          if (!z)            strcat(cbuf, ": ");          curx = strlen(cbuf);          indraw();          break;        }    }    break;    case 20:                              /* Ctrl-T */    if (!curchan || !curchan->topic)      tind = 0;    else {      if (tind >= (strlen(curchan->topic)-(COLS-(strlen(VERSION)+8)-4)))	tind = 0;      else	tind += 5;    }    ebuf = 0;    dstatus();    break;  case KEY_PPAGE:                       /* PgUp */  case 16:                              /* Ctrl-P */     dscroll(win, (LINES-2)/2);    indraw();    ebuf = 0;    break;  case KEY_NPAGE:                       /* PgDn */  case 14:                              /* Ctrl-N */  case 22:                              /* Ctrl-V */    dscroll(win, -(LINES-2)/2);    indraw();    ebuf = 0;    break;      case 12:                              /* Ctrl-L */    clearok(wchan, 1);    drw(wchan);    drw(sep);    drw(winput);    if (!info.notop)    {      drw(whead);    }    dstatus();    dscr(wchan);    drw(wchan);    indraw();    ebuf = 0;    break;  case KEY_F(1):                        /* F1 */  case 128 + '1':                       /* M-1 */    return(1);    break;  case KEY_F(2):                        /* F2 */  case 128 + '2':                       /* M-2 */    switchtoscreen(RESULT_SCREEN);    return(1);    break;  case KEY_F(3):                        /* F3 */  case 128 + '3':                       /* M-3 */    switchtoscreen(DLUL_SCREEN);    return(1);    break;  case KEY_UP:                          /* UP */    if (ccmd == NULL) {      if (cmdlend != NULL) {	strcpy(cscratch, cbuf);	ccmd = cmdlend;	strcpy(cbuf, ccmd->cmd);	curx = strlen(cbuf);      }    } else {      if (ccmd->prev != NULL) {	ccmd = ccmd->prev;	strcpy(cbuf, ccmd->cmd);	curx = strlen(cbuf);      }    }    indraw();    break;  case KEY_DOWN:                        /* DOWN */    if (ccmd) {      ccmd = ccmd->next;      if (ccmd == NULL) {	strcpy(cbuf, cscratch);	curx = strlen(cbuf);      } else {	strcpy(cbuf, ccmd->cmd);	curx = strlen(cbuf);      }    }    indraw();    break;  case KEY_RIGHT:                       /* RIGHT */    if (cbuf[curx])    {      curx++;    }    indraw();    break;  case KEY_LEFT:                        /* LEFT */    if (curx)    {      curx--;    }    indraw();    break;  default:                              /* printable characters? */    if ((32 <= ebuf && ebuf <= 126) || (160 <= ebuf && ebuf <=255))    {      j = strlen(cbuf);      if (j<sizeof(cbuf)-1) {	memmove(cbuf+curx+1, cbuf+curx, j-curx+1);	cbuf[curx] = ebuf;	ebuf = 0;	curx++;	indraw();      }    }    break;  } /* switch */    return(1);}void initwin(unsigned char f){  struct termios ts;  WINDOW *w;  int bgcolor;  if (info.daemon) {    info.daemon = 2;    return;  }  tcgetattr(0, &ts);#ifdef __CYGWIN32__  setenv("TERMINFO", "./", 1);    if (!newterm("cygwin", stdout, stdin))  {    fprintf(stderr, "Error opening terminal\n");    exit(-1);  }  def_prog_mode();  w = stdscr;#else  if (f)  {    if (!newterm("nxterm", stdout, stdin))    {      fprintf(stderr, "Error opening terminal\n");      exit(-1);    }    def_prog_mode();    w = stdscr;  }  else    w = initscr();#endif/*  COLS = w->_maxx+1;  LINES = w->_maxy+1; *//*  dolc(); */  start_color();  cbreak();  noecho();    bgcolor = COLOR_BLACK;#ifdef HAVE_ASSUME_DEFAULT_COLORS  if (info.transparent) {    assume_default_colors(COLOR_WHITE, -1);    bgcolor = -1;  }#endif  init_pair(1, COLOR_WHITE, COLOR_BLUE);  init_pair(CPR, COLOR_RED, bgcolor);  init_pair(CPG, COLOR_GREEN, bgcolor);  init_pair(CPW, COLOR_WHITE, bgcolor);  init_pair(CPB, COLOR_BLUE, bgcolor);  init_pair(CPY, COLOR_YELLOW, bgcolor);  init_pair(CPM, COLOR_MAGENTA, bgcolor);  init_pair(CPC, COLOR_CYAN, bgcolor);  init_pair(CPWR, COLOR_WHITE, COLOR_RED);  init_pair(CPWB, COLOR_WHITE, COLOR_BLUE);  winput = newwin("input", 1, 0, LINES-1, 0);  sep = newwin("sep", 1, 0, LINES-2, 0);  if (!info.notop)  {    wchan = newwin("chan", LINES-3, 0, 1, 0);    whead = newwin("head", 1, 0, 0, 0);  }  else    wchan = newwin("chan", LINES-2, 0, 0, 0);  nodelay(winput, TRUE);  wattrset(sep, COLOR_PAIR(1));  if (!info.notop)    wattrset(whead, COLOR_PAIR(1));  idlok(wchan, FALSE);  scrollok(wchan, TRUE);  wbkgdset(winput, COLOR_PAIR(CPW));  wbkgdset(wchan, COLOR_PAIR(CPW));/*  bkgd(COLOR_PAIR(1)); */  keypad(winput, TRUE);  keypad(stdscr, TRUE);  indraw();/*  tcgetattr(0, &ts);  printf("%i\n", ts.c_iflag&ISTRIP);  exit(1); */    dstatus();}/* draw the input area of the main window. This depends on the * following:  * * cbuf:  the text currently being edited by the user * curx:  the current cursor position within buf (0 <= strlen(buf) <= curx) * curr: the first character of buf that is actually visible (this is *       a global variable and we update it here - the only other two  *       places where it's updated are two places in input().) * curchan, curchan->nm, and curchan->q: this determines whether or not *       the current channel/query should be printed at the beginning of *       the line. * COL:  the current width of the screen. * **/void indraw(){  int i, b;  int cols = COLS;         /* trick compiler into allowing t[COLS+1] */  int ucols;               /* columns not taken up by channel */  unsigned char t[cols+1]; /* the entire line to be output, plus 0 */  t[cols]=0;  /* print the channel portion of the line */  if (curchan && cols>=3) {    char *p = strdup(curchan->nm);    if (strlen(curchan->nm) > (cols/4))      p[cols/4] = 0;    sprintf(t, curchan->q ? "(%s) " : "[%s] ", p);    free(p);  } else {    *t=0;  }    ucols = cols-strlen(t);  /* first adjust curr with respect to curx */  while (curx >= curr+ucols)    curr += ucols/4 ? ucols/4 : 1;  while (curx < curr)    curr -= ucols/4 ? ucols/4 : 1;  /* now curx is in the window defined by curr, unless ucols==0. */  /* now adjust curr with respect to cbuf, to make sure there is no     empty space on the left, and preferably none on the right. Notice     this cannot move the cursor out of the window. */  if (curr+ucols>strlen(cbuf)+1 && curr)    curr = strlen(cbuf)+1-ucols;  if (curr < 0)    curr = 0;  /* assemble the line */  strncat(t, cbuf+curr, ucols);  memset(t+strlen(t), ' ', cols-strlen(t));    /* and print it. */  werase(winput);  for (i=0;i<cols;i++)  {    if ((t[i] & 0x7f) < 32 || (t[i] & 0x7f) == 127) {      /* non-printable characters - should not normally occur */      waddch(winput, ((t[i] & 0x1f)+'A'-1)|doesc(winput, BOLD));    } else {      waddch(winput, t[i]);    }  }  /* calculate cursor position relative to window */  b = curx-curr+cols-ucols;  /* if the above calculations were right, we shouldn't have to do this */  if (b < 0)    b = 0;  if (b >= cols)    b = cols-1;  /* move the cursor */  wmove(winput, 0, b);  drw(winput);}/* not used */unsigned char gchr(WINDOW *win){  fd_set fs;  struct timeval tv;  unsigned char r;    FD_ZERO(&fs);  FD_SET(0, &fs);    tv.tv_sec = 0;  tv.tv_usec = 50000;    if (!select(1, &fs, NULL, NULL, &tv))    return(0);    r = wgetch(win);  if (r == '\e')  {    ungetch('\e');    return(0);  }    return(r);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -