+require "time"
+require "open3"
+
+class IO
+ def readline_nonblock
+ buffer = ""
+ buffer << read_nonblock(1) while buffer[-1] != "\n"
+ buffer
+ rescue IO::WaitReadable => blocking
+ if (not buffer.empty?)
+ ungetc(buffer)
+ end
+ raise blocking
+ end
+end
+
+
+class Ncurses::WINDOW
+ def print_line(str, hscroll=0)
+ revert_color = false
+ str[0,5].match(/\033\[3(.)m/) { |c| #Line starts with an escape sequence. We handle that `a la xterm`
+ Ncurses.init_pair(10, c[1].to_i, Ncurses::COLOR_BLACK)
+ self.attron(Ncurses.COLOR_PAIR(10))
+ revert_color = true
+ str = str[5,str.length]
+ }
+ str = str.gsub("\011"," ")
+ #Any other control char is ignored and escaped
+ str = str.gsub(/[[:cntrl:]]/) { |m|
+ "^"+(m.ord + 64).chr
+ }
+ if(hscroll > 0)
+ strcut = str[hscroll,str.length]
+ if(strcut.nil? or strcut.empty?)
+ str = ""
+ else
+ str = "…"+strcut
+ end
+ end
+ strlen = str.length
+ winlen = self.getmaxx-self.getcurx-1
+ if(strlen <= winlen)
+ self.addstr(str + " "*(winlen-strlen))
+ else
+ self.addstr(str[0,winlen-1]+"…")
+ end
+ if(revert_color)
+ self.attroff(Ncurses.COLOR_PAIR(10))
+ end
+ end
+end
+
+