RedMage
RedMage

Reputation: 1116

Determine number of lines in terminal

I would like to write a short d program that fills the screen with pound symbols. Here is what I have

import std.stdio;
import std.process;
import std.conv;

void main(string[] args){
  auto lines =  environment.get("LINES");
  int line_count = to!int(lines); 
  for(int a = 1; a <= line_count; a++){
    writeln("######################################################################");
  }
}

I expected this to work because when I execute "echo $LINES" from the terminal it prints "47". However, LINES appears empty when I run the program via rdmd in the same session. This is on Ubuntu Raring. Any ideas?

Upvotes: 3

Views: 215

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263617

If you can grab the output of the command stty size, that's probably more reliable than examining the $LINES and $COLUMNS environment variables.

Or you can invoke the TIOCGWINSZ ioctl as described in this answer.

Upvotes: 7

that other guy
that other guy

Reputation: 123650

If you just want a simple fix, you can put export LINES COLUMNS in your ~/.bashrc to make these variables available in your program.

For a proper solution, you could try to invoke the ioctl TIOCGWINSZ, or find a D library that supports querying the terminal (such as ncurses wrappers).

Upvotes: 4

Related Questions