Recent

Author Topic: [SOLVED] Console Output  (Read 13479 times)

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
[SOLVED] Console Output
« on: December 29, 2018, 08:05:26 pm »
Hello there,
I am triying to build a simple application, which can run on a headless server and i wanted to implement auto completion.
So i tried with crt and video, but both had problems, which i couldn't solve.
I was trying to make a "input box" at the bottom, where you type your commands and a log above where you see the output from the application, but there was always something, that i couldn't solve, like clearing only a line instead of clearing the entire screen the whole time and sometimes the log wouldn't appear.

I am now asking if anyone has an Idea for creating such a commandline interface.
« Last Edit: September 24, 2019, 05:59:43 pm by JoeJoeTV »
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Console Output
« Reply #1 on: December 29, 2018, 08:26:09 pm »
Hello there,
I am triying to build a simple application, which can run on a headless server and i wanted to implement auto completion.
So i tried with crt and video, but both had problems, which i couldn't solve.
I was trying to make a "input box" at the bottom, where you type your commands and a log above where you see the output from the application, but there was always something, that i couldn't solve, like clearing only a line instead of clearing the entire screen the whole time and sometimes the log wouldn't appear.

I am now asking if anyone has an Idea for creating such a commandline interface.
sorry. Not enough information to answer properly.
1) why the existing libraries did not work? be specific there might be a simple solution to an existing problem.
2) if the server is headless where do yo expect the output to show up?
3) try to forget about clearing anything. You start by writing a bunch of line feed characters to place your cursor at the bottom of the screen, after that you simply write a line feed first then write the line data you need to show this will force the screen to scroll up a line and clear the bottom line of the screen.

Sorry but with out any more information there is nothing anyone can do.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Console Output
« Reply #2 on: December 29, 2018, 08:47:58 pm »
Also, please: if you can, share (some of) your code so that we can see what you tried and maybe find why it didn't work.

I have made in the past interfaces similar to what you're talking and never had (more than the normal) problems.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14390
  • Sensorship about opinions does not belong here.
Re: Console Output
« Reply #3 on: December 29, 2018, 09:23:46 pm »
You can't use the video unit for a headless server.  But crt should work. The video unit relies on the video card hardware of the actual machine. Both units are DOS era legacy, though.
But anyway: auto-completion can simply be implemented and should not rely on either of the two units.
« Last Edit: December 29, 2018, 09:27:31 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Console Output
« Reply #4 on: December 29, 2018, 10:45:06 pm »
First off all, thank you for repliying so fast.

Hello there,
I am triying to build a simple application, which can run on a headless server and i wanted to implement auto completion.
So i tried with crt and video, but both had problems, which i couldn't solve.
I was trying to make a "input box" at the bottom, where you type your commands and a log above where you see the output from the application, but there was always something, that i couldn't solve, like clearing only a line instead of clearing the entire screen the whole time and sometimes the log wouldn't appear.

I am now asking if anyone has an Idea for creating such a commandline interface.
sorry. Not enough information to answer properly.
1) why the existing libraries did not work? be specific there might be a simple solution to an existing problem.
2) if the server is headless where do yo expect the output to show up?
3) try to forget about clearing anything. You start by writing a bunch of line feed characters to place your cursor at the bottom of the screen, after that you simply write a line feed first then write the line data you need to show this will force the screen to scroll up a line and clear the bottom line of the screen.

Sorry but with out any more information there is nothing anyone can do.

1) Well most of the time, the problem was, that i either had something working, but it was really slow, or the output didn't show up right.
2) Well I have a linux server where i want to run it and i can ssh into it, so i can't have a graphical interface.
3) Ok i will try that.

Also, please: if you can, share (some of) your code so that we can see what you tried and maybe find why it didn't work.

I have made in the past interfaces similar to what you're talking and never had (more than the normal) problems.

