doukuiqian9911 2017-10-30 15:00
浏览 80
已采纳

是否可以获得终端宽度或打印100%宽度的填充线?

PHP. fputs(STDOUT, 'some short text')! Is there any way to get char length for terminal that launch php script?

How to print text with fputs(STDOUT, "Some Error "); with full line background of red color?

  • 写回答

2条回答 默认 最新

  • dtf76989 2017-10-30 16:09
    关注

    This will output a line with "=" characters filling the whole line of the terminal. Will work on linux-based system.

    // '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");
    

    Which can be condensed to one line

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

    Here's a script to ANSI color code in XTerm/ANSI-compatible terminals with a 256-color palette support if you want to know more about displaying colours on terminal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大