Terminal control/Dimensions: Difference between revisions

From Rosetta Code
Content added Content deleted
({{omit from|PARI/GP}})
Line 115: Line 115:
HEIGHT=`tput lines`
HEIGHT=`tput lines`
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines"</lang>
echo "The terminal is $WIDTH characters wide and has $HEIGHT lines"</lang>

{{omit from|PARI/GP}}


[[Category:Terminal Control]]
[[Category:Terminal Control]]

Revision as of 02:01, 31 October 2010

Task
Terminal control/Dimensions
You are encouraged to solve this task according to the task description, using any language you may know.

Determine the height and width of the terminal, and store this information into variables for subsequent use.


Forth

Works with: GNU Forth

<lang forth>variable term-width variable term-height

form ( width height ) term-height ! term-width !</lang>

PicoLisp

<lang PicoLisp>(setq

  Width (in '(tput cols) (read))
  Height (in '(tput lines) (read)) )</lang>

PureBasic

PureBasic does not have native functions for reading the size of this window, but supports API-functions that allows this.

This code is for Windows only. <lang PureBasic>Macro ConsoleHandle()

 GetStdHandle_( #STD_OUTPUT_HANDLE )

EndMacro

Procedure ConsoleWidth()

 Protected CBI.CONSOLE_SCREEN_BUFFER_INFO
 Protected hConsole = ConsoleHandle()
 GetConsoleScreenBufferInfo_( hConsole, @CBI )
 ProcedureReturn CBI\srWindow\right - CBI\srWindow\left + 1

EndProcedure

Procedure ConsoleHeight()

 Protected CBI.CONSOLE_SCREEN_BUFFER_INFO
 Protected hConsole = ConsoleHandle()
 GetConsoleScreenBufferInfo_( hConsole, @CBI )
 ProcedureReturn CBI\srWindow\bottom - CBI\srWindow\top + 1

EndProcedure

If OpenConsole()

 x$=Str(ConsoleWidth())
 y$=Str(ConsoleHeight())
 PrintN("This window is "+x$+"x"+y$+ " chars.")
 ;
 Print(#CRLF$+"Press ENTER to exit"):Input()

EndIf</lang>

Python

Works with: Python version 2.6
Library: ctypes

This uses the ctypes library in order to get the console dimensions on Windows. This code is a slight refactoring of an ActiveState Recipe. For Linux, the tput utility is used.

<lang python>import os

def get_windows_terminal():

   from ctypes import windll, create_string_buffer
   h = windll.kernel32.GetStdHandle(-12)
   csbi = create_string_buffer(22)
   res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
   #return default size if actual size can't be determined
   if not res: return 80, 25 
   import struct
   (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy)\
   = struct.unpack("hhhhHhhhhhh", csbi.raw)
   width = right - left + 1
   height = bottom - top + 1
   return width, height

def get_linux_terminal():

   width = os.popen('tput cols', 'r').readline()
   height = os.popen('tput lines', 'r').readline()
   return int(width), int(height)

print get_linux_terminal() if os.name == 'posix' else get_windows_terminal() </lang>

Retro

This information is provided by Retro in the fh (height) and fw (width) variables. You can manually obtain it using the io ports.

<lang Retro>-3 5 out wait 5 in !fw -4 5 out wait 5 in !fh</lang>

REXX

Works with: brexx
Works with: regina
Works with: rexximc

Rexx does not provide basic terminal control as part of the language. However, it is possible to determine the size of the terminal window by using external system commands:

<lang rexx> width = 'tput'( 'cols' ) height = 'tput'( 'lines' )

say 'The terminal is' width 'characters wide' say 'and has' height 'lines' </lang>

Tcl

Translation of: UNIX Shell

<lang tcl>set width [exec tput cols] set height [exec tput lines] puts "The terminal is $width characters wide and has $height lines"</lang>

UNIX Shell

Works with: Bourne Shell
Works with: bash

<lang sh>#!/bin/sh WIDTH=`tput cols` HEIGHT=`tput lines` echo "The terminal is $WIDTH characters wide and has $HEIGHT lines"</lang>