Well here is my first try with crt( sorry for the messy code, i was really frustrated):

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7.   Classes, SysUtils, CustApp
  8.   { you can add units after this }
  9.   , crt, Graph
  10.   ;
  11.  
  12. type
  13.   StringArray= Array of String;
  14.  
  15.   { TLNETServer }
  16.  
  17.   TLNETServer = class(TCustomApplication)
  18.   protected
  19.     procedure DoRun; override;
  20.   public
  21.     constructor Create(TheOwner: TComponent); override;
  22.     destructor Destroy; override;
  23.     procedure WriteHelp; virtual;
  24.     procedure RunOnce();
  25.   private
  26.     RanOnce: Boolean;
  27.   end;
  28.  
  29. //procedure updateText(newText: String);
  30.  
  31. { TLNETServer }
  32.  
  33. var
  34.   address: String;
  35.   port: Integer;
  36.  
  37.   currentInputString: String;
  38.   currentChar: Char;
  39.   consolelog: StringArray;
  40.   cmdBuffer: StringArray;
  41.  
  42. //Console Procedures
  43.  
  44. procedure removeLine(Y: tcrtcoord);
  45. var
  46.   I: Integer;
  47. begin
  48.   GotoXY(WindMinX, Y);
  49.   for I := WindMinX to WindMaxX do
  50.     begin
  51.       GotoXY(I, Y);
  52.       Write(' ');
  53.     end;
  54. end;
  55.  
  56. procedure updateLog();
  57. var
  58.   I: Integer;
  59.   logIndex: Integer;
  60.   currentRow: Integer;
  61.   maxRow: Integer;
  62. begin
  63.   if Length(consolelog) > 0 then
  64.     begin
  65.       maxRow := WindMaxY - 1;
  66.       for I := maxRow downto WindMinY do
  67.       begin
  68.         logIndex := maxRow - I;
  69.         if logIndex >= Low(consolelog) then
  70.           begin
  71.             GotoXY(WindMinX,I);
  72.             Write(consolelog[logIndex]);
  73.           end;
  74.       end;
  75.  
  76.     end;
  77. end;
  78.  
  79. procedure logMessage(msg: String);
  80. begin
  81.   SetLength(consolelog,Length(consolelog)+1);
  82.   consolelog[High(consolelog)] := msg;
  83.   updateLog();
  84. end;
  85.  
  86. procedure updateText(newText: String);
  87. begin
  88.   GotoXY(WindMinX,WindMaxY);
  89.   DelLine;
  90.   Write(newText);
  91. end;
  92.  
  93. procedure checkKeyInput();
  94. begin
  95.   if KeyPressed then
  96.   begin
  97.     currentChar := ReadKey;
  98.     if currentChar = #0 then
  99.       begin
  100.         currentChar := ReadKey;
  101.         //WriteLn('SPECIAL KEY: '+inttostr(Ord(currentChar)));
  102.       end
  103.     else
  104.       begin
  105.         if (Ord(currentChar) < 32) and (Ord(currentChar) <> 0) then
  106.           begin
  107.             case Ord(currentChar) of
  108.               8: begin
  109.                   if Length(currentInputString) > 0 then
  110.                     Delete(currentInputString,Length(currentInputString)-1,1);
  111.  
  112.                   updateText(currentInputString);
  113.                 end;
  114.               13: begin
  115.                   ClrScr;
  116.                   //WriteLn('Command: '+currentInputString);
  117.                   logMessage(currentInputString);
  118.                   currentInputString := '';
  119.  
  120.                   updateText(currentInputString);
  121.                 end
  122.               else
  123.                 begin
  124.                   Write(Char(7));
  125.                   updateText(currentInputString);
  126.                 end;
  127.             end;
  128.           end
  129.         else
  130.           begin
  131.             currentInputString := currentInputString + currentChar;
  132.             //WriteLn('NORMAL KEY: '+inttostr(Ord(currentChar))+' ('+currentChar+')');
  133.             updateText(currentInputString);
  134.           end;
  135.       end;
  136.   end;
  137. end;
  138.  
  139. procedure TLNETServer.DoRun;
  140. var
  141.   ErrorMsg: String;
  142. begin
  143.   // quick check parameters
  144.   ErrorMsg:=CheckOptions('ha:p:','help address: port:');
  145.   if ErrorMsg<>'' then begin
  146.     WriteLn(ErrorMsg);
  147.     ShowException(Exception.Create(ErrorMsg));
  148.     Terminate;
  149.     Exit;
  150.   end;
  151.  
  152.   // parse parameters
  153.   if HasOption('h', 'help') then begin
  154.     WriteHelp;
  155.     Terminate;
  156.     Exit;
  157.   end;
  158.  
  159.   if HasOption('a', 'address') then begin
  160.     writeln('found option address(a) with argument '+GetOptionValue('a','address'));
  161.     readln;
  162.   end;
  163.  
  164.   if RanOnce = false then
  165.     RunOnce();
  166.  
  167.   if HasOption('p', 'port') then begin
  168.     writeln('found option port(p) with argument '+GetOptionValue('p','port'));
  169.     ReadLn;
  170.   end;
  171.  
  172.   { add your program here }
  173.  
  174.   checkKeyInput();
  175.  
  176.   // stop program loop
  177.   //Terminate;
  178. end;
  179.  
  180. constructor TLNETServer.Create(TheOwner: TComponent);
  181. begin
  182.   inherited Create(TheOwner);
  183.   StopOnException:=True;
  184. end;
  185.  
  186. destructor TLNETServer.Destroy;
  187. begin
  188.   inherited Destroy;
  189. end;
  190.  
  191. procedure TLNETServer.WriteHelp;
  192. begin
  193.   { add your help code here }
  194.   writeln('Usage: ', ExeName, ' -h');
  195. end;
  196.  
  197. procedure TLNETServer.RunOnce;
  198. begin
  199.   WriteLn(inttostr(WindMinX));
  200.   ReadLn;
  201.   ClrScr;
  202.   GotoXY(WindMinX,1);
  203.   Write('ConsoleApplication.Size: '+inttostr(ConsoleApplication.Size));
  204.   GotoXY(WindMinX,2);
  205.   Write('Graph.getMaxX: '+inttostr(Graph.GetMaxX));
  206.   GotoXY(WindMinX,3);
  207.   Write('crt.WindMinX: '+inttostr(crt.WindMinX));
  208.   GotoXY(WindMinX,4);
  209.   Write('crt.WindMinY: '+inttostr(crt.WindMinY));
  210.   GotoXY(WindMinX,5);
  211.   Write('crt.WindMaxX: '+inttostr(crt.WindMaxX));
  212.   GotoXY(WindMinX,6);
  213.   Write('crt.WindMaxY: '+inttostr(crt.WindMaxY));
  214.   Read;
  215.   //crt.ClrScr();
  216.   GotoXY(1,3);
  217.   //Sleep(1000);
  218.   Write(' ');
  219.   removeLine(3);
  220.   Read;
  221.       {
  222.   ClrScr;
  223.   GotoXY(10,10);
  224.   Write('10,10');
  225.   GotoXY(70,20);
  226.   Write('70,20');
  227.   GotoXY(1,22);
  228.   Write('lol'); }
  229.  
  230.   RanOnce := true;
  231. end;
  232.  
  233. var
  234.   Application: TLNETServer;
  235. begin
  236.   Application:=TLNETServer.Create(nil);
  237.   Application.Title:='lNetServer';
  238.   Application.Run;
  239.   Application.Free;
  240. end.
  241.  

