Tuesday 9 July 2019

How do I find the width & height of a terminal window?



As a simple example, I want to write a CLI script which can print = across the entire width of the terminal window.



#!/usr/bin/env php
echo str_repeat('=', ???);


or



#!/usr/bin/env python
print '=' * ???


or



#!/usr/bin/env bash
x=0
while [ $x -lt ??? ]; do echo -n '='; let x=$x+1 done; echo

Answer




  • tput cols tells you the number of columns.

  • tput lines tells you the number of rows.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...