簡體   English   中英

是否有可能獲得端子的寬度或以100%的寬度填充來打印行?

[英]Is it possible to get width of terminal or to print line with fill of 100% width?

PHP。 fputs(STDOUT, 'some short text') 有什么方法可以獲取啟動php腳本的終端的char長度嗎?

如何用fputs(STDOUT, "Some Error\\n");打印文本fputs(STDOUT, "Some Error\\n"); 全線背景的紅色?

這將輸出一行,其中“ =”字符填充終端的整個行。 將在基於linux的系統上工作。

// 'tput cols' is a shell command which returns
//   the number of chars to fill up the whole
//   line of the current terminal.
$colCount = shell_exec('tput cols');

// Create the string filling the whole line
$string = str_repeat("=", intval($colCount));

// This is the way to print out text with coloured background
// 48, 5, 160 representing 256-colour of red, green and blue.
fputs(STDOUT, "\e[48;5;160m".$string."\e[0m");

可以濃縮成一條線

fputs(STDOUT, "\e[48;5;160m".str_repeat("=", intval(shell_exec('tput cols')))."\e[0m");

如果您想了解有關在終端上顯示顏色的更多信息, 這是一個在XTerm / ANSI兼容終端中支持256色調色板的ANSI顏色代碼的腳本

您可以為此使用Symfony \\ Console,因為它具有完全滿足您需要的Terminal類:

用作曲家安裝

composer require symfony/console

創建腳本index.php

<?php

require __DIR__ . '/vendor/autoload.php';

$terminal = new Symfony\Component\Console\Terminal;

$terminalWidth = $terminal->getWidth();
var_dump($terminalWidth);

測試一下

$ php index.php
int(80)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM