1

Is there any possibilty to get the terminal line width in Fortran (90) beside calling tput cols, which, if possible, is cross platform?

I am only using gfortran, so it is sufficient if it works with this compiler.

Daniel Hauck
  • 117
  • 7
  • Hi, welcome at StackOverflow. You shouldn't put tags into the question if it is not a natural part of it http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles – Vladimir F Героям слава Jul 11 '16 at 12:16
  • 1
    You are much better using Fortran 2008 features, because they include interaction with the operating system (like `execute_command_line()`). Fortran 90 is too old. – Vladimir F Героям слава Jul 11 '16 at 12:18
  • Didn't know that, thank you for editing. Does Fortran 2008 include such a thing as getting terminal width or do I have to call different command depending on the operation system? Edit: accidently pressed Enter to early – Daniel Hauck Jul 11 '16 at 12:21
  • 1
    `call get_environment_variable( "COLUMNS", stmp ); read( stmp, * ) cols` seemed to work, but I experienced it doesn't work sometimes... (so it might be not reliable) http://stackoverflow.com/a/1022984/3501546 – roygvib Jul 11 '16 at 14:14
  • For me the COLUMNS variable is only set in Linux (tested with zsh) but e.g. not in Windows. But this nevertheless seems like a reasonable alternative for calling `tput cols` as it is easier to handle. – Daniel Hauck Jul 11 '16 at 14:59
  • `COLUMNS` does not exist in Fortran code runtime. – Li Dong Apr 03 '17 at 08:10

1 Answers1

1

Terminal/console functionality is always a system depends feature. It means, that getting a terminal width will be different for Unix and Windows systems. One of the library that can help you is ncurses, writen in C language. There are some Fortran interfaces for this library, like this uses Fortran 2003.

If this library will fit your requirements, please look at integer variables LINES and COLS defined in , they will be filled with the size of the screen.

In Fortran interface, mentioned before, there are getmaxyx subroutine:

subroutine getmaxyx(win,y,x) bind(C, name='macro_getmaxyx')
   use iso_c_binding
   use ncurses_types
   type (C_PTR), value :: win
   integer(C_INT) :: y,x
end subroutine

it stores the size of the specified window.

syscreat
  • 553
  • 1
  • 7
  • 16
  • Thank you, that was the kind of answer I was looking for. The library unfortunatly does not exist for Windows, but this sounds at least on Linux much more reliable than calling `tput cols` or getting environment variables. – Daniel Hauck Jul 12 '16 at 12:43