Recent

Author Topic: What control to use to mimic a console window?  (Read 2845 times)

new2thisagain395

  • New Member
  • *
  • Posts: 19
What control to use to mimic a console window?
« on: March 24, 2024, 12:46:11 am »
I'm writing an app that (currently) just writes to the console (using writeln etc.) and accepts user input by monitoring keystrokes.  Works fine.

But if I want to do this within a windowed Lazarus application (essentially an endlessly-scrolling window like a video terminal), what control do I use?  I've been reading through all of the controls descriptions and can't find one that appears to do what I want.

TRon

  • Hero Member
  • *****
  • Posts: 4375
Re: What control to use to mimic a console window?
« Reply #1 on: March 24, 2024, 01:08:18 am »
But if I want to do this within a windowed Lazarus application (essentially an endlessly-scrolling window like a video terminal), what control do I use?  I've been reading through all of the controls descriptions and can't find one that appears to do what I want.
TMemo is able to do that for you. You can reroute the output error channels to add the output to the strings property. Use the background to set another background and a fixed (mono) font.
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2154
    • http://www.cdbc.dk
Re: What control to use to mimic a console window?
« Reply #2 on: March 24, 2024, 01:09:17 am »
Hi
edit: TRon beat me to it  :D
Use a 'TMemo' and do this:
Code: Pascal  [Select][+][-]
  1. ...
  2. {when the /screen/ gets to aLineCount lines, it starts scrolling, default is 24}
  3. procedure AddStrToMemoScroll(const aStr: string; aLineCOunt: integer = 24);
  4. begin
  5.   if Memo1.Lines.Count >= aLineCOunt then Memo1.Lines.Delete(0);
  6.   Memo1.Lines.add(aStr);
  7. end;
  8. ...
it will mimic a scrolling console
Regards Benny
« Last Edit: March 24, 2024, 01:11:47 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Leledumbo

  • Hero Member
  • *****
  • Posts: 8801
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: What control to use to mimic a console window?
« Reply #3 on: March 24, 2024, 11:03:57 am »
https://wiki.lazarus.freepascal.org/CmdLine, install from OPM, wiki is outdated.
« Last Edit: March 24, 2024, 11:05:35 am by Leledumbo »

new2thisagain395

  • New Member
  • *
  • Posts: 19
Re: What control to use to mimic a console window?
« Reply #4 on: March 24, 2024, 03:57:11 pm »
So, I've been working on this for a few hours, migrating from a console program to a simple window program using a TMemo control.  I'm getting there.

To prevent what would be considered normal functionality in this TMemo control (such as the ability to highlight text, edit, move the cursor up and down, etc.), which I don't want any of, how do I disable all of that?  Do I need to set event handlers for certain user-driven functions such as noted above and don't have them call the base event handler code?  Or is there an easier way?   Thanks.

Edit: I'm still not sure if this is the best approach to take (using this control).  The way this application works is, there's a series of cascaded menus that the user makes single-keystroke selections from.   After 2, 3, or 4 (usually) of these selections they have to enter in some text and hit Return.  It does what was requested and restarts at the topmost menu.  Something similar to this:

Code: [Select]
$Run, Load, Set, Display, Transfer, scheduLe, Bye?  S
$Value, Definition, History, Quit?  V

Item:  ABCOBJECT (CR)
Value: 65 (CR)

Ok

$Run, Load, Set, Display, Transfer, scheduLe, Bye?


