Skip to content

fix: resolve 4 bugs in termui - #3511

Closed
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-80869
Closed

fix: resolve 4 bugs in termui#3511
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-80869

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).
  • Fixed default sort: .sort() coerces elements to strings, so [10, 9, 2] sorts as [10, 2, 9]; numeric comparator sorts correctly.
  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.
  • Replaced global isNaN with Number.isNaN: the global version coerces its argument, so isNaN('1') returns false while Number.isNaN is strict.

Type of Change

  • Bug fix (non-breaking change fixing an issue)

How Has This Been Tested?

  • Local manual testing

Checklist

  • My code follows the style guidelines
  • I have performed a self-review

Related Issue

Ref: #3510

Summary by CodeRabbit

  • Bug Fixes
    • Improved progress percentage rounding so displayed values are more accurate, particularly near whole-number boundaries.
    • Strengthened select-input validation to handle invalid choices more reliably while preserving existing range checks and retry behavior.
    • Improved dependency ordering during build processing for more consistent generated results.

@github-actions github-actions Bot added the type:bug +10 pts. Bug fix. label Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes update progress rounding, select prompt validation, and dependency sorting. Each change affects one line in a separate part of the repository.

Changes

Progress percentage rounding

Layer / File(s) Summary
Progress percentage calculation
examples/todo-app/src/index.ts
The rendered progress percentage adds Number.EPSILON before rounding.

Select input validation

Layer / File(s) Summary
Parsed select input validation
packages/ui/src/prompts.ts
promptSelect uses Number.isNaN to reject non-numeric parsed input.

Dependency sorting

Layer / File(s) Summary
Dependency name ordering
scripts/build-registry.ts
collectDeps uses a subtraction comparator when sorting dependency names.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fixes and testing, but it omits the required package section and does not provide a linked issue in the required Closes # format. Add the affected package names, change the issue reference to Closes #3510`` with a link, and complete the repository checklist and GSSoC fields.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the pull request as four bug fixes and follows the required type: short description format.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added area:examples Example apps. area:ui @termuijs/ui labels Aug 4, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/todo-app/src/index.ts`:
- Line 107: Update the percentage calculation assigned to pct so Number.EPSILON
is added to value before multiplying by 100, then retain Math.round for the
final integer percentage.

In `@packages/ui/src/prompts.ts`:
- Line 116: Update the select-input validation around the parsed variable n to
reject malformed values such as “2abc”: validate that the complete user input
represents an integer, then retain the existing 1-to-choices.length range check
before selecting an option.

In `@scripts/build-registry.ts`:
- Line 47: Update the dependency sorting in the registry-building flow to use
lexicographic string ordering rather than numeric subtraction. In the visible
sort expression within the dependency collection logic, use the default string
comparator so package names such as `@termuijs/core`, `@termuijs/motion`, and
`@termuijs/widgets` are emitted deterministically.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: df4e2bf6-fb43-4ced-866e-8950865672a3

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and a3f03f0.

📒 Files selected for processing (3)
  • examples/todo-app/src/index.ts
  • packages/ui/src/prompts.ts
  • scripts/build-registry.ts

const filled = Math.round(barWidth * value);

const pct = Math.round(value * 100);
const pct = Math.round(value * 100 + Number.EPSILON);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file excerpt =="
sed -n '80,125p' examples/todo-app/src/index.ts

echo
echo "== occurrences of pct/value =="
rg -n "const pct|value \* 100|Number\.EPSILON|progress|percent" examples/todo-app/src/index.ts

echo
echo "== JS behavior probe =="
node - <<'JS'
const values = [0.285, 0.1, 0.5, 0.165, 0.28499999999999995, 0.28500000000000004];
for (const value of values) {
  const a = Math.round(value * 100 + Number.EPSILON);
  const b = Math.round((value + Number.EPSILON) * 100);
  console.log(JSON.stringify(Object.entries({
    value,
    value_times_100: value * 100,
    epsilon: Number.EPSILON,
    expression_a_result: a,
    expression_a_rounded_input: value * 100 + Number.EPSILON,
    expression_a_midpoint_delta: (value * 100 + Number.EPSILON) - 28.5,
    proposed_b_result: b,
    proposed_b_rounded_input: (value + Number.EPSILON) * 100,
    proposed_b_midpoint_delta: (value + Number.EPSILON) * 100 - 28.5
  })));
}
JS

Repository: Karanjot786/TermUI

Length of output: 4865


Apply the epsilon before percentage scaling.

Number.EPSILON is added after value * 100, where it is too small to correct half-boundary floating-point errors. For example, 0.285 * 100 rounds down to 28, while adding the epsilon before * 100 rounds it to 29.

Proposed fix
-            const pct = Math.round(value * 100 + Number.EPSILON);
+            const pct = Math.round((value + Number.EPSILON) * 100);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const pct = Math.round(value * 100 + Number.EPSILON);
const pct = Math.round((value + Number.EPSILON) * 100);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/todo-app/src/index.ts` at line 107, Update the percentage
calculation assigned to pct so Number.EPSILON is added to value before
multiplying by 100, then retain Math.round for the final integer percentage.

}
const n = parseInt(trimmed, 10);
if (!isNaN(n) && n >= 1 && n <= choices.length) {
if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Reject malformed select input.

n is already a number from parseInt, so Number.isNaN(n) does not change the behavior of the previous check. parseInt('2abc', 10) returns 2, so malformed input is still accepted. Parse the complete value and require an integer before selecting an option.

Proposed fix
-                const n = parseInt(trimmed, 10);
-                if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {
+                const n = Number(trimmed);
+                if (Number.isInteger(n) && n >= 1 && n <= choices.length) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {
const n = Number(trimmed);
if (Number.isInteger(n) && n >= 1 && n <= choices.length) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/prompts.ts` at line 116, Update the select-input validation
around the parsed variable n to reject malformed values such as “2abc”: validate
that the complete user input represents an integer, then retain the existing
1-to-choices.length range check before selecting an option.

Comment thread scripts/build-registry.ts
let m: RegExpExecArray | null;
while ((m = re.exec(content)) !== null) deps.add(m[1]!);
return [...deps].sort();
return [...deps].sort((a, b) => a - b);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Use a string comparator for dependency names.

a and b are package-name strings, so a - b evaluates to NaN. The sort then preserves insertion order instead of sorting dependencies. The provided test expects @termuijs/core, @termuijs/motion, and @termuijs/widgets, so this change makes that test fail and can make registry output depend on source order.

Use the default lexicographic sort:

Proposed fix
-  return [...deps].sort((a, b) => a - b);
+  return [...deps].sort();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return [...deps].sort((a, b) => a - b);
return [...deps].sort();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/build-registry.ts` at line 47, Update the dependency sorting in the
registry-building flow to use lexicographic string ordering rather than numeric
subtraction. In the visible sort expression within the dependency collection
logic, use the default string comparator so package names such as
`@termuijs/core`, `@termuijs/motion`, and `@termuijs/widgets` are emitted
deterministically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:examples Example apps. area:ui @termuijs/ui type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant