Skip to content

feat(cli): Introduce StaticLine #6758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

BlackAsLight
Copy link
Contributor

@BlackAsLight BlackAsLight commented Jul 9, 2025

Read (#6755) for an overview.

#6756 should be merged before this.

This class uses the scrollable region Ansi code to allow scrolling content to not overwrite lines at the top and bottom of the terminal. It is meant to provide a foundation for anyone looking to easily have a line at the bottom or top of the terminal.

import { delay } from "@std/async/delay";
import { StaticLine } from "@std/cli/unstable-static-line";

const id = setInterval(() => console.log(Math.random()), 1000);

const line = new StaticLine();
await line.write("Hello World!");
await delay(1500);
await line.write("How are you?");
await delay(1500);
await line.write("Doing good?");
await delay(1500);
await line.releaseLine();

No tests as of yet have been written for this class as I am not sure how to write unit tests to test its behaviour.

@BlackAsLight BlackAsLight requested a review from kt3k as a code owner July 9, 2025 10:57
@github-actions github-actions bot added the cli label Jul 9, 2025
Copy link

codecov bot commented Jul 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.16%. Comparing base (4db142b) to head (1caadfb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6758   +/-   ##
=======================================
  Coverage   94.15%   94.16%           
=======================================
  Files         587      587           
  Lines       42514    42514           
  Branches     6712     6712           
=======================================
+ Hits        40030    40034    +4     
+ Misses       2433     2431    -2     
+ Partials       51       49    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BlackAsLight BlackAsLight changed the title feat(cli): Introduce askForCursorPositionSync feat(cli): Introduce StaticLine Jul 11, 2025
@BlackAsLight
Copy link
Contributor Author

Example

import { delay } from "@std/async/delay";
const id = setInterval(() => console.log(Math.random()), 1000);

for (let i = 0; i < 20; ++i) {
  const line = new StaticLine();
  await line.write(`Line (${i}): Placeholder!`);
  const id = setInterval(
    () => line.write(`Line (${i}): ${Math.random()}`),
    Math.random() * 3000 + 1000,
  );
  setTimeout(() => {
    clearInterval(id);
    line.releaseLine();
  }, Math.random() * 5_000 + 25_000);
  await delay(Math.random() * 1000 + 1000);
}

await delay(30_000);
clearInterval(id);
Screen.Recording.2025-07-11.at.21.14.31.mp4

@kt3k
Copy link
Member

kt3k commented Jul 22, 2025

Can you provide some real-world example (cli/library/whatever) in which this type of output is used?

Also does this type of abstraction have prior example?

@BlackAsLight
Copy link
Contributor Author

BlackAsLight commented Jul 22, 2025

Can you provide some real-world example (cli/library/whatever) in which this type of output is used?

Also does this type of abstraction have prior example?

I don't know of any real world examples, but as I learn more and more about ANSI escape sequences it is becoming apparent just how unreliable terminals can be.

This type, or some type, of abstraction is needed to "reliably" support multiple spinners and progress bars at once. As request here: #5037. The word reliably is doing a lot of heavy lifting here as any other area that starts inserting sequences can screw this all up.

If we want something truely robust, we'll need to offer some type of complete abstraction away from ansi codes. A framework so to say that isn't merely offering small functions here and there, but that specifies the entire state and keeps track of it all.

Very little of the terminals state can actually be asked or looked up from the terminal itself. Much of these settings and modes must be remembered in code to know if it's enabled or disabled. With the sync functions this isn't really a problem as you can't have random console logs from other areas screwing everything up by sending their own sequences, but with the spinner and progress bar and any other functionality that allows other code to run, allows for the state to be screwed up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants