@timmy6942025/cli-timer 1.1.14 → 1.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +54 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timmy6942025/cli-timer",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "description": "Simple customizable terminal timer and stopwatch",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/index.js CHANGED
@@ -83,6 +83,30 @@ function showCursor() {
83
83
  process.stdout.write("\x1b[?25h");
84
84
  }
85
85
 
86
+ function enterAlternateScreen() {
87
+ process.stdout.write("\x1b[?1049h");
88
+ }
89
+
90
+ function exitAlternateScreen() {
91
+ process.stdout.write("\x1b[?1049l");
92
+ }
93
+
94
+ function disableLineWrap() {
95
+ process.stdout.write("\x1b[?7l");
96
+ }
97
+
98
+ function enableLineWrap() {
99
+ process.stdout.write("\x1b[?7h");
100
+ }
101
+
102
+ function enableMouseCapture() {
103
+ process.stdout.write("\x1b[?1000h\x1b[?1006h");
104
+ }
105
+
106
+ function disableMouseCapture() {
107
+ process.stdout.write("\x1b[?1000l\x1b[?1006l");
108
+ }
109
+
86
110
  function formatHms(totalSeconds) {
87
111
  const hours = Math.floor(totalSeconds / 3600);
88
112
  const minutes = Math.floor((totalSeconds % 3600) / 60);
@@ -570,6 +594,13 @@ function toDisplayLines(text) {
570
594
  return lines.length > 0 ? lines : [""];
571
595
  }
572
596
 
597
+ function writeFrameLines(lines) {
598
+ const safeLines = Array.isArray(lines) && lines.length > 0 ? lines : [""];
599
+ const terminalHeight = process.stdout.rows || safeLines.length;
600
+ const visibleLines = safeLines.slice(0, Math.max(1, terminalHeight));
601
+ process.stdout.write(visibleLines.join("\n"));
602
+ }
603
+
573
604
  function writeCenteredBlock(lines) {
574
605
  const safeLines = lines.length > 0 ? lines : [""];
575
606
  const terminalWidth = process.stdout.columns || 120;
@@ -585,8 +616,7 @@ function writeCenteredBlock(lines) {
585
616
  output += "\n".repeat(padTop);
586
617
  }
587
618
  output += safeLines.map((line) => `${leftPrefix}${line}`).join("\n");
588
- output += "\n";
589
- process.stdout.write(output);
619
+ writeFrameLines(toDisplayLines(output));
590
620
  }
591
621
 
592
622
  function writeCenteredBlockWithTop(topLines, centerLines) {
@@ -609,8 +639,7 @@ function writeCenteredBlockWithTop(topLines, centerLines) {
609
639
  output += "\n".repeat(padTop);
610
640
  }
611
641
  output += safeCenter.map((line) => `${leftPrefix}${line}`).join("\n");
612
- output += "\n";
613
- process.stdout.write(output);
642
+ writeFrameLines(toDisplayLines(output));
614
643
  }
615
644
 
616
645
  function drawFrame({ mode, seconds, paused, config, done }) {
@@ -646,7 +675,7 @@ function drawFrame({ mode, seconds, paused, config, done }) {
646
675
  writeCenteredBlockWithTop(topLines, centerLines);
647
676
  } else {
648
677
  const lines = [...topLines, ...centerLines];
649
- process.stdout.write(`${lines.join("\n")}\n`);
678
+ writeFrameLines(lines);
650
679
  }
651
680
  }
652
681
 
@@ -958,6 +987,9 @@ function runClock({ mode, initialSeconds, config }) {
958
987
  let tick = null;
959
988
  let lastDrawState = "";
960
989
  let hasExited = false;
990
+ let didEnterAlternateScreen = false;
991
+ let didDisableLineWrap = false;
992
+ let didEnableMouseCapture = false;
961
993
 
962
994
  const stdin = process.stdin;
963
995
 
@@ -1053,7 +1085,17 @@ function runClock({ mode, initialSeconds, config }) {
1053
1085
  }
1054
1086
  stdin.pause();
1055
1087
  showCursor();
1056
- clearScreen();
1088
+ if (didEnableMouseCapture) {
1089
+ disableMouseCapture();
1090
+ }
1091
+ if (didDisableLineWrap) {
1092
+ enableLineWrap();
1093
+ }
1094
+ if (didEnterAlternateScreen) {
1095
+ exitAlternateScreen();
1096
+ } else {
1097
+ clearScreen();
1098
+ }
1057
1099
  process.exit(code);
1058
1100
  }
1059
1101
 
@@ -1112,6 +1154,12 @@ function runClock({ mode, initialSeconds, config }) {
1112
1154
  stdin.resume();
1113
1155
  stdin.setEncoding("utf8");
1114
1156
  stdin.on("data", onKeypress);
1157
+ enterAlternateScreen();
1158
+ didEnterAlternateScreen = true;
1159
+ disableLineWrap();
1160
+ didDisableLineWrap = true;
1161
+ enableMouseCapture();
1162
+ didEnableMouseCapture = true;
1115
1163
  hideCursor();
1116
1164
 
1117
1165
  draw(true);