Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-05

terminal width/height

16 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

David Emerson

Hi all,

Is there a function to query the terminal width and height, as a number of characters?

For example, in windows 98, the terminal is always 80 characters wide, but the height varies; in linux, the height and width can both change, even while the program is running. I'm throwing some text up on the screen and would like to be able to format it to fit the terminal width and height.

Thanks,
David

Unknown author
to David Emerson

Quoting David Emerson :

Is there a function to query the terminal width and height, as a number of characters?

If you "use CRT", you can make use of gotoxy() and WhereX plus WhereY to find out. Below is a unit I sometimes use for this purpose.

Hope this helps,

Unit UWinSize;

{
This unit determines the size of a text window.
Just include it, and the variables will be set automatically. }

Interface
Uses CRT;
Var ScreenMaxX, ScreenMaxY : Integer;
Procedure SetScreenMax;
Implementation
Var Current, Increment : Integer;
Function Biggest(UseX : Boolean) : Integer;
Var Done : Boolean;
Begin
Repeat
   If UseX Then Begin
      GotoXY(Current,1);
      Done:= Current  WhereX;
      End
    Else Begin
      GotoXY(1,Current);
      Done:= Current  WhereY;
      End;
   If Not Done Then Current := Current + Increment;
 Until Done;
Biggest := Current - Increment;
End;
Procedure SetScreenMax;
Begin
Current := 10;
Increment := 10;
Current := Biggest(True);
Increment := 1;
ScreenMaxX := Biggest(True);
Current := 10;
Increment := 10;
Current := Biggest(False);
Increment := 1;
ScreenMaxY := Biggest(False);
End;
Var StartingX, StartingY : Integer;
Begin
StartingX := WhereX;
StartingY := WhereY;
SetScreenMax;
GotoXY(StartingX,StartingY);
End.
Michalis Kamburelis
to David Emerson: Is there a function to query the terminal width and height, as a number of characters?…
David Emerson wrote 9 lines
Hi all, Is there a function to query the terminal width and height, as a number of characters? For example, in windows 98, the terminal is always 80 characters wide, but the height varies; in linux, the height and width can both change, even while the program is running. I'm throwing some text up on the screen and would like to be able to format it to fit the terminal width and height. Thanks, David

If you use Video unit, check ScreenWidth/Height variables.

If you use Crt,
- under UNIX look also at ScreenWidth/Height variables.
- Under Win32 (I say it only looking at Crt sources) you should be able to use initial values of WindMaxX and WindMaxY (they seem to be initialized at the initialization of Crt on Win32 to screen size).

BTW to devels interested in Crt unit:

- under UNIX WindMaxX, WindMaxY, WindMinX, WindMinY are broken. Unix Crt implementation uses private variables WinMaxX, WinMaxY, WinMinX, WinMinY (without "d" in the middle), I think that this should be changed to use public variables WindMaxX, WindMaxY, WindMinX, WindMinY.

- under Win32, functions GetScreenWidth/Height could be made public under names ScreenWidth/Height (without "Get"), this would be compatible with Unix Crt.

Michalis

Michael Van Canneyt
to David Emerson: Is there a function to query the terminal width and height, as a number of characters?…
David Emerson wrote 5 lines
Hi all, Is there a function to query the terminal width and height, as a number of characters? For example, in windows 98, the terminal is always 80 characters wide, but the height varies; in linux, the height and width can both change, even while the program is running. I'm throwing some text up on the screen and would like to be able to format it to fit the terminal width and height.

On unix, use fpioctl:

fpioctl(stdinputhandle, TIOCGWINSZ, @WS);

where ws is TWinSize:

  TWinsize = packed record
    ws_row,
    ws_col,
    ws_xpixel,
    ws_ypixel : word;
  end;

fpioctl is in unit unix, TWinSize in unit termio.

Michael.

L505
to Michael Van Canneyt
function FindPart(const HelpWilds: String; const InputStr: String):Integer;"

According to the function syntax, it returns an integer...

Description FindPart searches the string InputStr and returns the first string that matches

the wildcards specification in HelpWilds . If no >match is found, and empty string is returned. valid wildcards are the "?" (question mark) and "*" (asterisk) characters."

According to the description it returns a String. Does it return an integer though?
I'm just about to use this function actually, in a wiki I'm building.. so I wanted to ask you guys if maybe the docs are misworded?

Also, is there any way to update these docs or add notes to them? I'm working on a freepascal based wiki, and I might integrate the docs into a system where users can add comments to the docs.

Funny thing also is, due to my laziness, I wished to check into/verify what the function returns this with lazarus code insight.Aand the list box isn't wide enough to see what the function returns ;-) There's another project for me or someone: turning the lazarus list box into something expandable or an optional dockable side panel type thing.

Lars

L505
to L505

| FindPart searches the string InputStr and returns the first string that matches | the wildcards specification in HelpWilds . If no match is found, and empty string | is returned. valid wildcards are the "?" (question mark) and "*" (asterisk) | characters."

To answer my own question ;-) here is the proposed help doc changes.

Description

FindPart searches the string InputStr and returns an integer, which is the first position of a string matched. The HelpWilds constant is a string which can contain Help Wilds. If no match is found, the integer 0 is returned. Valid Help Wilds are the "?" (question mark) and "*" (asterisk) characters.

Notes: The positioning starts at 1, not 0.
0 indicates no match found, and 1 indicates the first position. This is because the creators of freepascal are not mathematecians. (ok omit this one).

Example program:

program Project1;
{$mode objfpc}{$H+}

uses
Classes,
sysutils,

  strutils;
begin
  writeln('Hello, where is "is" in "thisisatest"?');
  readln;
{should return 3...}
  writeln('It is at position: '+inttostr(FindPart('is','thisisatest')));
  readln;
{should return 0...}
 writeln('inttostr(FindPart('ZZZXXX','thisisatest')));
  readln;
end.

The program should return the integer "3" due to the fact that the first instance of "is" happens to be in the 3rd position in "thisisatest".

L505
to Michael Van Canneyt: On unix, use fpioctl: fpioctl(stdinputhandle, TIOCGWINSZ, @WS); where ws is TWinSize:…
Show quoted text 15 lines
Mattias Gaertner nc-gaertnma netcologne.de Wed, 30 Oct 2002 15:56:09 +0100 > Another option is > http://anso.virtualave.net/delphi_stuff.htm#TRegExpr > > But I doubt that 1.0.x will eat it. Even for 1.1 one or > two things need a fix. I downloaded it, changed a few things for mode objfpc 1.0.x and tested some examples successfully. What will not work? Mattias

Attn: Mattias..(or anyone else who knows about regexpr units)

Do you know which unit this is that you talked about of above? I'm looking for regexpr units and I wondered if this unit worked for you with those minor changes you made. The URL does not work and I wondered which unit that was. If it was the Regexpr Studio, one then I have it. It seems to be geared toward Win32. I will modify it for linux maybe if this is possible, but I thought maybe you had already found something yourself, or mucked around and got something working.
I'm looking for a Linux capable regexpr unit with back referencing available.

Lars

Michalis Kamburelis
to L505: I think maybe another section "Contributed Examples" could be useful in addition to…
L505 wrote 25 lines
Hi, I think maybe another section "Contributed Examples" could be useful in addition to contributed units In this cloned section, it will appear similar to the Contributed Units section, but in fact it is a "contributed examples" section. This way users can upload descriptions of their example programs designed with freepascal, and point to a url and FTP to download that example. (i.e. small programs demonstrating how to use freepascal and her units). Another idea would be to have another cloned section called "Contributed Programs". This section could contain all programs designed with freepascal that are fully working shipped with sources and/or binaries. I see some fully working binary programs right now in the Misc section but I think they'd be good having their own section on the site. This way users can upload descriptions of their functional programs designed with freepascal, and point to a url and FTP to download that functional program. (i.e. freecommander, DelphiOS, FPS - IDE, etc.). What do you think? Also, if you want me to help let me know.. I can maybe do things like this.. are you guys using PHP and MySql to manage the site? I may also just make a dedicated freepascal section on my domain and server.. doing the above tasks using PSP, so either way. Lars

I think that you can create such longer descriptions of contributed things in FPC wiki, [http://www.freepascal.org/wiki/]. On main page there is a link to "Contributed Units". Just follow it, create new page, and list there various pages describing contributed things. Then you can create whole pages dedicated to some particular contributed things.

Michalis

L505
to Michael Van Canneyt: On unix, use fpioctl: fpioctl(stdinputhandle, TIOCGWINSZ, @WS); where ws is TWinSize:…

Hi,

I think maybe another section "Contributed Examples" could be useful in addition to contributed units In this cloned section, it will appear similar to the Contributed Units section, but in fact it is a "contributed examples" section. This way users can upload descriptions of their example programs designed with freepascal, and point to a url and FTP to download that example. (i.e. small programs demonstrating how to use freepascal and her units).

Another idea would be to have another cloned section called "Contributed Programs". This section could contain all programs designed with freepascal that are fully working shipped with sources and/or binaries. I see some fully working binary programs right now in the Misc section but I think they'd be good having their own section on the site. This way users can upload descriptions of their functional programs designed with freepascal, and point to a url and FTP to download that functional program. (i.e. freecommander, DelphiOS, FPS - IDE, etc.).

What do you think?

Also, if you want me to help let me know.. I can maybe do things like this.. are you guys using PHP and MySql to manage the site?

I may also just make a dedicated freepascal section on my domain and server.. doing the above tasks using PSP, so either way.

Lars

L505
to Michalis Kamburelis: I think that you can create such longer descriptions of contributed things in FPC wiki,…

|
| I think that you can create such longer descriptions of contributed | things in FPC wiki, [http://www.freepascal.org/wiki/]. On main page | there is a link to "Contributed Units". Just follow it, create new page, | and list there various pages describing contributed things. Then you can | create whole pages dedicated to some particular contributed things. |
| Michalis
|

And throw it all in the Misc section? Just seems like the Misc section would get overloaded, with all this stuff.

CoLinux505
to Michalis Kamburelis: I think that you can create such longer descriptions of contributed things in FPC wiki,…

| I think that you can create such longer descriptions of contributed | things in FPC wiki, [http://www.freepascal.org/wiki/]. On main page | there is a link to "Contributed Units". Just follow it, create new page, | and list there various pages describing contributed things. Then you can | create whole pages dedicated to some particular contributed things. |

Ahh I see now, in the actual wiki there are info regarding contributed units. I think the reason I couldn't ever find these pages was because you have to log in. Does this wiki get indexed by the search engines like google if there is login required?

I have been using the old freepascal website to download stuff, I didn't really know about the wiki. So something must not be getting indexed.

Lars

L505

|
| Ahh I see now, in the actual wiki there are info regarding contributed units. I think the reason I couldn't | ever find these pages was because you have to log in. Does this wiki get indexed by the search engines like | google if there is login required?
|
| I have been using the old freepascal website to download stuff, I didn't really know about the wiki. So | something must not be getting indexed.
|

Ohhh now I see, it's because the contributed units page is blank. So -I- have to do the work and start filling it in now ;-)
I thought maybe that someone had at least already filled the page in with a couple of words.

Well, it seems I've spammed quite a few emails to the freepascal mailing list today, but at least I am making progress with freepascal projects today ;-)

Lars

Michael Van Canneyt
to L505: Something I am unsure about here (maybe I will need to implement it): When using FindPart…
L505 wrote 5 lines
Something I am unsure about here (maybe I will need to implement it): When using FindPart function, and utilizing wild cards, I cannot figure a way to calculate the length of the string found. Only the matching start position of the found string. Any ideas?

Create an overloaded version with an extra
Var ResLength : Integer
parameter

And have the current version call this one with a dummy length param.

Michael.

Mattias Gaertner
to L505: Attn: Mattias..(or anyone else who knows about regexpr units) Do you know which unit…
Show quoted text 19 lines
>Mattias Gaertner nc-gaertnma netcologne.de >Wed, 30 Oct 2002 15:56:09 +0100 >> Another option is >> http://anso.virtualave.net/delphi_stuff.htm#TRegExpr >> >> But I doubt that 1.0.x will eat it. Even for 1.1 one or >> two things need a fix. >I downloaded it, changed a few things for mode objfpc 1.0.x and tested >some examples successfully. >What will not work? > >Mattias Attn: Mattias..(or anyone else who knows about regexpr units) Do you know which unit this is that you talked about of above?

I don't remember. In the last years I used several regexpr units with fpc. synregexpr works with perl style regular expressions.

Show quoted text 8 lines
I'm looking for regexpr units and I wondered if this unit worked for you with those minor changes you made. The URL does not work and I wondered which unit that was. If it was the Regexpr Studio, one then I have it. It seems to be geared toward Win32. I will modify it for linux maybe if this is possible, but I thought maybe you had already found something yourself, or mucked around and got something working. I'm looking for a Linux capable regexpr unit with back referencing available.

synregexpr does (e.g. $1).

Mattias

L505
to Mattias Gaertner

| > Attn: Mattias..(or anyone else who knows about regexpr units) | >
| > Do you know which unit this is that you talked about of above? |
| I don't remember. In the last years I used several regexpr units with fpc. | synregexpr works with perl style regular expressions. |
|
| > I'm
| > looking for regexpr units and I wondered if this unit worked for you with | > those minor changes you made. The URL does not work and I wondered which | > unit that was. If it was the Regexpr Studio, one then I have it. It seems | > to be geared toward Win32. I will modify it for linux maybe if this is | > possible, but I thought maybe you had already found something yourself, or | > mucked around and got something working. I'm looking for a Linux capable | > regexpr unit with back referencing available.
|
| synregexpr does (e.g. $1).
|
|

Synregexpr is a similar unit (basically exactly the same) as the one I have used for years in my Delphi projects.. so that is great to find that! It was just a matter of finding it, since there has been little discussion of regexes it on the mailing lists ;-)

I have uploaded this working regular expression unit (modified a bit) to the contributed units section on the freepascal website.

That unit has worked fine for me in Delphi, so I'm all set for freepascal now. The only thing I noticed when using it in Delphi was that it was a bit slow in a custom program I built when I used it to modify about 10,000 files doing a long 10-30 minute search and replace.. (compared to some other C++ programs I have used). But I rarely do that - usually just 1-10 files that I need to modify. Maybe there is some improvements in speed that I could work on in those units eventually. I will have to do rigorous testing in freepascal !

Lars