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.14 1999/04/26 13:03:01 grubba Exp $ + // $Id: Readline.pike,v 1.15 1999/04/30 06:57:59 hubbe Exp $      class OutputController   {    static private object outfd, term;    static private int xpos = 0, columns = 0;    static private mapping oldattrs = 0;       void disable()    {    catch{
pike.git/lib/modules/Stdio.pmod/Readline.pike:41:    };    if(!columns)    columns = term->tgetnum("co") || 80;    }       int get_number_of_columns()    {    return columns;    }    -  static string escapify(string s) +  static string escapify(string s, void|int hide)    { -  + #if 1 +  s=replace(s, +  ({ +  "\000","\001","\002","\003","\004","\005","\006","\007", +  "\010","\011","\012","\013","\014","\015","\016","\017", +  "\177", +  "\200","\201","\202","\203","\204","\205","\206","\207", +  "\210","\211","\212","\213","\214","\215","\216","\217", +  "\220","\221","\222","\223","\224","\225","\226","\227", +  "\230","\231","\232","\233","\234","\235","\236","\237", +  }), +  ({ +  "^@","^A","^B","^C","^D","^E","^F","^G", +  "^H","^I","^J","^K","^L","^M","^N","^O", +  "^?", +  "~@","~A","~B","~C","~D","~E","~F","~G", +  "~H","~I","~J","~K","~L","~M","~N","~O", +  "~P","~Q","~R","~S","~T","~U","~V","~W", +  "~X","~Y","~Z","~[","~\\","~]","~^","~_", +  })); +  return hide ? "*"*strlen(s) : s; + #else +     for(int i=0; i<strlen(s); i++)    if(s[i]<' ')    s = s[..i-1]+sprintf("^%c", s[i]+'@')+s[i+1..];    else if(s[i]==127)    s = s[..i-1]+"^?"+s[i+1..];    else if(s[i]>=128 && s[i]<160)    s = s[..i-1]+sprintf("~%c", s[i]-128+'@')+s[i+1..];    return s; -  + #endif    }    -  +     static int width(string s)    {    return strlen(s);    }       static int escapified_width(string s)    {    return width(escapify(s));    }    -  void low_write(string s) +  void low_write(string s, void|int opt, void|int word_break)    {    int n = width(s);    if(!n)    return; -  while(xpos+n>=columns) { +  + // 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) +  { +  outfd->write(line); +  }else{ +  l=strlen(line)-spos; +  outfd->write(line[..l-2]); +  } + // while(l<strlen(s) && s[l]==' ') l++; +  s=s[l..]; +  n-=l; + // if(l!=columns || !term->tgetflag("am")) +  if(n) +  outfd->write((term->put("cr")||"")+(term->put("do")||"\n")); +  xpos = 0; +  } +  }else{ +  while(xpos+n>=columns) +  { +  int l = columns-xpos;    outfd->write(s[..l-1]); -  s = s[l..]; -  n -= l; +  s=s[l..]; +  n-=l;    xpos = 0;    if(!term->tgetflag("am"))    outfd->write((term->put("cr")||"")+(term->put("do")||"\n"));    } -  +  }    string le; -  if(xpos==0 && term->tgetflag("am") && (le=term->put("le"))) +  if(!opt && xpos==0 && term->tgetflag("am") && (le=term->put("le")))    outfd->write(" "+le);    if(n>0) {    outfd->write(s);    xpos += n;    }    }    -  void write(string s) +  void write(string s,void|int opt, void|int word_break,void|int hide)    { -  low_write(escapify(s)); +  low_write(escapify(s,hide),opt,word_break);    }       void low_move_downward(int n)    {    if(n<=0)    return;    outfd->write(term->put("DO", n) || (term->put("do")||"")*n);    }       void low_move_upward(int n)
pike.git/lib/modules/Stdio.pmod/Readline.pike:786:    {    _readline = readline;    set_default_bindings();    }      }      class History   {    static private array(string) historylist; -  static private mapping(int:string) historykeep; +  static private mapping(int:string) historykeep=([]);    static private int minhistory, maxhistory, historynum;       int get_history_num()    {    return historynum;    }       string history(int n, string text)    {    if (n<minhistory)
pike.git/lib/modules/Stdio.pmod/Readline.pike:827:    }    }    historynum = sizeof(historylist)-1+minhistory;    historykeep = ([]);    }       void finishline(string text)    {    foreach(indices(historykeep), int n)    historylist[n-minhistory]=historykeep[n]; -  historykeep = 0; +  historykeep = ([]);    historylist[-1] = text;    if(sizeof(historylist)>1 && historylist[-2]==historylist[-1])    historylist = historylist[..sizeof(historylist)-2];    }       void set_max_history(int maxhist)    {    maxhistory = maxhist;    }   
pike.git/lib/modules/Stdio.pmod/Readline.pike:855:   }         static private object(OutputController) output_controller;   static private object(InputController) input_controller;   static private string prompt="";   static private string text="", readtext;   static private function(string:void) newline_func;   static private int cursorpos = 0;   static private object(History) historyobj = 0; + static private int hide = 0;      object(OutputController) get_output_controller()   {    return output_controller;   }      object(InputController) get_input_controller()   {    return input_controller;   }      string get_prompt()   {    return prompt;   }      string set_prompt(string newp)   {    string oldp = prompt; -  +  if(newp!=prompt) +  { +  if(newline_func != read_newline) +  { +  int p=cursorpos; +  setcursorpos(0); +  output_controller->bol(); +  output_controller->clear(1);    prompt = newp; -  +  cursorpos=strlen(text); +  redisplay(0, 1); +  cursorpos=p; +  }else{ +  prompt = newp; +  } +  }    return oldp;   }    -  + void set_echo(int onoff) + { +  hide=!onoff; + } +    string gettext()   {    return text;   }      int getcursorpos()   {    return cursorpos;   }   
pike.git/lib/modules/Stdio.pmod/Readline.pike:914:    return cursorpos;   }      void insert(string s, int p)   {    if (p<0)    p = 0;    if (p>strlen(text))    p = strlen(text);    setcursorpos(p); -  output_controller->write(s); +  output_controller->write(s,0,0,hide);    cursorpos += strlen(s);    string rest = text[p..];    if (strlen(rest))    { -  output_controller->write(rest); +  output_controller->write(rest,0,0,hide);    output_controller->move_backward(rest);    }    text = text[..p-1]+s+rest;   }      void delete(int p1, int p2)   {    if (p1<0)    p1 = 0;    if (p2>strlen(text))    p2 = strlen(text);    setcursorpos(p1);    if (p1>=p2)    return; -  output_controller->write(text[p2..]); +  output_controller->write(text[p2..],0,0,hide);    output_controller->erase(text[p1..p2-1]);    text = text[..p1-1]+text[p2..];    cursorpos = strlen(text);    setcursorpos(p1);   }      void history(int n)   {    if(historyobj) {    string h = historyobj->history(n, text); -  delete(0, sizeof(text)); +  delete(0, strlen(text)+strlen(prompt));    insert(h, 0);    }   }      void delta_history(int d)   {    if(historyobj)    history(historyobj->get_history_num()+d);   }   
pike.git/lib/modules/Stdio.pmod/Readline.pike:967:   {    int p = cursorpos;    if(clear)    output_controller->clear();    else if(!nobackup) {    setcursorpos(0);    output_controller->bol();    output_controller->clear(1);    }    output_controller->check_columns(); -  if(newline_func == read_newline) +  + // This seems like a silly limitation + // if(newline_func == read_newline)    output_controller->write(prompt); -  output_controller->write(text); +  output_controller->write(text,0,0,hide);    cursorpos = sizeof(text);    setcursorpos(p);   }      static private void initline()   {    text = "";    cursorpos = 0;    if (historyobj)    historyobj->initline();   }      string newline()   {    setcursorpos(sizeof(text));    output_controller->newline();    string data = text; -  if (historyobj) +  if (historyobj && !hide)    historyobj->finishline(text);    initline();    if(newline_func)    newline_func(data);   }      void eof()   {    if (historyobj)    historyobj->finishline(text);    initline();    if(newline_func)    newline_func(0);   }    -  +    void message(string msg)   {    int p = cursorpos;    setcursorpos(strlen(text));    output_controller->newline();    foreach(msg/"\n", string l) {    output_controller->write(l);    output_controller->newline();    }    redisplay(0, 1);    setcursorpos(p);   }    -  + void write(string msg,void|int word_wrap) + { +  int p = cursorpos; +  setcursorpos(0); +  output_controller->bol(); +  output_controller->clear(1); +  array(string) tmp=msg/"\n"; +  foreach(tmp[..sizeof(tmp)-2],string l) +  { +  output_controller->write(l,1,word_wrap); +  output_controller->newline(); +  } +  output_controller->write(tmp[-1],0,word_wrap); +  +  cursorpos=strlen(text); +  redisplay(0, 1); +  setcursorpos(p); + } +    void list_completions(array(string) c)   { -  message(sprintf("%-"+output_controller->get_number_of_columns()+"#s", +  message(sprintf("%-*#s",output_controller->get_number_of_columns(),    c*"\n"));   }      static private void read_newline(string s)   {    input_controller->disable();    readtext = s;   }      void set_nonblocking(function f)