I also tried with video, but since Thaddy said i can't use that, because it actually uses the video card and drivers i don't think it would be useful if i posted that.

You can't use the video unit for a headless server.  But crt should work. The video unit relies on the video card hardware of the actual machine. Both units are DOS era legacy, though.
But anyway: auto-completion can simply be implemented and should not rely on either of the two units.

I just tested with the video unit and I didn't even know it uses the video driver.
And how can you implement auto-completion?

EDIT:
Something line this is what I would like to do:
+---------------------------------------------------+
| Command Prompt                                _ # X |
+---------------------------------------------------+
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|Test Info                                                       |
|> Enter Command                                         |
+---------------------------------------------------+


EDIT 2:
Also, since i wanna make a simple server, i tried making inputting text not interrupt the code.
« Last Edit: December 29, 2018, 11:00:33 pm by JoeJoeTV »
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

mpv

  • Newbie
  • Posts: 6
Re: Console Output
« Reply #5 on: December 29, 2018, 11:08:20 pm »
Its a offtopic, but bash already implement auto completion. All you need to do is a small script for your program.
The good tutorials is in    this article for example[url]

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Console Output
« Reply #6 on: December 29, 2018, 11:11:48 pm »
Its a offtopic, but bash already implement auto completion. All you need to do is a small script for your program.
The good tutorials is in    this article for example[url]

