3

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?

RedMage
  • 1,126
  • 1
  • 9
  • 21

2 Answers2

7

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.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
4

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).

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 2
    There are two such libraries, ycurses and dcruses. However, the easiest solution is http://stackoverflow.com/questions/1022957/getting-terminal-width-in-c . – DejanLekic Aug 05 '13 at 20:53