pike.git / lib / modules / Stdio.pmod / Readline.pike

version» Context lines:

pike.git/lib/modules/Stdio.pmod/Readline.pike:1: - // $Id: Readline.pike,v 1.4 1999/03/17 23:46:20 marcus Exp $ + // $Id: Readline.pike,v 1.5 1999/03/23 15:25:23 marcus Exp $      class OutputController   {    static private object outfd, term;    static private int xpos = 0, columns = 0; -  +  static private mapping oldattrs = 0;    -  +  void disable() +  { +  catch{ +  if(oldattrs) +  outfd->tcsetattr((["OPOST":0,"ONLCR":0,"OCRNL":0, +  "OLCUC":0,"OFILL":0,"OFDEL":0, +  "ONLRET":0,"ONOCR":0])&oldattrs); +  else +  outfd->tcsetattr((["OPOST":1])); +  }; +  } +  +  void enable() +  { +  if(term->put("cr") && term->put("do")) +  catch { outfd->tcsetattr((["OPOST":0])); }; +  else +  catch { outfd->tcsetattr((["OPOST":1,"ONLCR":1,"OCRNL":0,"OLCUC":0, +  "OFILL":1,"OFDEL":0,"ONLRET":0,"ONOCR":0]));}; +  } +  +  void destroy() +  { +  disable(); +  } +     void check_columns()    {    catch {    int c = outfd->tcgetattr()->columns;    if(c)    columns = c;    };    if(!columns)    columns = term->tgetnum("co") || 80;    }
pike.git/lib/modules/Stdio.pmod/Readline.pike:48:    int n = width(s);    if(!n)    return;    while(xpos+n>=columns) {    int l = columns-xpos;    outfd->write(s[..l-1]);    s = s[l..];    n -= l;    xpos = 0;    if(!term->tgetflag("am")) -  outfd->write((term->put("cr")||"\r")+(term->put("do")||"\n")); +  outfd->write((term->put("cr")||"")+(term->put("do")||"\n"));    }    if(xpos==0 && term->tgetflag("am")) -  outfd->write(" "+(term->put("le")||"\b")); +  outfd->write(" "+(term->put("le")||""));    if(n>0) {    outfd->write(s);    xpos += n;    }    }       void write(string s)    {    low_write(escapify(s));    }
pike.git/lib/modules/Stdio.pmod/Readline.pike:82:    if(n<=0)    return;    outfd->write(term->put("UP", n) || (term->put("up")||"")*n);    }       void low_move_forward(int n)    {    if(n<=0)    return;    if(xpos+n<columns) { -  outfd->write(term->put("RI", n) || (term->put("ri")||" ")*n); +  outfd->write(term->put("RI", n) || (term->put("ri")||"")*n);    xpos += n;    } else {    int l = (xpos+n)/columns;    low_move_downward(l);    n -= l*columns;    if(n<0)    low_move_backward(-n);    else if(n>0)    low_move_forward(n);    }    }       void low_move_backward(int n)    {    if(n<=0)    return;    if(xpos-n>=0) { -  outfd->write(term->put("LE", n) || (term->put("le")||"\b")*n); +  outfd->write(term->put("LE", n) || (term->put("le")||"")*n);    xpos -= n;    } else {    int l = 1+(n-xpos-1)/columns;    low_move_upward(l);    n -= l*columns;    if(n<0)    low_move_forward(-n);    else if(n>0)    low_move_backward(n);    }
pike.git/lib/modules/Stdio.pmod/Readline.pike:142:    low_move_backward(escapified_width(s));    }       void erase(string s)    {    low_erase(escapified_width(s));    }       void newline()    { -  outfd->write((term->put("cr")||"\r")+(term->put("do")||"\n")); +  string cr = term->put("cr"), down = term->put("do"); +  if(cr && down) +  outfd->write(cr+down); +  else +  // In this case we have ONLCR (hopefully) +  outfd->write("\n");    xpos = 0;    }       void bol()    { -  outfd->write(term->put("cr")||"\r"); +  outfd->write(term->put("cr")||"");    xpos = 0;    }       void clear(int|void partial)    {    string s;    if(!partial && (s = term->put("cl"))) {    outfd->write(s);    xpos = 0;    return;    }    if(!partial) { -  outfd->write(term->put("ho")||term->put("cm", 0, 0)||"\f"); +  outfd->write(term->put("ho")||term->put("cm", 0, 0)||"");    xpos = 0;    } -  outfd->write(term->put("cd")||(partial?"":"\f")); +  outfd->write(term->put("cd")||"");    }       void beep()    {    outfd->write(term->put("bl")||"");    }       void create(object|void _outfd, object|string|void _term)    {    outfd = _outfd || Stdio.File("stdout");    term = objectp(_term)? _term : .Terminfo.getTerm(_term); -  +  catch { oldattrs = outfd->tcgetattr(); };    check_columns();    }      }      class InputController   {    static private object infd, term;    static private int enabled = -1;    static private function(:int) close_callback = 0;    static private string prefix="";    static private mapping(int:function|mapping(string:function)) bindings=([]);    static private function grab_binding = 0;    static private mapping oldattrs = 0;       void destroy()    {    catch{ infd->set_blocking(); }; -  catch{ if(oldattrs) infd->tcsetattr(oldattrs); }; +  catch{ if(oldattrs) infd->tcsetattr((["ECHO":0,"ICANON":0,"VEOF":0, +  "VEOL":0,"VLNEXT":0])&oldattrs); };    catch{ infd->tcsetattr((["ECHO":1,"ICANON":1])); };    }       static private string process_input(string s)    {    int i;       for (i=0; i<sizeof(s); i++)    {    if (!enabled)
pike.git/lib/modules/Stdio.pmod/Readline.pike:887:   }      static private void read_newline(string s)   {    input_controller->disable();    readtext = s;   }      void set_nonblocking(function f)   { -  if (newline_func = f) +  if (newline_func = f) { +  output_controller->enable();    input_controller->enable(); -  else +  } else {    input_controller->disable(); -  +  output_controller->disable();    } -  + }      void set_blocking()   {    set_nonblocking(0);   }      string read()   {    if(newline_func == read_newline)    return 0;    function oldnl = newline_func;    output_controller->write(prompt);    initline();    newline_func = read_newline;    readtext = ""; -  +  output_controller->enable();    input_controller->run_blocking();    set_nonblocking(oldnl);    return readtext;   }      void enable_history(object(History)|int hist)   {    if (objectp(hist))    historyobj = hist;    else if(!hist)