So I know I can use the KeyPressed event to capture keypresses, but then (since the event handler doesn't have any idea where in this menu tree the user is at) I'd have to set status values or use some other method of identifying every unique 'location' in the menuing tree where the user is.  Etc.

Hope that explanation makes sense.  If there was a different control that worked more like the non-GUI console that'd probably be better.
« Last Edit: March 24, 2024, 07:19:53 pm by new2thisagain395 »

TRon

  • Hero Member
  • *****
  • Posts: 4375
Re: What control to use to mimic a console window?
« Reply #5 on: March 25, 2024, 05:00:29 am »
To prevent what would be considered normal functionality in this TMemo control (such as the ability to highlight text, edit, move the cursor up and down, etc.), which I don't want any of, how do I disable all of that?
Do realize that some of that functionality that you write that you do not want to have is standard behaviour for a terminal window, e.g. higlighting/selecting text, scroll back in the history buffer etc.

Quote
Do I need to set event handlers for certain user-driven functions such as noted above and don't have them call the base event handler code?  Or is there an easier way?   Thanks.
You would have to be a bit more specific. There are properties that allows to disable some of the functionality, other behavioural changes might require to add an eventhandler or override an existing event.

For more information about TMemo control see also TMemo LCL documentation

Quote
The way this application works is, there's a series of cascaded menus that the user makes single-keystroke selections from.   After 2, 3, or 4 (usually) of these selections they have to enter in some text and hit Return.  It does what was requested and restarts at the topmost menu.
Single keystrokes are not part of standard terminal I/O, at least not with standard Pascal functionality. So you must rely on a third party implementation to get such behaviour. As such you would have to 'emulate' non standard behaviour (yourself and doing so manually).

Quote
So I know I can use the KeyPressed event to capture keypresses, but then (since the event handler doesn't have any idea where in this menu tree the user is at) I'd have to set status values or use some other method of identifying every unique 'location' in the menuing tree where the user is.
In case you require functionality that is used multiple times then it is usually better (red easier to maintain) to create a subroutine for that, and which is able to obtain the right information at the right time. I am assuming that you rely on crt unit function keypressed. If that is the case then simply emulate the functionality. If you do that literally (wait until keypressed) then that poses a bit of a problem so you would have to have some kind of poll functionality that check whether or not a key was pressed.

To me it looks more like you are looking for a full fledged terminal emulator and afaik there isn't one that meets your criteria. A TMemo is just an easy drop in replacement for some basic functionality that could be used to interact with your code.

In that regards the suggestion from Leledumbo  to use the cmdline control is a much better solution but that is just /a/ terminal emulator solving things in its own way. If you seek crt functionality then that is not an easy job to implement.
« Last Edit: March 25, 2024, 05:15:22 am by TRon »
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2154
    • http://www.cdbc.dk
Re: What control to use to mimic a console window?
« Reply #6 on: March 25, 2024, 07:34:04 am »
Hi
Have a look at this: https://github.com/Warfley/LazTermUtils
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

new2thisagain395

  • New Member
  • *
  • Posts: 19
Re: What control to use to mimic a console window?
« Reply #7 on: March 25, 2024, 11:55:38 am »
Thanks!  I'll look that over.  As I think I noted, the console window in Windows works perfectly.  I will build an alternate using this library and see how that goes.

Thank you.

TRon

  • Hero Member
  • *****
  • Posts: 4375
Re: What control to use to mimic a console window?
« Reply #8 on: March 25, 2024, 12:22:53 pm »
As I think I noted, the console window in Windows works perfectly
As will any decent terminal emulator on/at Linux. The only thing that differs is that you need to set that up manually in Linux while windows has provisions to do that automatically on startup for you. And in case asking  why: on linux you have a choice, on window the terminal is forced.
Today is tomorrow's yesterday.

new2thisagain395

  • New Member
  • *
  • Posts: 19
Re: What control to use to mimic a console window?
« Reply #9 on: March 25, 2024, 08:42:34 pm »
So I played with the sample programs in that library.  I see they operate in the Linux console similarly to what I had been doing with the console application.

Is it correct that I need to install an xterm-compatible terminal emulator and then tie it to the code library (probably in a constructor somewhere)?

Edit: actually now that I think about it, is there a way to just redirect stdin and stdout (assuming that's what console programs are reading from and writing to) to a terminal emulator?  That might be easier.
« Last Edit: March 25, 2024, 09:44:42 pm by new2thisagain395 »

cdbc

  • Hero Member
  • *****
  • Posts: 2154
    • http://www.cdbc.dk
Re: What control to use to mimic a console window?
« Reply #10 on: March 26, 2024, 10:47:59 am »
Hi
You're 'overcomplicating' things...
Just /keep your grubby hands off the crt-unit/ then all is fine, in both /binbows/ & /lunix/ !!!! NO CRT IN USES!  :D
You can actually write your program like you think it ...and only using read, readln, write & writeln.
That's it! 'readln' & 'writeln' scrolls the screen by way of crlf <-> #13#10 and #9 'tab'ulates text...
edit: Get the above right first, then you can play with warfley's gems  %)
Regards Benny
« Last Edit: March 26, 2024, 10:49:51 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

new2thisagain395

  • New Member
  • *
  • Posts: 19
Re: What control to use to mimic a console window?
« Reply #11 on: March 26, 2024, 12:08:50 pm »
Yeah, that's probably the best way.  Searching for a function to replicate Crt's "readkey" that works on both Windows and Linux currently.

TRon

  • Hero Member
  • *****
  • Posts: 4375
Re: What control to use to mimic a console window?
« Reply #12 on: March 26, 2024, 01:07:24 pm »
Yeah, that's probably the best way.  Searching for a function to replicate Crt's "readkey" that works on both Windows and Linux currently.
unit crt works for me when launching a console application from a terminal on Linux so what exactly are you trying to solve ?

It might be beneficial to learn and understand how the (actually your) Linux desktop works and how you are suppose to launch console applications (*). If you have mastered that subject then it might become clear that there is no unified answer to what you are trying to achieve. To me it seems you are trying to defy Linux logic, and that is perfectly ok (I myself do that all the time) but that has some consequences that you would have to deal with (and quite honestly no sane person would want to burn him/herself over it doing so).

Do note that in case you are writing this console application just for yourself (and only yourself) and not for others who run another distro (with their own set of rules/configuration) that there is no harm in doing things as you please but I would still suggest to go with the flow from your distro (as that is much easier to deal with).

(*) crash course picture attached:
« Last Edit: March 26, 2024, 01:27:32 pm by TRon »
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018