From ec8a23505dfd08173a3c2874a304c1e48df72570 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Thu, 29 Apr 1999 23:57:59 -0700
Subject: [PATCH] some optimizations, minor new features and fixes to work
 better in non-blocking mode

Rev: lib/modules/Stdio.pmod/Readline.pike:1.15
---
 lib/modules/Stdio.pmod/Readline.pike | 150 ++++++++++++++++++++++-----
 1 file changed, 123 insertions(+), 27 deletions(-)

diff --git a/lib/modules/Stdio.pmod/Readline.pike b/lib/modules/Stdio.pmod/Readline.pike
index d494ed59c6..1c471e4b5a 100644
--- a/lib/modules/Stdio.pmod/Readline.pike
+++ b/lib/modules/Stdio.pmod/Readline.pike
@@ -1,4 +1,4 @@
-// $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,9 +48,32 @@ class OutputController
     return columns;
   }
 
-  static string escapify(string s)
-  {
-    for(int i=0; i<strlen(s); i++)
+  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)
@@ -58,8 +81,10 @@ class OutputController
       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,22 +95,50 @@ class OutputController
     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) {
-      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")||"")+(term->put("do")||"\n"));
+
+//    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;
+	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,9 +146,9 @@ class OutputController
     }
   }
 
-  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,7 +846,7 @@ class DefaultEditKeys
 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,7 +887,7 @@ class History
   {
     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,6 +915,7 @@ 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()
 {
@@ -881,10 +935,30 @@ string get_prompt()
 string set_prompt(string newp)
 {
   string oldp = prompt;
-  prompt = newp;
+  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,12 +995,12 @@ void insert(string s, int p)
   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,7 +1015,7 @@ void delete(int p1, int p2)
   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,7 +1026,7 @@ 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);
   }
 }
@@ -974,9 +1048,11 @@ void redisplay(int clear, int|void nobackup)
     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,7 +1070,7 @@ string newline()
   setcursorpos(sizeof(text));
   output_controller->newline();
   string data = text;
-  if (historyobj)
+  if (historyobj && !hide)
     historyobj->finishline(text);
   initline();
   if(newline_func)
@@ -1010,6 +1086,7 @@ void eof()
     newline_func(0);    
 }
 
+
 void message(string msg)
 {
   int p = cursorpos;
@@ -1023,9 +1100,28 @@ void message(string msg)
   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"));
 }
 
-- 
GitLab