terminal-info 1.3.1

An extensible terminal information CLI and developer toolbox
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
/* =============================================
   Terminal Info — Landing Page Script
   ============================================= */

(function () {
  "use strict";

  var RELEASE_API = "https://api.github.com/repos/T-1234567890/terminal-info/releases/latest";
  var VERSION_CACHE_KEY = "tinfo-version";
  var VERSION_CACHE_TTL = 10 * 60 * 1000;
  var LANGUAGE_KEY = "tinfo-language";
  var currentLanguage = "en";

  function normalizeVersion(tag) {
    if (!tag || typeof tag !== "string") return null;
    var value = tag.trim();
    return value || null;
  }

  function readVersionCache() {
    try {
      var raw = window.localStorage.getItem(VERSION_CACHE_KEY);
      if (!raw) return null;
      var parsed = JSON.parse(raw);
      if (!parsed || typeof parsed.version !== "string" || typeof parsed.cachedAt !== "number") {
        return null;
      }
      if (Date.now() - parsed.cachedAt > VERSION_CACHE_TTL) {
        return null;
      }
      return parsed;
    } catch (_err) {
      return null;
    }
  }

  function writeVersionCache(version, releaseUrl) {
    try {
      window.localStorage.setItem(
        VERSION_CACHE_KEY,
        JSON.stringify({
          version: version,
          releaseUrl: releaseUrl || null,
          cachedAt: Date.now()
        })
      );
    } catch (_err) {
      // Ignore cache failures and continue.
    }
  }

  function updateVersionDisplays(version, releaseUrl) {
    if (!version) return;
    document.querySelectorAll("[data-stable-version]").forEach(function (node) {
      node.textContent = version;
      if (releaseUrl && node.tagName === "A") {
        node.setAttribute("href", releaseUrl);
      }
    });
  }

  function getLocalizedText(node, lang) {
    if (!node || !node.dataset) return null;
    if (lang === "zh" && node.dataset.zh) return node.dataset.zh;
    if (node.dataset.en) return node.dataset.en;
    return null;
  }

  function updateLanguageButtons(lang) {
    document.querySelectorAll("[data-lang-btn]").forEach(function (btn) {
      btn.classList.toggle("active", btn.getAttribute("data-lang-btn") === lang);
      btn.setAttribute("aria-pressed", btn.getAttribute("data-lang-btn") === lang ? "true" : "false");
    });
  }

  function applyLanguage(lang) {
    currentLanguage = lang === "zh" ? "zh" : "en";
    document.documentElement.lang = currentLanguage === "zh" ? "zh-CN" : "en";

    document.querySelectorAll("[data-en][data-zh]").forEach(function (node) {
      var next = getLocalizedText(node, currentLanguage);
      if (typeof next === "string") {
        node.textContent = next;
      }
    });

    document.querySelectorAll("[data-lang-content]").forEach(function (node) {
      node.hidden = node.getAttribute("data-lang-content") !== currentLanguage;
    });

    var heroCopyBtn = document.getElementById("hero-copy-btn");
    if (heroCopyBtn) {
      heroCopyBtn.title = currentLanguage === "zh" ? "复制安装命令" : "Copy install command";
    }

    updateLanguageButtons(currentLanguage);

    if (document.getElementById("demo-info")) {
      var activeTab = document.querySelector(".demo-tab.active");
      if (activeTab) {
        renderInfo(activeTab.getAttribute("data-demo"));
      }
    }

    try {
      window.localStorage.setItem(LANGUAGE_KEY, currentLanguage);
    } catch (_err) {
      // Ignore storage failures.
    }
  }

  function initLanguageToggle() {
    var saved = null;
    try {
      saved = window.localStorage.getItem(LANGUAGE_KEY);
    } catch (_err) {
      saved = null;
    }

    applyLanguage(saved === "zh" ? "zh" : "en");

    document.querySelectorAll("[data-lang-btn]").forEach(function (btn) {
      btn.addEventListener("click", function () {
        applyLanguage(btn.getAttribute("data-lang-btn"));
      });
    });
  }

  function initSectionReveal() {
    var nodes = document.querySelectorAll(".section, .stats-strip, .site-footer, [data-reveal]");
    if (!nodes.length) return;

    if (!("IntersectionObserver" in window)) {
      nodes.forEach(function (node) {
        node.classList.add("is-visible");
      });
      return;
    }

    var observer = new IntersectionObserver(function (entries) {
      entries.forEach(function (entry) {
        if (entry.isIntersecting) {
          entry.target.classList.add("is-visible");
          observer.unobserve(entry.target);
        }
      });
    }, {
      threshold: 0.12,
      rootMargin: "0px 0px -24px 0px"
    });

    nodes.forEach(function (node) {
      observer.observe(node);
    });
  }

  function animateValue(el, nextText) {
    if (!el || el.textContent === nextText) return;
    el.style.opacity = "0.35";
    el.style.transform = "translateY(5px)";
    window.setTimeout(function () {
      el.textContent = nextText;
      el.style.opacity = "1";
      el.style.transform = "translateY(0)";
    }, 220);
  }

  function pad(value) {
    return String(value).padStart(2, "0");
  }

  function padEnd(value, width) {
    var text = String(value);
    if (text.length >= width) return text.slice(0, width);
    return text + new Array(width - text.length + 1).join(" ");
  }

  function formatBoxRows(title, rows, options) {
    var opts = options || {};
    var width = opts.width || 34;
    var labelWidth = opts.labelWidth || 10;
    var innerWidth = width;

    function border(left, fill, right) {
      return left + new Array(innerWidth + 1).join(fill) + right;
    }

    function contentLine(text) {
      return "" + padEnd(text, innerWidth) + "";
    }

    var centeredTitle = title;
    var leftPad = Math.max(0, Math.floor((innerWidth - title.length) / 2));
    centeredTitle = new Array(leftPad + 1).join(" ") + title;
    centeredTitle = padEnd(centeredTitle, innerWidth);

    var lines = [
      border("", "", ""),
      contentLine(centeredTitle),
      border("", "", "")
    ];

    rows.forEach(function (row) {
      var label = padEnd(row.label + ":", labelWidth);
      var text = " " + label + " " + row.value;
      lines.push(contentLine(text));
    });

    lines.push(border("", "", ""));
    return lines;
  }

  function renderBoxLine(line) {
    return '<div class="term-line"><span class="term-box">' + escHtml(line) + "</span></div>";
  }

  function heroRowsFromState(state) {
    return [
      { label: "Location", value: "Tokyo" },
      { label: "Weather", value: "Clear sky, 20.3°C" },
      { label: "Time", value: state.time },
      { label: "Network", value: "143.xxx.x.xx" },
      { label: "CPU", value: state.cpu },
      { label: "Memory", value: state.memory + " used" },
      { label: "Timers", value: state.timer },
      { label: "Reminders", value: state.reminder }
    ];
  }

  function renderHeroTerminal(state) {
    var body = document.getElementById("hero-term-body");
    if (!body) return;
    var lines = formatBoxRows("Terminal Info", heroRowsFromState(state), {
      width: 44,
      labelWidth: 10
    });
    var html = '<div class="term-line"><span class="term-ps">$</span><span class="term-cmd">tinfo</span></div>';
    lines.forEach(function (line) {
      html += renderBoxLine(line);
    });
    html += '<div class="term-line" style="height:0.6rem"></div>';
    html += '<div class="term-line"><span class="term-out">Press q or Ctrl+C to exit.</span></div>';
    body.innerHTML = html;
  }

  function initHeroTerminalUpdates() {
    var body = document.getElementById("hero-term-body");
    if (!body) return;

    var cpuSeries = ["28.4%", "31.2%", "35.6%", "42.1%", "51.8%"];
    var memorySeries = [
      "16.2 GiB / 24.0 GiB",
      "16.3 GiB / 24.0 GiB",
      "16.1 GiB / 24.0 GiB",
      "16.2 GiB / 24.0 GiB"
    ];
    var cpuIndex = 0;
    var memoryIndex = 0;
    var reminderTarget = Date.now() + 12 * 60 * 1000;
    var timerRemaining = 24 * 60 + 52;
    var state = {
      time: "2026-03-28 20:45:44",
      cpu: cpuSeries[cpuIndex],
      memory: memorySeries[memoryIndex],
      timer: formatCountdown(timerRemaining),
      reminder: formatReminder(reminderTarget)
    };

    renderHeroTerminal(state);

    window.setInterval(function () {
      var now = new Date();
      state.time = [
        now.getFullYear(),
        "-",
        pad(now.getMonth() + 1),
        "-",
        pad(now.getDate()),
        " ",
        pad(now.getHours()),
        ":",
        pad(now.getMinutes()),
        ":",
        pad(now.getSeconds())
      ].join("");
      if (timerRemaining > 0) {
        timerRemaining -= 1;
      }
      state.timer = formatCountdown(timerRemaining);
      state.reminder = formatReminder(reminderTarget);
      renderHeroTerminal(state);
    }, 1000);

    window.setInterval(function () {
      cpuIndex = (cpuIndex + 1) % cpuSeries.length;
      state.cpu = cpuSeries[cpuIndex];
      renderHeroTerminal(state);
    }, 4200);

    window.setInterval(function () {
      memoryIndex = (memoryIndex + 1) % memorySeries.length;
      state.memory = memorySeries[memoryIndex];
      renderHeroTerminal(state);
    }, 6200);
  }

  async function loadStableVersion() {
    var cached = readVersionCache();
    if (cached) {
      updateVersionDisplays(cached.version, cached.releaseUrl);
    }

    if (!window.fetch) {
      return cached ? cached.version : null;
    }

    try {
      var response = await window.fetch(RELEASE_API, {
        headers: {
          Accept: "application/vnd.github+json"
        }
      });
      if (!response.ok) {
        throw new Error("GitHub release lookup failed");
      }

      var payload = await response.json();
      var version = normalizeVersion(payload && payload.tag_name);
      var releaseUrl = payload && payload.html_url ? String(payload.html_url) : null;

      if (!version) {
        throw new Error("Latest release tag missing");
      }

      updateVersionDisplays(version, releaseUrl);
      writeVersionCache(version, releaseUrl);
      return version;
    } catch (_err) {
      if (cached) {
        updateVersionDisplays(cached.version, cached.releaseUrl);
        return cached.version;
      }
      return null;
    }
  }

  /* ------------------------------------------
     Demo scenarios
  ------------------------------------------ */
  var demos = {
    dashboard: {
      title: "Dashboard",
      command: "tinfo",
      info: {
        heading: "Instant system overview",
        headingZh: "即时系统总览",
        bullets: [
          "Shows location, weather, time, network, CPU, and memory in one view",
          "Timers and reminders appear directly in the main summary",
          "Useful as a quick status check when you open the tool",
        ],
        bulletsZh: [
          "在一个视图中显示位置、天气、时间、网络、CPU 和内存",
          "计时器和提醒会直接出现在主摘要里",
          "适合在打开工具时快速查看当前状态"
        ]
      },
      lines: [
        {
          type: "box",
          title: "Terminal Info",
          width: 44,
          labelWidth: 10,
          rows: [
            { label: "Location", value: "Tokyo" },
            { label: "Weather", value: "Clear sky, 20.3°C" },
            { label: "Time", value: "__DYNAMIC_TIME__" },
            { label: "Network", value: "143.xxx.x.xx" },
            { label: "CPU", value: "19.3%" },
            { label: "Memory", value: "16.2 GiB / 24.0 GiB used" },
            { label: "Timers", value: "Timer: 00:24:52 remaining" },
            { label: "Reminders", value: "⏳ break in 12 min" }
          ]
        },
        { type: "blank" },
        { type: "out", cls: "term-output", text: "Press q or Ctrl+C to exit." }
      ],
    },

    chat: {
      title: "AI Chat",
      liveSession: true,
      info: {
        heading: "Simple terminal AI chat",
        headingZh: "简单的终端 AI 聊天",
        bullets: [
          "Interactive AI chat in the terminal",
          "Shows basic markdown rendering in the chat output",
          "The prompt always shows provider and model",
          "OpenRouter is recommended for broad model access"
        ],
        bulletsZh: [
          "直接在终端中进行交互式 AI 聊天",
          "展示聊天输出中的基础 Markdown 渲染",
          "提示符会始终显示提供商和模型",
          "推荐使用 OpenRouter 来访问更多模型"
        ]
      },
      lines: [
        { type: "dynamic-chat" }
      ]
    },

    weather: {
      title: "Weather",
      command: "tinfo weather now",
      info: {
        heading: "Real-time weather",
        headingZh: "实时天气",
        bullets: [
          "Shows the current conditions in a compact boxed view",
          "Includes temperature, wind, and humidity",
          "Useful when you want a quick weather check without leaving the terminal",
        ],
        bulletsZh: [
          "以紧凑的盒式视图显示当前天气",
          "包含温度、风速和湿度",
          "适合在不离开终端时快速查看天气"
        ]
      },
      lines: [
        {
          type: "box",
          title: "Tokyo Weather",
          width: 38,
          labelWidth: 12,
          rows: [
            { label: "Location", value: "Tokyo" },
            { label: "Weather", value: "Clear sky" },
            { label: "Temperature", value: "20.3°C" },
            { label: "Wind", value: "3.1 m/s" },
            { label: "Humidity", value: "61%" }
          ]
        }
      ],
    },

    diagnostic: {
      title: "Diagnostic",
      command: "tinfo diagnostic network",
      info: {
        heading: "Network diagnostics",
        headingZh: "网络诊断",
        bullets: [
          "Checks DNS, ping, HTTP reachability, and Cloudflare connectivity",
          "Also reports public IP, local IP, and ISP details",
          "Useful when you want a fast connectivity check from the CLI",
        ],
        bulletsZh: [
          "检查 DNS、ping、HTTP 可达性和 Cloudflare 连通性",
          "同时报告公网 IP、本地 IP 和运营商信息",
          "适合在 CLI 中快速检查连接状态"
        ]
      },
      lines: [
        { type: "check", key: "  DNS resolution     ", val: "ok", good: true },
        { type: "check", key: "  External ping      ", val: "26 ms", good: true },
        { type: "check", key: "  HTTP reachability  ", val: "ok", good: true },
        { type: "check", key: "  Cloudflare ping    ", val: "14 ms", good: true },
        { type: "kv", key: "  Public IP          ", val: "143.xxx.x.xx" },
        { type: "kv", key: "  Local IP           ", val: "192.168.x.xx" },
        { type: "kv", key: "  ISP                ", val: "Example ISP" },
        { type: "blank" },
        { type: "out", cls: "good", text: "  All network checks passed." },
      ],
    },

    productivity: {
      title: "Productivity",
      command: "tinfo timer start 25m",
      info: {
        heading: "Built-in productivity",
        headingZh: "内置效率工具",
        bullets: [
          "Starts a countdown timer from the command line",
          "Shows the remaining time in a compact live view",
          "Useful for focused work sessions directly in the terminal",
        ],
        bulletsZh: [
          "可以直接从命令行启动倒计时",
          "以紧凑的实时视图显示剩余时间",
          "适合在终端中进行专注工作"
        ]
      },
      lines: [
        { type: "out", cls: "term-output", text: "Started countdown timer for 25m 0s." },
        { type: "blank" },
        {
          type: "box",
          title: "Terminal Info Timer",
          width: 27,
          labelWidth: 7,
          rows: [
            { label: "Timer", value: "__DYNAMIC_TIMER__" }
          ]
        },
        { type: "blank" },
        { type: "out", cls: "term-output", text: "Press q or Ctrl+C to exit." }
      ],
    },

    plugin: {
      title: "Plugin",
      command: "tinfo plugin install news",
      info: {
        heading: "Plugin management",
        headingZh: "插件管理",
        bullets: [
          "The install command resolves a plugin name and places the binary in the managed plugin directory",
          "Installed plugins become available as first-class terminal commands",
          "Useful for extending the CLI without modifying the core tool",
        ],
        bulletsZh: [
          "安装命令会解析插件名称,并将二进制文件放入受管理的插件目录",
          "安装后的插件会作为一等终端命令提供使用",
          "适合在不修改核心工具的情况下扩展 CLI"
        ]
      },
      lines: [
        { type: "out", cls: "good", text: "Installed plugin 'news' at /Users/you/.terminal-info/plugins/docker/tinfo-news." },
      ],
    },
  };

  /* ------------------------------------------
     Render demo info panel
  ------------------------------------------ */
  function renderInfo(key) {
    var d = demos[key];
    if (!d) return;
    var info = d.info;
    var heading = currentLanguage === "zh" && info.headingZh ? info.headingZh : info.heading;
    var bullets = currentLanguage === "zh" && info.bulletsZh ? info.bulletsZh : info.bullets;
    var html = '<h3>' + heading + '</h3>';
    if (bullets && bullets.length) {
      html += '<ul>';
      for (var i = 0; i < bullets.length; i++) {
        html += '<li>' + bullets[i] + '</li>';
      }
      html += '</ul>';
    }
    var el = document.getElementById("demo-info");
    if (el) el.innerHTML = html;
  }

  /* ------------------------------------------
     Build terminal HTML lines
  ------------------------------------------ */
  function buildLines(key) {
    var d = demos[key];
    if (!d) return "";
    var html = "";
    if (!d.liveSession) {
      html += '<div class="term-line"><span class="term-ps">$</span><span class="term-cmd" id="demo-cmd-text"></span><span class="term-cursor" id="demo-cursor"></span></div>';
    }
    // Output wrapper (hidden initially)
    html += '<div id="demo-output" style="opacity:0;transition:opacity 0.3s">';
    for (var i = 0; i < d.lines.length; i++) {
      var l = d.lines[i];
      if (l.type === "blank") {
        html += '<div class="term-line" style="height:0.6rem"></div>';
      } else if (l.type === "dynamic-chat") {
        html += '<div data-dynamic-box="chat"></div>';
      } else if (l.type === "box") {
        if (
          l.rows &&
          l.rows.some(function (row) {
            return row.value === "__DYNAMIC_TIME__";
          })
        ) {
          html += '<div data-dynamic-box="dashboard"></div>';
          continue;
        }
        if (
          l.rows &&
          l.rows.length === 1 &&
          l.rows[0].label === "Timer" &&
          l.rows[0].value === "__DYNAMIC_TIMER__"
        ) {
          html += '<div data-dynamic-box="timer"></div>';
          continue;
        }
        var boxLines = formatBoxRows(l.title, l.rows, {
          width: l.width,
          labelWidth: l.labelWidth
        });
        for (var j = 0; j < boxLines.length; j++) {
          html += renderBoxLine(boxLines[j]);
        }
      } else if (l.type === "out") {
        html += '<div class="term-line"><span class="term-out ' + (l.cls || "") + '">' + escHtml(l.text) + '</span></div>';
      } else if (l.type === "chat-prompt") {
        html += '<div class="term-line"><span class="term-cmd">' + escHtml(l.text) + '</span></div>';
      } else if (l.type === "kv") {
        html += '<div class="term-line"><span class="term-key">' + escHtml(l.key) + '</span><span class="term-val">' + escHtml(l.val) + '</span></div>';
      } else if (l.type === "check") {
        var cls = l.good ? "good" : "bad";
        html += '<div class="term-line"><span class="term-key">' + escHtml(l.key) + '</span><span class="term-out ' + cls + '">\u2713 ' + escHtml(l.val) + '</span></div>';
      }
    }
    html += '</div>';
    // Prompt after output
    if (!d.liveSession) {
      html += '<div id="demo-end-prompt" style="display:none;margin-top:0.5rem"><div class="term-line"><span class="term-ps">$</span><span class="term-cursor"></span></div></div>';
    }
    return html;
  }

  function escHtml(str) {
    return String(str)
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;");
  }

  /* ------------------------------------------
     Typewriter animation
  ------------------------------------------ */
  var typeTimer = null;
  var showTimer = null;
  var demoValueTimer = null;
  var demoClockTimer = null;

  function clearTimers() {
    if (typeTimer) clearTimeout(typeTimer);
    if (showTimer) clearTimeout(showTimer);
    if (demoValueTimer) clearInterval(demoValueTimer);
    if (demoClockTimer) clearInterval(demoClockTimer);
    typeTimer = null;
    showTimer = null;
    demoValueTimer = null;
    demoClockTimer = null;
  }

  function formatDateTime(now) {
    return [
      now.getFullYear(),
      "-",
      pad(now.getMonth() + 1),
      "-",
      pad(now.getDate()),
      " ",
      pad(now.getHours()),
      ":",
      pad(now.getMinutes()),
      ":",
      pad(now.getSeconds())
    ].join("");
  }

  function formatCountdown(totalSeconds) {
    var hours = Math.floor(totalSeconds / 3600);
    var minutes = Math.floor((totalSeconds % 3600) / 60);
    var seconds = totalSeconds % 60;
    return pad(hours) + ":" + pad(minutes) + ":" + pad(seconds) + " remaining";
  }

  function formatReminder(targetTime) {
    var diffMs = Math.max(0, targetTime - Date.now());
    var remainingMinutes = Math.ceil(diffMs / 60000);
    if (remainingMinutes <= 0) {
      return "break due now";
    }
    if (remainingMinutes === 1) {
      return "break in 1 min";
    }
    return "break in " + remainingMinutes + " min";
  }

  function startProductivityDemo() {
    var output = document.getElementById("demo-output");
    if (!output) return;

    var remaining = 25 * 60;

    function render() {
      var lines = formatBoxRows("Terminal Info Timer", [
        { label: "Timer", value: formatCountdown(remaining) }
      ], {
        width: 27,
        labelWidth: 7
      });

      var html = "";
      for (var i = 0; i < lines.length; i++) {
        html += renderBoxLine(lines[i]);
      }

      output.querySelectorAll('[data-dynamic-box="timer"]').forEach(function (node) {
        node.remove();
      });

      var wrapper = document.createElement("div");
      wrapper.setAttribute("data-dynamic-box", "timer");
      wrapper.innerHTML = html;
      output.appendChild(wrapper);
    }

    render();
    demoValueTimer = window.setInterval(function () {
      if (remaining > 0) {
        remaining -= 1;
      } else {
        clearInterval(demoValueTimer);
        demoValueTimer = null;
        var endPrompt = document.getElementById("demo-end-prompt");
        if (endPrompt) {
          endPrompt.style.display = "block";
        }
      }
      render();
    }, 1000);
  }

  function startDashboardDemo() {
    var output = document.getElementById("demo-output");
    if (!output) return;

    var cpuSeries = ["19.3%", "22.1%", "24.8%", "21.7%"];
    var memorySeries = [
      "16.2 GiB / 24.0 GiB used",
      "16.3 GiB / 24.0 GiB used",
      "16.1 GiB / 24.0 GiB used",
      "16.2 GiB / 24.0 GiB used"
    ];
    var reminderTarget = Date.now() + 12 * 60 * 1000;
    var timerRemaining = 24 * 60 + 52;
    var cpuIndex = 0;

    function render() {
      var now = new Date();
      var lines = formatBoxRows("Terminal Info", [
        { label: "Location", value: "Tokyo" },
        { label: "Weather", value: "Clear sky, 20.3°C" },
        { label: "Time", value: formatDateTime(now) },
        { label: "Network", value: "143.xxx.x.xx" },
        { label: "CPU", value: cpuSeries[cpuIndex] },
        { label: "Memory", value: memorySeries[cpuIndex] },
        { label: "Timers", value: formatCountdown(timerRemaining) },
        { label: "Reminders", value: formatReminder(reminderTarget) }
      ], {
        width: 44,
        labelWidth: 10
      });
      var html = "";
      for (var i = 0; i < lines.length; i++) {
        html += renderBoxLine(lines[i]);
      }

      output.querySelectorAll('[data-dynamic-box="dashboard"]').forEach(function (node) {
        node.remove();
      });

      var wrapper = document.createElement("div");
      wrapper.setAttribute("data-dynamic-box", "dashboard");
      wrapper.innerHTML = html;
      output.insertBefore(wrapper, output.firstChild);
    }

    render();
    demoClockTimer = window.setInterval(function () {
      cpuIndex = (cpuIndex + 1) % cpuSeries.length;
      if (timerRemaining > 0) {
        timerRemaining -= 1;
      }
      render();
    }, 1000);
  }

  function renderChatLine(text, cls) {
    return '<div class="term-line"><span class="' + (cls || "term-out") + '">' + escHtml(text) + '</span></div>';
  }

  function renderChatPrompt(text) {
    return '<div class="term-line"><span class="term-cmd">' + escHtml(text) + '</span></div>';
  }

  function renderMarkdownHeading(text) {
    return '<div class="term-line"><span class="term-md-heading">' + escHtml(text) + '</span></div>';
  }

  function renderMarkdownBullet(text) {
    return '<div class="term-line"><span class="term-md-bullet">' + escHtml(text) + '</span></div>';
  }

  function renderMarkdownCodeLine(prefix, codeText, suffix) {
    var html = '<div class="term-line">';
    if (prefix) {
      html += '<span class="term-out">' + escHtml(prefix) + '</span>';
    }
    html += '<span class="term-md-code">' + escHtml(codeText) + '</span>';
    if (suffix) {
      html += '<span class="term-out">' + escHtml(suffix) + '</span>';
    }
    html += '</div>';
    return html;
  }

  function startChatDemo() {
    var output = document.getElementById("demo-output");
    if (!output) return;

    var modelOptions = [
      "openai/gpt-5.4-pro",
      "anthropic/claude-4.6-sonnet",
      "google/gemini-3.1-pro-preview",
      "Custom model..."
    ];
    var modelIndex = 0;
    var phase = 0;

    function render() {
      var html = "";
      html += renderChatLine("Using provider: OpenRouter", "term-out");
      html += renderChatLine("Type 'exit' or 'quit' to leave.", "term-out");
      html += renderChatLine("Tip: /provider switch provider · /model switch model", "term-out");
      html += '<div class="term-line" style="height:0.6rem"></div>';
      html += renderChatPrompt("[OpenRouter · openai/gpt-5.4-pro] > Give me a short release note in markdown.");
      html += '<div class="term-line" style="height:0.6rem"></div>';
      html += renderChatLine("AI:", "term-cmd");
      html += renderMarkdownHeading("Release note");
      html += renderMarkdownBullet("Added live agent approvals");
      html += renderMarkdownBullet("Simplified the chat prompt");
      html += renderMarkdownCodeLine("Use ", "cargo check --workspace", " before shipping");
      html += '<div class="term-line" style="height:0.6rem"></div>';
      html += renderChatPrompt("[OpenRouter · openai/gpt-5.4-pro] > /model");
      html += renderChatLine("Select a model with t/t.", "term-out");

      for (var i = 0; i < modelOptions.length; i++) {
        var prefix = i === modelIndex ? "" : "  ";
        html += renderChatLine(prefix + modelOptions[i], "term-out");
      }

      output.querySelectorAll('[data-dynamic-box="chat"]').forEach(function (node) {
        node.remove();
      });

      var wrapper = document.createElement("div");
      wrapper.setAttribute("data-dynamic-box", "chat");
      wrapper.innerHTML = html;
      output.appendChild(wrapper);
    }

    render();
    demoValueTimer = window.setInterval(function () {
      if (phase < 4) {
        modelIndex = (modelIndex + 1) % modelOptions.length;
        phase += 1;
      } else {
        modelIndex = 0;
      }
      render();
    }, 1100);
  }

  function typeCommand(text, el, cursor, callback) {
    var i = 0;
    el.textContent = "";
    function step() {
      if (i < text.length) {
        el.textContent += text[i];
        i++;
        typeTimer = setTimeout(step, 38 + Math.random() * 20);
      } else {
        if (cursor) cursor.style.display = "none";
        if (callback) showTimer = setTimeout(callback, 300);
      }
    }
    step();
  }

  function runDemo(key) {
    var d = demos[key];
    if (!d) return;
    clearTimers();

    var body = document.getElementById("demo-term-body");
    if (!body) return;

    body.innerHTML = buildLines(key);

    var cmdEl    = document.getElementById("demo-cmd-text");
    var cursor   = document.getElementById("demo-cursor");
    var output   = document.getElementById("demo-output");
    var endPrompt = document.getElementById("demo-end-prompt");

    if (d.liveSession) {
      if (output) {
        output.style.opacity = "1";
      }
      if (key === "chat") {
        startChatDemo();
      }
      return;
    }

    if (!cmdEl) return;

    typeCommand(d.command, cmdEl, cursor, function () {
      if (output) {
        output.style.opacity = "1";
      }
      if (endPrompt && key !== "dashboard" && key !== "productivity") {
        showTimer = setTimeout(function () {
          endPrompt.style.display = "block";
        }, 200);
      }
      if (key === "dashboard") {
        startDashboardDemo();
      }
      if (key === "productivity") {
        startProductivityDemo();
      }
    });
  }

  /* ------------------------------------------
     Demo tab switching
  ------------------------------------------ */
  function initDemoTabs() {
    var tabs = document.querySelectorAll(".demo-tab");
    if (!tabs.length) return;

    function activate(key) {
      tabs.forEach(function (t) {
        var active = t.getAttribute("data-demo") === key;
        t.classList.toggle("active", active);
        t.setAttribute("aria-selected", active ? "true" : "false");
      });
      renderInfo(key);
      runDemo(key);
    }

    tabs.forEach(function (tab) {
      tab.addEventListener("click", function () {
        activate(tab.getAttribute("data-demo"));
      });
    });

    // Init first demo
    activate("dashboard");
  }

  /* ------------------------------------------
     Install tab switching
  ------------------------------------------ */
  function initInstallTabs() {
    var tabs     = document.querySelectorAll(".install-tab");
    var contents = document.querySelectorAll(".install-content");

    tabs.forEach(function (tab) {
      tab.addEventListener("click", function () {
        var key = tab.getAttribute("data-install");

        tabs.forEach(function (t) { t.classList.remove("active"); });
        contents.forEach(function (c) { c.classList.remove("active"); });

        tab.classList.add("active");
        var target = document.getElementById("install-" + key);
        if (target) target.classList.add("active");
      });
    });
  }

  /* ------------------------------------------
     Copy-to-clipboard helpers
  ------------------------------------------ */
  function copyText(text, btn) {
    if (!navigator.clipboard) {
      // Fallback for older browsers
      var ta = document.createElement("textarea");
      ta.value = text;
      ta.style.position = "fixed";
      ta.style.opacity  = "0";
      document.body.appendChild(ta);
      ta.select();
      document.execCommand("copy");
      document.body.removeChild(ta);
      flashBtn(btn);
      return;
    }
    navigator.clipboard.writeText(text).then(function () {
      flashBtn(btn);
    });
  }

  function flashBtn(btn) {
    var original = getLocalizedText(btn, currentLanguage) || btn.textContent;
    btn.textContent = currentLanguage === "zh" ? "已复制" : "Copied!";
    btn.classList.add("copied");
    setTimeout(function () {
      btn.textContent = original;
      btn.classList.remove("copied");
    }, 2000);
  }

  function initCopyButtons() {
    // Hero install copy
    var heroBtn = document.getElementById("hero-copy-btn");
    var heroCmd = document.getElementById("hero-install-cmd");
    if (heroBtn && heroCmd) {
      heroBtn.addEventListener("click", function () {
        copyText(heroCmd.getAttribute("data-copy") || heroCmd.textContent.trim(), heroBtn);
      });
    }

    // Code block copy buttons
    document.querySelectorAll(".code-copy").forEach(function (btn) {
      btn.addEventListener("click", function () {
        var text = btn.getAttribute("data-copy") || "";
        copyText(text, btn);
      });
    });
  }

  function initAvailabilityLogos() {
    document.querySelectorAll(".logo-link .logo-icon").forEach(function (img) {
      var link = img.closest(".logo-link");
      if (!link) return;

      function showFallback() {
        img.style.display = "none";
        link.classList.remove("has-icon");
      }

      function showIcon() {
        link.classList.add("has-icon");
      }

      if (img.complete && img.naturalWidth > 0) {
        showIcon();
      } else if (img.complete) {
        showFallback();
      } else {
        img.addEventListener("load", showIcon, { once: true });
        img.addEventListener("error", showFallback, { once: true });
      }
    });
  }

  /* ------------------------------------------
     Mobile hamburger menu
  ------------------------------------------ */
  function initHamburger() {
    var btn = document.getElementById("hamburger");
    var nav = document.getElementById("site-nav");
    if (!btn || !nav) return;

    btn.addEventListener("click", function () {
      var open = nav.classList.toggle("open");
      btn.setAttribute("aria-expanded", open ? "true" : "false");
    });

    // Close on nav link click
    nav.querySelectorAll("a").forEach(function (link) {
      link.addEventListener("click", function () {
        nav.classList.remove("open");
      });
    });
  }

  /* ------------------------------------------
     Smooth scroll for anchor links
  ------------------------------------------ */
  function initSmoothScroll() {
    document.querySelectorAll('a[href^="#"]').forEach(function (a) {
      a.addEventListener("click", function (e) {
        var id = a.getAttribute("href").slice(1);
        var target = document.getElementById(id);
        if (target) {
          e.preventDefault();
          var offset = 70; // header height
          var top = target.getBoundingClientRect().top + window.pageYOffset - offset;
          window.scrollTo({ top: top, behavior: "smooth" });
        }
      });
    });
  }

  /* ------------------------------------------
     Init
  ------------------------------------------ */
  document.addEventListener("DOMContentLoaded", function () {
    initLanguageToggle();
    loadStableVersion();
    initSectionReveal();
    initHeroTerminalUpdates();
    initDemoTabs();
    initInstallTabs();
    initCopyButtons();
    initAvailabilityLogos();
    initHamburger();
    initSmoothScroll();
  });
})();