Skip to content

fix: resolve 4 bugs in termui - #3657

Open
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-48795
Open

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

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Simplified empty-string validation: comparing trim() to '' misses whitespace-only input; .trim().length === 0 is explicit.
  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).

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: #3656

Summary by CodeRabbit

  • Bug Fixes
    • Whitespace-only lines are now correctly treated as paragraph separators in the chat app.
    • Hexadecimal numeric entities in the RSS reader are decoded more reliably.
    • Progress percentages now round more accurately, especially near whole-number values.
    • Numeric tab selection in the widget gallery now behaves consistently.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Four example applications receive focused fixes for whitespace detection, hexadecimal entity parsing, floating-point percentage rounding, and decimal tab-key parsing.

Changes

Example correctness fixes

Layer / File(s) Summary
Text parsing corrections
examples/chat-app/src/index.tsx, examples/rss-reader/src/index.tsx
Whitespace-only lines now separate chat paragraphs. Hexadecimal entities parse at most eight digits.
Numeric handling corrections
examples/todo-app/src/index.ts, examples/widget-gallery/src/index.ts
Percentage rounding adds Number.EPSILON. Tab-key parsing explicitly uses radix 10.

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

Possibly related PRs

Suggested labels: type:bug, area:examples

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the changes and type, but it omits required package, checklist, GSSoC, and properly linked issue details. Complete the required template sections, identify affected packages, check applicable checklist items, add GSSoC details, and use Closes #3656`` for the linked issue.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies a bug-fix change 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.

@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: 2

🤖 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/rss-reader/src/index.tsx`:
- Around line 28-31: Update the entity-decoding callback in the RSS reader’s
value replacement logic to validate parsed numeric code points before calling
String.fromCodePoint: require an integer within the Unicode range 0 through
0x10ffff, and return the original match for invalid or out-of-range values.
Apply this validation to both hexadecimal and decimal numeric entities.

In `@examples/todo-app/src/index.ts`:
- Line 107: Update the percentage calculation in the progress-value conversion
to add Number.EPSILON after scaling value by 100, ensuring values such as 0.145
round to 15 instead of 14 while preserving the existing Math.round behavior.
🪄 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: 0e838808-c101-4991-b3d9-38d0560e7e9e

📥 Commits

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

📒 Files selected for processing (4)
  • examples/chat-app/src/index.tsx
  • examples/rss-reader/src/index.tsx
  • examples/todo-app/src/index.ts
  • examples/widget-gallery/src/index.ts

Comment on lines 28 to 31
return value.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, entity: string) => {
if (entity.startsWith('#x')) {
const codePoint = Number.parseInt(entity.slice(2), 16);
const codePoint = Number.parseInt(entity.slice(2, 10), 16);
return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
node <<'NODE'
for (const entity of ['&`#x00000041dead`;', '&`#x110000`;']) {
  const value = entity.match(/&(`#x`?[0-9a-fA-F]+);/)[1];
  const codePoint = Number.parseInt(value.slice(2, 10), 16);
  try {
    console.log(entity, String.fromCodePoint(codePoint));
  } catch (error) {
    console.error(entity, error.name);
  }
}
NODE

Repository: Karanjot786/TermUI

Length of output: 197


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'File matches:\n'
fd -a 'index\.tsx$' . | sed 's#^\./##' | rg 'examples/rss-reader/src/index\.tsx|rss-reader' || true

printf '\nRelevant source:\n'
cat -n examples/rss-reader/src/index.tsx | sed -n '1,70p'

printf '\nBehavioral probe matching exact logic:\n'
node - <<'NODE'
function decode(value, entity) {
  const codePoint = Number.parseInt(entity.slice(2, 10), 16);
  try {
    return { ok: true, value: Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : value };
  } catch (error) {
    return { ok: false, name: error.name, message: error.message };
  }
}

for (const entity of [
  '&`#x00000041dead`;',
  '&`#x110000`;',
  '&`#x10ffff`;',
  '&`#x110001`;',
  '&`#12345`;',
  '&`#9999999`;',
  '&`#110000`;',
  '&`#x0041`;',
  '&`#xc0`;'
]) {
  const match = entity.match(/&(`#x`?[0-9a-fA-F]+);/);
  if (!match) throw new TypeError('Unexpected entity shape');
  const result = decode(entity, match[1]);
  console.log(JSON.stringify({ entity, parsed: match[1], result }));
}
NODE

Repository: Karanjot786/TermUI

Length of output: 3709


Reject out-of-range numeric entities before decoding.

The current checks allow decimal values such as &#110000;, which exceeds the Unicode code-point limit and throws in String.fromCodePoint. Reject non-integer code points and values above 0x10ffff before decoding.

🤖 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/rss-reader/src/index.tsx` around lines 28 - 31, Update the
entity-decoding callback in the RSS reader’s value replacement logic to validate
parsed numeric code points before calling String.fromCodePoint: require an
integer within the Unicode range 0 through 0x10ffff, and return the original
match for invalid or out-of-range values. Apply this validation to both
hexadecimal and decimal numeric entities.

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:

#!/usr/bin/env bash
set -euo pipefail

node <<'NODE'
const value = 0.145;
const current = Math.round(value * 100 + Number.EPSILON);
const corrected = Math.round((value + Number.EPSILON) * 100);

if (current !== 14 || corrected !== 15) {
  throw new Error(`Unexpected results: ${current}, ${corrected}`);
}
NODE

Repository: Karanjot786/TermUI

Length of output: 156


🌐 Web query:

MDN Number.EPSILON value JavaScript

💡 Result:

The Number.EPSILON property in JavaScript represents the difference between 1 and the smallest floating-point number greater than 1 [1][2]. Its value is 2^-52, which is approximately 2.220446049250313e-16 [1][3]. This property is a static data property of the Number object and cannot be changed, as its attributes are [[Writable]]: false, [[Enumerable]]: false, and [[Configurable]]: false [4][5]. It is primarily used to handle floating-point arithmetic inaccuracies when comparing numbers that are close in value, as standard equality operators (===) can be unreliable for floating-point calculations [2].

Citations:


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

node - <<'NODE'
const cases = [
  { value: 0.145, label: '0.145' },
  { value: Number.EPSILON / 100, label: 'Number.EPSILON/100' },
  { value: 0.005, label: '0.005' },
  { value: 0.999999999999999, label: 'near 1' },
 ];

for (const { value, label } of cases) {
  const current = Math.round(value * 100 + Number.EPSILON);
  const corrected = Math.round((value + Number.EPSILON) * 100);
  console.log(label.padEnd(22), 'current:', current, 'corrected:', corrected, 'same:', current === corrected, 'value:', value, '0.145*100:', 0.145 * 100, 'eps:', Number.EPSILON);
}
NODE

Repository: Karanjot786/TermUI

Length of output: 703


Add Number.EPSILON before scaling the percentage.

Number.EPSILON is too small to reliably change the result after multiplication by 100. Use the scaled input so 0.145 rounds to 15 instead of 14.

-            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 in the progress-value conversion to add Number.EPSILON after scaling
value by 100, ensuring values such as 0.145 round to 15 instead of 14 while
preserving the existing Math.round behavior.

@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant