Branch: Tag:

1999-04-30

1999-04-30 06:57:59 by Fredrik Hübinette (Hubbe) <hubbe@hubbe.net>

some optimizations, minor new features and fixes to work better in
non-blocking mode

Rev: lib/modules/Stdio.pmod/Readline.pike:1.15

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   {
48:    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..];
58:    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);
70:    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);
93:    }    }    -  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)
793:   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()
834:    {    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];
862:   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()   {
881:   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;
921:    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;
941:    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);
952:   {    if(historyobj) {    string h = historyobj->history(n, text); -  delete(0, sizeof(text)); +  delete(0, strlen(text)+strlen(prompt));    insert(h, 0);    }   }
974:    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);   }
994:    setcursorpos(sizeof(text));    output_controller->newline();    string data = text; -  if (historyobj) +  if (historyobj && !hide)    historyobj->finishline(text);    initline();    if(newline_func)
1010:    newline_func(0);   }    +    void message(string msg)   {    int p = cursorpos;
1023:    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"));   }