← 都来码 / Dolaima← 例程索引perl/tty/demo_tty.pl 原文

perl/tty/demo_tty.pl · 1417 字节 · 下载原文

# demo_tty.pl —— Perl 第二层:**彩色命令行**(tty)
# demo_tty.pl -- Perl layer 2: **colour command line** (tty)
#
# 这一层做「在字符界面上排版」:清屏、彩色文字、按行输出。
# This layer lays out a character screen: clear it, colour the text, print lines.
#
# 跑法(桌面 vmlcli):
# How to run (desktop vmlcli):
#   dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/perl/tty/demo_tty.pl
#
# 实现说明:`Lib/perl/` 下没有 tty 声明文件,`Lib/perl/builtin.vml` 也没有链 tty.vml,
# Implementation note: there is no tty declaration file under Lib/perl/ and Lib/perl/builtin.vml does not link tty.vml,
# 所以这里用 ANSI 转义串表达同一件事(清屏 ESC[2J、颜色 ESC[<n>m、复位 ESC[0m)。
# so this file expresses the same thing with ANSI escapes (clear ESC[2J, colour ESC[<n>m, reset ESC[0m).
# Perl 的 `chr(27)` 正好给出 ESC。
# Perl's `chr(27)` gives exactly ESC.

my $esc = chr(27);
print $esc, "[2J", $esc, "[H", $esc, "[1;36m", "=== Perl TTY demo ===", $esc, "[0m\n";
print $esc, "[33m", "  yellow: tty layer / color console", $esc, "[0m\n";
print $esc, "[32m", "  green : clear screen + SGR colors", $esc, "[0m\n";
print $esc, "[35m", "  magenta: loop follows", $esc, "[0m\n";

for my $i (1..3) {
    print $esc, "[34m", "  loop line $i", $esc, "[0m\n";
}

print $esc, "[0m\n";
print "Perl TTY demo: done\n";