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.24 1999/06/22 19:22:42 marcus Exp $ + // $Id: Readline.pike,v 1.25 1999/06/22 21:53:39 marcus Exp $      class OutputController   {    static private object outfd, term;    static private int xpos = 0, columns = 0;    static private mapping oldattrs = 0;    -  + #define BLINK 1 + #define BOLD 2 + #define DIM 4 + #define REVERSE 8 + #define ITALIC 16 + #define STANDOUT 32 + #define UNDERLINE 64 +  +  static private int selected_attributes = 0, needed_attributes = 0; +  static private int active_attributes = 0; +  +  static int low_attribute_mask(array(string) atts) +  { +  return `|(@rows(([ +  "blink":BLINK, +  "bold":BOLD, +  "dim":DIM, +  "reverse":REVERSE, +  "italic":ITALIC, +  "standout":STANDOUT, +  "underline":UNDERLINE +  ]), atts)); +  } +  +  static void low_set_attributes(int mask, int val, int|void temp) +  { +  int i, remv = mask & selected_attributes & ~val; +  string s = ""; +  +  if(remv & 15) { +  s += term->put("me")||""; +  needed_attributes |= selected_attributes; +  active_attributes = 0; +  } +  if(temp) { +  needed_attributes |= remv; +  } else { +  selected_attributes &= ~remv; +  needed_attributes &= ~remv; +  } +  active_attributes &= ~remv; +  for(i=0; remv; i++) +  if(remv & (1<<i)) { +  string cap = ({0,0,0,0,"ZR","se","ue"})[i]; +  if(cap && (cap = term->put(cap))) +  s += cap; +  remv &= ~(1<<i); +  } +  if(sizeof(s)) +  outfd->write(s); +  int add = mask & val & ~selected_attributes; +  selected_attributes |= add; +  needed_attributes |= add; +  } +  +  static void low_disable_attributes() +  { +  low_set_attributes(active_attributes, 0, 1); +  } +  +  static void low_enable_attributes() +  { +  int i, add = needed_attributes; +  string s = ""; +  +  needed_attributes &= ~add; +  for(i=0; add; i++) +  if(add & (1<<i)) { +  string cap = ({"mb","md","mh","mr","ZH","so","us"})[i]; +  if(cap && (cap = term->put(cap))) { +  s += cap; +  active_attributes |= i; +  } +  add &= ~(1<<i); +  } +  if(sizeof(s)) +  outfd->write(s); +  } +  +  void turn_on(string ... atts) +  { +  low_set_attributes(low_attribute_mask(atts), ~0); +  } +  +  void turn_off(string ... atts) +  { +  low_set_attributes(low_attribute_mask(atts), 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]));    };
pike.git/lib/modules/Stdio.pmod/Readline.pike:98:    {    return width(escapify(s));    }       void low_write(string s, void|int word_break)    {    int n = width(s);    if(!n)    return;    +  if(needed_attributes) +  low_enable_attributes(); +    // werror("low_write(%O)\n",s);       if(word_break)    {    while(xpos+n>=columns)    {    int l = columns-xpos;    string line=s[..l-1];    int spos=search(reverse(line)," ");    if(spos==-1)
pike.git/lib/modules/Stdio.pmod/Readline.pike:150:       void write(string s,void|int word_break,void|int hide)    {    low_write(escapify(s,hide),word_break);    }       void low_move_downward(int n)    {    if(n<=0)    return; +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    outfd->write(term->put("DO", n) || (term->put("do")||"")*n);    }       void low_move_upward(int n)    {    if(n<=0)    return; -  +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    outfd->write(term->put("UP", n) || (term->put("up")||"")*n);    }       void low_move_forward(int n)    {    if(n<=0)    return; -  +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    if(xpos+n<columns) {    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(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    if(xpos-n>=0) {    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);    }    }       void low_erase(int n)    {    string e = term->put("ec", n); -  +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    if (e)    outfd->write(e);    else    {    low_write(" "*n);    low_move_backward(n);    }    }       void move_forward(string s)
pike.git/lib/modules/Stdio.pmod/Readline.pike:226:    }       void erase(string s)    {    low_erase(escapified_width(s));    }       void newline()    {    string cr = term->put("cr"), down = term->put("do"); +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    if(cr && down)    outfd->write(cr+down);    else    // In this case we have ONLCR (hopefully)    outfd->write("\n");    xpos = 0;    }       void bol()    { -  +  if(active_attributes && !term->tgetflag("ms")) +  low_disable_attributes();    outfd->write(term->put("cr")||"");    xpos = 0;    }       void clear(int|void partial)    {    string s; -  +  if(active_attributes) +  low_disable_attributes();    if(!partial && (s = term->put("cl"))) {    outfd->write(s);    xpos = 0;    return;    }    if(!partial) {    outfd->write(term->put("ho")||term->put("cm", 0, 0)||"");    xpos = 0;    }    outfd->write(term->put("cd")||"");