← 都来码 / Dolaima← 例程索引objc/tty/demo_tty.m原文

objc/tty/demo_tty.m · 6952 字节 · 下载原文

// demo_tty.m —— **彩色控制台**示范(Objective-C)

// demo_tty.m — a **colored console** demo (Objective-C)

//

// 四层示范的第二层:清屏 + 设前景/背景色 + 定位光标 + 打印彩色文字,正常结束。

// Layer 2 of the four-layer demo: clear screen + set fg/bg colors + position the cursor + print colored text, terminating normally.

//

// ◆ 为什么直接发 ANSI,而不是用 `#include <conio.h>`

// ◆ Why it emits ANSI directly instead of using `#include <conio.h>`

//

// `Lib/c/conio.h` 能被 ObjC 前端**解析**(它没有 hex 字面量,不像 `graphics.h`),

// `Lib/c/conio.h` **can be parsed** by the ObjC frontend (it has no hex literals, unlike `graphics.h`),

// 头文件里的 `#param lib("conio")` 也会被预处理器收下 —— 但**符号解不开**:

// and the header's `#param lib("conio")` is accepted by the preprocessor — but **the symbols cannot be resolved**:

//   · `clrscr` 这类名字不在 `SharedPrefixMap` 里 ⇒ `AutoDetectSharedLibs` 想不到 conio;

//   · Names like `clrscr` are not in `SharedPrefixMap` ⇒ `AutoDetectSharedLibs` never thinks of conio;

//   · 显式 `#param lib("conio")` + 直接调 `clrscr()`,实测报「未定义的函数 'clrscr'」,

//   · With an explicit `#param lib("conio")` plus a direct `clrscr()` call, the measured result is "undefined function 'clrscr'",

//     而链接日志里**根本没有 conio.vml**。

//     and the link log shows **no conio.vml at all**.

// ⇒ 直接产出真终端会产出的字节,与 `Examples/c/conio_screen.c` 在命令行页上走**同一条

// ⇒ It emits the same bytes a real terminal would, going **through the same

//    渲染链**(`AnsiMarkup`)。

//    render chain** as `Examples/c/conio_screen.c` on the command-line page (`AnsiMarkup`).

//

// ◆ 写法(ObjC 前端的优势)

// ◆ Style (advantages of the ObjC frontend)

//

//   · **`\x1b` 转义是解析的**(与 Rust/Go 相反)⇒ 整条转义序列可以直接写进字符串;

//   · **The `\x1b` escape is parsed** (unlike Rust/Go) ⇒ a whole escape sequence can be written straight into the string;

//   · `printf` 不补换行(`puts` 补)⇒ 一行一条 CSI,换行自己写在末尾。

//   · `printf` adds no newline (`puts` does) ⇒ one CSI per line, with the newline written at the end yourself.

//   · `ui_scr_get_language()` 来自 `Lib/c/vml_ui.h`,本文件**显式引用**它(`#import`,`#include` 等价)。

//   · `ui_scr_get_language()` comes from `Lib/c/vml_ui.h`, which this file **references explicitly** (`#import`; `#include` is equivalent).

//

// 跑法:命令行页输入  vml run examples/objc/demo_tty.m

// How to run: on the command-line page type  vml run examples/objc/demo_tty.m


#include <vml_ui.h>
#import <vml_ui.h>

int main() {
    int lang;
    lang = ui_scr_get_language();

    printf("\x1b[2J\n");
    printf("\x1b[H\n");
    printf("\x1b[0;1;44;97m\n");
    if (lang == 0) printf("\x1b[3;1H  === Dolaima 彩色控制台演示 (Objective-C)  ===\n");
    else printf("\x1b[3;1H  === Dolaima color console demo (Objective-C)  ===\n");
    printf("\x1b[0;37m\n");
    if (lang == 0) printf("\x1b[5;1H标准 8 色前景(30-37):\n");
    else printf("\x1b[5;1HStandard 8 foreground colors (30-37):\n");
    printf("\x1b[0;30m\n");
    if (lang == 0) printf("\x1b[6;1H  30 黑\n");
    else printf("\x1b[6;1H  30 black\n");
    printf("\x1b[0;31m\n");
    if (lang == 0) printf("\x1b[7;1H  31 红\n");
    else printf("\x1b[7;1H  31 red\n");
    printf("\x1b[0;32m\n");
    if (lang == 0) printf("\x1b[8;1H  32 绿\n");
    else printf("\x1b[8;1H  32 green\n");
    printf("\x1b[0;33m\n");
    if (lang == 0) printf("\x1b[9;1H  33 黄\n");
    else printf("\x1b[9;1H  33 yellow\n");
    printf("\x1b[0;34m\n");
    if (lang == 0) printf("\x1b[10;1H  34 蓝\n");
    else printf("\x1b[10;1H  34 blue\n");
    printf("\x1b[0;35m\n");
    if (lang == 0) printf("\x1b[11;1H  35 品红\n");
    else printf("\x1b[11;1H  35 magenta\n");
    printf("\x1b[0;36m\n");
    if (lang == 0) printf("\x1b[12;1H  36 青\n");
    else printf("\x1b[12;1H  36 cyan\n");
    printf("\x1b[0;37m\n");
    if (lang == 0) printf("\x1b[13;1H  37 白\n");
    else printf("\x1b[13;1H  37 white\n");
    printf("\x1b[0;37m\n");
    if (lang == 0) printf("\x1b[15;1H亮色前景(90-97)与背景色:\n");
    else printf("\x1b[15;1HBright foreground (90-97) and background colors:\n");
    printf("\x1b[0;91m\n");
    if (lang == 0) printf("\x1b[16;1H  91 亮红\n");
    else printf("\x1b[16;1H  91 bright red\n");
    printf("\x1b[0;92m\n");
    if (lang == 0) printf("\x1b[17;1H  92 亮绿\n");
    else printf("\x1b[17;1H  92 bright green\n");
    printf("\x1b[0;41m\n");
    if (lang == 0) printf("\x1b[18;1H  41 红底\n");
    else printf("\x1b[18;1H  41 red bg\n");
    printf("\x1b[0;104m\n");
    if (lang == 0) printf("\x1b[19;1H 104 亮蓝底\n");
    else printf("\x1b[19;1H 104 bright blue bg\n");
    printf("\x1b[0;103m\n");
    if (lang == 0) printf("\x1b[20;1H 103 亮黄底\n");
    else printf("\x1b[20;1H 103 bright yellow bg\n");
    printf("\x1b[0;37m\n");
    if (lang == 0) printf("\x1b[22;1H256 色(38;5;N)与真彩(38;2;r;g;b):\n");
    else printf("\x1b[22;1H256-color (38;5;N) and truecolor (38;2;r;g;b):\n");
    printf("\x1b[0;38;5;208m\n");
    if (lang == 0) printf("\x1b[23;1H  256-208 橙\n");
    else printf("\x1b[23;1H  256-208 orange\n");
    printf("\x1b[0;38;5;46m\n");
    if (lang == 0) printf("\x1b[24;1H  256-46 亮绿\n");
    else printf("\x1b[24;1H  256-46 bright green\n");
    printf("\x1b[0;38;2;255;128;0m\n");
    if (lang == 0) printf("\x1b[25;1H  真彩 橙\n");
    else printf("\x1b[25;1H  truecolor orange\n");
    printf("\x1b[0;38;2;0;200;255m\n");
    if (lang == 0) printf("\x1b[26;1H  真彩 青\n");
    else printf("\x1b[26;1H  truecolor cyan\n");
    printf("\x1b[0;36m\n");
    if (lang == 0) printf("\x1b[28;1H光标定位(把光标移到第 30 行第 5 列)+ 画框:\n");
    else printf("\x1b[28;1HCursor positioning (move the cursor to row 30, column 5) + draw a box:\n");
    printf("\x1b[30;5H+----------------+\n");
    if (lang == 0) printf("\x1b[31;5H|  定位画框      |\n");
    else printf("\x1b[31;5H|  located box   |\n");
    printf("\x1b[32;5H+----------------+\n");
    printf("\x1b[0;2m\n");
    if (lang == 0) printf("\x1b[34;1H(以上全部是 ANSI SGR 序列,由终端 / 命令行页的 AnsiMarkup 渲染)\n");
    else printf("\x1b[34;1H(all of the above are ANSI SGR sequences, rendered by the terminal / AnsiMarkup on the command-line page)\n");
    printf("\x1b[0;32m\n");
    if (lang == 0) printf("\x1b[36;1H结束 —— 正常退出\n");
    else printf("\x1b[36;1Hdone -- exiting normally\n");
    return 0;
}