That's a good idea, but i rather would do it in free pascal directly (also, because I mainly use windows and i just have a linux server).

But thanks for suggesting.
« Last Edit: December 29, 2018, 11:13:35 pm by JoeJoeTV »
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Console Output
« Reply #7 on: December 29, 2018, 11:59:59 pm »
2) Well I have a linux server where i want to run it and i can ssh into it, so i can't have a graphical interface.

Both crt and video (and graphic!) assume you have direct access to a PC compatible video card. Your best bet is to prescind of them and implement the routines you need using standard dumb-terminal control codes. I'm not well versed in ssh but IIRC the client interface basically emulates a terminal (ANSI/VT100 is one of the most common).

The relatively difficult part then is communication through the ssh tunnel. At least for me, so I can't help with that.

Regarding your code, it's a bit messy even for a normal program. For example, checkKeyInput(); could be halved and made a lot quicker; or  your removeLine(Y: tcrtcoord); which could be easily be implemented as:
Code: Pascal  [Select][+][-]
  1. procedure removeLine(Y: tcrtcoord);
  2. begin
  3.   GotoXY(1, Y);
  4.   ClrEol; {or DelLine if you want to really remove it!}
  5. end;
and lots of details like this. Sorry if it sounds harsh (not my intention) but I think you should know it. But then that's how everyone learns: by having their code taken to pieces :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Console Output
« Reply #8 on: December 30, 2018, 03:35:37 pm »
Did you try "ncrt" ? Afaik it is more vt like

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Console Output
« Reply #9 on: December 30, 2018, 05:48:06 pm »
That's a good idea, but i rather would do it in free pascal directly (also, because I mainly use windows and i just have a linux server).

Why not in Lazarus? I have made a Terminal program: https://github.com/t-edson/Tito-s-Terminal

Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Console Output
« Reply #10 on: December 31, 2018, 02:56:01 pm »
2) Well I have a linux server where i want to run it and i can ssh into it, so i can't have a graphical interface.

Both crt and video (and graphic!) assume you have direct access to a PC compatible video card. Your best bet is to prescind of them and implement the routines you need using standard dumb-terminal control codes. I'm not well versed in ssh but IIRC the client interface basically emulates a terminal (ANSI/VT100 is one of the most common).

The relatively difficult part then is communication through the ssh tunnel. At least for me, so I can't help with that.

Regarding your code, it's a bit messy even for a normal program. For example, checkKeyInput(); could be halved and made a lot quicker; or  your removeLine(Y: tcrtcoord); which could be easily be implemented as:
Code: Pascal  [Select][+][-]
  1. procedure removeLine(Y: tcrtcoord);
  2. begin
  3.   GotoXY(1, Y);
  4.   ClrEol; {or DelLine if you want to really remove it!}
  5. end;
