Last active
March 15, 2026 13:47
-
-
Save NuroDev/88558bb02e7c10513bf8eb72e4024656 to your computer and use it in GitHub Desktop.
π OSC 9;4 terminal progress example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Or use the `osc-progress` package | |
| enum ProgressState { | |
| Hidden = 0, | |
| Normal = 1, | |
| Error = 2, | |
| Indeterminate = 3, | |
| Warning = 4, | |
| } | |
| function setProgress(state: ProgressState, value: number): void { | |
| if (process.stdout.isTTY) { | |
| process.stdout.write(`\x1b]9;4;${state};${value}\x07`); | |
| } | |
| } | |
| function clearProgress(): void { | |
| setProgress(ProgressState.Hidden, 0); | |
| } | |
| // Clean up on exit | |
| process.on('SIGINT', () => { | |
| clearProgress(); | |
| process.exit(0); | |
| }); | |
| async function main(): Promise<void> { | |
| console.log('1. Indeterminate progress (like starting a bundler)...'); | |
| setProgress(ProgressState.Indeterminate, 0); | |
| await Bun.sleep(3000); | |
| console.log('2. Determinate progress (like processing files)...'); | |
| for (let i = 0; i <= 100; i += 5) { | |
| setProgress(ProgressState.Normal, i); | |
| console.log(` Processing... ${i}%`); | |
| await Bun.sleep(150); | |
| } | |
| console.log('3. Warning state...'); | |
| setProgress(ProgressState.Warning, 100); | |
| await Bun.sleep(2000); | |
| console.log('4. Error state...'); | |
| setProgress(ProgressState.Error, 100); | |
| await Bun.sleep(2000); | |
| console.log('5. Done!'); | |
| clearProgress(); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment