Post Reply 
Terminal alternative
12-19-2014, 05:13 PM (This post was last modified: 12-25-2014 04:24 PM by Snorre.)
Post: #1
Terminal alternative
Hello,

this program is borrowed from the german HP-Prime forum.
It was written because the builtin terminal has some drawbacks (which were introduced in some recent firmware update) -- mainly very size-limited line buffer and not displaying the most recent PRINTed line.

Beware: This program does not break long lines as the builtin terminal would do, and G9 is used as drawing buffer.

Program "TERM":
Code:
EXPORT LiveView:=1;  // Visible while outputting?

BackColor:=RGB(0,0,0);
TextColor:=RGB(0,255,0);
BarBColor:=RGB(0,128,0);
BarFColor:=RGB(64,255,64);

Capacity:=9999; // = max list size
FontSize:=2;
Buffer:={};
LastTouchY,Top;

LineHeight()
BEGIN 2*FontSize+10; END;

TotalHeight()
BEGIN SIZE(Buffer)*LineHeight; END;

MaxTop()
BEGIN MAX(TotalHeight-240,0); END;

Redraw(top)
BEGIN
  // Draw buffered lines
  LOCAL h:=LineHeight;
  LOCAL n:=SIZE(Buffer),
        k:=IP(top/h),
        y:=-(top MOD h);
  DIMGROB_P(G9,320,240,BackColor);
  WHILE k<n AND y<240 DO
    TEXTOUT_P(Buffer(k:=k+1),G9,0,y,
              FontSize,TextColor);
    y:=y+h;
  END;
  // Draw scrollbar
  LOCAL t:=TotalHeight;
  IF t≥240 THEN
    y:=240*top/t;
    RECT_P(G9,318,0,319,239,BarBColor);
    RECT_P(G9,318,y,319,y+240²/t,BarFColor);
  END;
  // Show
  BLIT(G9);
  Top:=top;
END;

MoveTo(top)
BEGIN
  top:=MAX(MIN(top,MaxTop),0);
  IF top≠Top THEN Redraw(top); END;
END;

Resize(fontSize)
BEGIN
  fontSize:=MAX(MIN(fontSize,7),1);
  IF fontSize≠FontSize THEN
    FontSize:=fontSize;
    Redraw(MIN(Top,MaxTop));
  END;
END;

HandleTouch(touch)
BEGIN
  IF touch(1)≤#1 THEN
    LOCAL y:=B→R(touch(3));
    CASE
      IF touch(2)≥#300d
      THEN MoveTo(y/240*TotalHeight); END;
      IF touch(1)=#1
      THEN MoveTo(Top+LastTouchY-y); END;
    END;
    LastTouchY:=y;
  END;
END;

HandleKey(code)
BEGIN
  CASE
    IF code=7  THEN MoveTo(0); END;
    IF code=8  THEN MoveTo(MaxTop); END;
    IF code=2  THEN MoveTo(Top-80); END;
    IF code=12 THEN MoveTo(Top+80); END;
    IF code=42 THEN Resize(1); END;
    IF code=43 THEN Resize(2); END;
    IF code=44 THEN Resize(3); END;
    IF code=37 THEN Resize(4); END;
    IF code=38 THEN Resize(5); END;
    IF code=39 THEN Resize(6); END;
    IF code=32 THEN Resize(7); END;
    IF code=45 THEN Resize(FontSize-1); END;
    IF code=50 THEN Resize(FontSize+1); END;
  END;
END;

// Clear the terminal buffer.
EXPORT CLEAR()
BEGIN
  Buffer:={};
  IF LiveView THEN Redraw(0); END;
END;

// Add a single line or a list of lines to terminal buffer.
EXPORT OUT(data)
BEGIN
  LOCAL s:=SIZE(Buffer)+
           IFTE(TYPE(data)=6,SIZE(data),1);
  IF s>Capacity THEN
    Buffer:=SUB(Buffer,s-Capacity+1,Capacity);
  END;
  Buffer:=CONCAT(Buffer,data);
  IF LiveView THEN Redraw(MaxTop); END;
END;

// Show the terminal buffer contents.
EXPORT SHOW()
BEGIN
  Redraw(MIN(Top,MaxTop));
  LOCAL inp;
  REPEAT
    IF TYPE(inp:=WAIT(-1))=6
    THEN HandleTouch(inp); inp:=-1;
    ELSE HandleKey(inp); END;
  UNTIL inp=30 OR inp=4;  // leave on [Enter] or [Esc]
END;

Usage:
Replace "PRINT()" by "TERM.CLEAR()", "PRINT(data)" by "TERM.OUT(data)", and call "TERM.SHOW()" to view the buffer. (Omit the prefix "TERM." if "CLEAR", "OUT", "SHOW" don't collide with other function names).
If "TERM.LiveView" is set to 0 the program won't display anything until "SHOW()"ed.
Scroll to top/bottom by cursoring left/right, or otherwise 1/3-page up/down.
Keys [1],...,[7],[+],[-] will change font size.

Test program "TERMTEST" for comparing builtin terminal vs. "TERM":
Code:
EXPORT TERMTEST1()
BEGIN
  PRINT();
  LOCAL n:=0,s;
  REPEAT
    s:=STRINGFROMID(n:=n+1);
    PRINT(n+": "+s);
  UNTIL s="LASTSTRING";
END;

EXPORT TERMTEST2()
BEGIN
  CLEAR();
  LOCAL n:=0,s;
  REPEAT
    s:=STRINGFROMID(n:=n+1);
    OUT(n+": "+s);
  UNTIL s="LASTSTRING";
  SHOW();
END;

Any improvements are very welcome. Ideas: handle line breaks, long lines, tabulators, highlight numbers, better type handling in general etc.
This program might become obsolete eventually, when the Prime's builtin terminal is upgraded.

Greetings

[EDIT] Thread renamed to not disrupt the sequence of "HP Prime: ..." articles by E. W. Shore when alphabetically sorted.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Terminal alternative - Snorre - 12-19-2014 05:13 PM
RE: Terminal alternative - Freire - 06-04-2017, 06:33 PM
RE: Terminal alternative - compsystems - 06-06-2017, 07:58 PM



User(s) browsing this thread: 1 Guest(s)