and lots of details like this. Sorry if it sounds harsh (not my intention) but I think you should know it. But then that's how everyone learns: by having their code taken to pieces :D

I just tried running a simple test program with crt and another one with video on my linux server and it worked no problem. Then I checked if a video driver is installed and afaik it isn't.
And I think you missunderstood the ssh part. I just use Putty to connect to my server, because it's headless. It's just a remote console. I think it's just like if you open a terminal on a desktop linux.
And I know that this code isn't pretty, but it was my first try with this and i just wanted to get it to work. I also had a more prettier verion, but iI think I deleted it, because it also didn't work.

Also, I didn't know ClrEol existed, thak you for showing me. And you don't sound that harsh.  :D

Did you try "ncrt" ? Afaik it is more vt like

I think that's only for unix terminals and don't think I can use it, sice I am developing on a Windows machine.

That's a good idea, but i rather would do it in free pascal directly (also, because I mainly use windows and i just have a linux server).

Why not in Lazarus? I have made a Terminal program: https://github.com/t-edson/Tito-s-Terminal



 I am using Lazarus, but I can't use a GUI application, because i want to run it on a headless server.
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Console Output
« Reply #11 on: December 31, 2018, 03:08:42 pm »
I just tried running a simple test program with crt and another one with video on my linux server and it worked no problem. Then I checked if a video driver is installed and afaik it isn't.

To make it clear (for me): you connected through ssh (with PuTTY or whatever client) and run a program in the server which uses crt/video, and it displayed OK in the client? If so that goes inmediatly to my "Things one must know.txt" file. :)

I always though a ssh connection was akin (UI wise) to a dumb terminal.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Console Output
« Reply #12 on: December 31, 2018, 03:27:23 pm »
I just tried running a simple test program with crt and another one with video on my linux server and it worked no problem. Then I checked if a video driver is installed and afaik it isn't.

To make it clear (for me): you connected through ssh (with PuTTY or whatever client) and run a program in the server which uses crt/video, and it displayed OK in the client? If so that goes inmediatly to my "Things one must know.txt" file. :)

I always though a ssh connection was akin (UI wise) to a dumb terminal.

Yes, I connect with PuTTY, start the programm and it displayed ok and I use screen or tmux to run the server in the background while i'm not connected.
And I think the ssh connection is just a remote terminal.

And my main goal is, to be able to input something without interrupting the program.
« Last Edit: December 31, 2018, 03:31:12 pm by JoeJoeTV »
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Console Output
« Reply #13 on: December 31, 2018, 04:56:24 pm »
Why not in Lazarus? I have made a Terminal program: https://github.com/t-edson/Tito-s-Terminal
I am using Lazarus, but I can't use a GUI application, because i want to run it on a headless server.
I 'm confused.  %) I understand you want a Terminal client to run in Windows, for connect to a Server. FAIK, you have nothing to do in the Server, just open a SSH connection from a remote client and that client is in Windows.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

JoeJoeTV

  • Jr. Member
  • **
  • Posts: 65
  • Hobbyist Programmer
    • Useless Website
Re: Console Output
« Reply #14 on: December 31, 2018, 05:06:40 pm »
Why not in Lazarus? I have made a Terminal program: https://github.com/t-edson/Tito-s-Terminal
I am using Lazarus, but I can't use a GUI application, because i want to run it on a headless server.
I 'm confused.  %) I understand you want a Terminal client to run in Windows, for connect to a Server. FAIK, you have nothing to do in the Server, just open a SSH connection from a remote client and that client is in Windows.

I think you misunderstood me, I want to create a basic server in free pascal/lazraus which runs in console on the server. I already have a basic client in lazarus, but i still need the server.
And my main question is, how i can get input while not interrupting the program and how can I make it look not that ugly.
Lazarus 2.2.4 / FPC 3.2.2 / 64bit / Linux Mint 21.1 Cinnamon
https://github.com/JoeJoeTV

 

TinyPortal © 2005-2018