Repository navigation
Add scientific and programmer calculator - #2090
Sushant215-cpu wants to merge 4 commits into
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
[Medium impact] The PR adds a calculator workflow within the terminal app. The PR needs fixes before merging because shifts, continued calculations, and history recall can return wrong answers. Findings
Fix with agent prompt### Issue 1
crates/jcode-tui/src/tui/app/calculator.rs:196-200
Evaluate `sin(30)` in degrees, switch to radians, then select that calculation with F9 and Enter. History shows `0.5`, but recall copies only the expression and recalculates it in radians, showing about `-0.988`. Save the angle mode with each entry and restore it before recalculating.
### Issue 2
crates/jcode-tui/src/tui/app/calculator.rs:512-513
`parse_shift` checks the size only after `wrapping_shl` has discarded overflowing bits. For example, `/calc 4 << 62` returns `0` instead of rejecting a result outside the supported 53-bit range. Shift in `i128` and check the range before narrowing the result.
### Issue 3
crates/jcode-tui/src/tui/app/calculator.rs:337-339
After evaluation, an operator appends to the original expression rather than the displayed answer. Pressing `2`, `+`, `3`, Enter, `*`, `4`, Enter evaluates `2+3*4` and shows `14`; continuing from the displayed answer `5` should give `20`. When an operator follows a successful evaluation, start the next expression from the saved result.
### Issue 4
crates/jcode-tui/src/tui/app/input.rs:2728-2729
The calculator captures keys but not terminal paste events. With `/calc` open, pasting `1+2` still calls `handle_paste`, which inserts it into the hidden chat draft instead of `CalculatorOverlay.expression`. Route pasted text to the calculator while it is open, without changing the draft.
### Issue 5
crates/jcode-tui/src/tui/ui_overlays.rs:undefined-337
Each frame clears the text-selection snapshot, and the calculator returns before recording a replacement. Its key handler also has no copy action. Add a copy-result shortcut or make the displayed text selectable so users do not have to retype long answers.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
### Issue 6
crates/jcode-tui/src/tui/ui_overlays.rs:520-523
At terminal widths of 49 columns or less, the fixed spacer pushes the entire `DEG`/`RAD` label out of view. Users can still press `d` and run trigonometric calculations, but cannot see which angle mode produced the answer. Reserve space for the angle label before adding padding or truncating the header.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
### Issue 7
crates/jcode-tui/src/tui/ui_overlays.rs:414-415
When the terminal is shorter than eight rows or narrower than 20 columns, this new guard draws nothing. The caller has already cleared the frame and still returns without drawing the chat, so opening or resizing the calculator leaves a blank screen while it continues capturing keys. Show a compact result or a size warning with the close shortcut instead of returning silently.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryAdds a scientific and programmer calculator through
Reviews (4) · Last reviewed commit: "Add calculator history and white theme" · Reviewed by Greptile |
| let shifted = as_integer(value)?.wrapping_shl(shift as u32); | ||
| if shifted.unsigned_abs() > (1u64 << 53) - 1 { |
There was a problem hiding this comment.
Large shifts return wrong answers
parse_shift checks the size only after wrapping_shl has discarded overflowing bits. For example, /calc 4 << 62 returns 0 instead of rejecting a result outside the supported 53-bit range. Shift in i128 and check the range before narrowing the result.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/app/calculator.rs
Line: 386-387
Comment:
**Large shifts return wrong answers**
`parse_shift` checks the size only after `wrapping_shl` has discarded overflowing bits. For example, `/calc 4 << 62` returns `0` instead of rejecting a result outside the supported 53-bit range. Shift in `i128` and check the range before narrowing the result.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| overlay.expression.clear(); | ||
| } | ||
| overlay.expression.push_str(text); |
There was a problem hiding this comment.
Continued calculations reuse old expressions
After evaluation, an operator appends to the original expression rather than the displayed answer. Pressing 2, +, 3, Enter, *, 4, Enter evaluates 2+3*4 and shows 14; continuing from the displayed answer 5 should give 20. When an operator follows a successful evaluation, start the next expression from the saved result.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/app/calculator.rs
Line: 233-235
Comment:
**Continued calculations reuse old expressions**
After evaluation, an operator appends to the original expression rather than the displayed answer. Pressing `2`, `+`, `3`, Enter, `*`, `4`, Enter evaluates `2+3*4` and shows `14`; continuing from the displayed answer `5` should give `20`. When an operator follows a successful evaluation, start the next expression from the saved result.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| } else if app.calculator_overlay.is_some() { | ||
| app.handle_calculator_key(code)?; |
There was a problem hiding this comment.
The calculator captures keys but not terminal paste events. With /calc open, pasting 1+2 still calls handle_paste, which inserts it into the hidden chat draft instead of CalculatorOverlay.expression. Route pasted text to the calculator while it is open, without changing the draft.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/app/input.rs
Line: 2728-2729
Comment:
**Pasted expressions enter chat**
The calculator captures keys but not terminal paste events. With `/calc` open, pasting `1+2` still calls `handle_paste`, which inserts it into the hidden chat draft instead of `CalculatorOverlay.expression`. Route pasted text to the calculator while it is open, without changing the draft.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| .iter() | ||
| .map(|line| jcode_tui_render::truncate_line_with_ellipsis_to_width(line, inner_width)) | ||
| .collect::<Vec<_>>(); | ||
| frame.render_widget(Paragraph::new(lines).block(block), popup); |
There was a problem hiding this comment.
Each frame clears the text-selection snapshot, and the calculator returns before recording a replacement. Its key handler also has no copy action. Add a copy-result shortcut or make the displayed text selectable so users do not have to retype long answers.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/ui_overlays.rs
Line: 337
Comment:
**Answers cannot be copied**
Each frame clears the text-selection snapshot, and the calculator returns before recording a replacement. Its key handler also has no copy action. Add a copy-result shortcut or make the displayed text selectable so users do not have to retype long answers.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
| let header = Line::from(vec
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| if width < 20 || height < 8 { | ||
| return; |
There was a problem hiding this comment.
When the terminal is shorter than eight rows or narrower than 20 columns, this new guard draws nothing. The caller has already cleared the frame and still returns without drawing the chat, so opening or resizing the calculator leaves a blank screen while it continues capturing keys. Show a compact result or a size warning with the close shortcut instead of returning silently.
Knowledge Base Used: Terminal user interface
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/ui_overlays.rs
Line: 320-321
Comment:
**Small panes go blank**
When the terminal is shorter than eight rows or narrower than 20 columns, this new guard draws nothing. The caller has already cleared the frame and still returns without drawing the chat, so opening or resizing the calculator leaves a blank screen while it continues capturing keys. Show a compact result or a size warning with the close shortcut instead of returning silently.
**Knowledge Base Used:** [Terminal user interface](https://app.greptile.com/-/switch-tenant?tenant=solo-systems&next=%2Fcustom-context%2Fknowledge-base%2F1jehuang%2Fjcode%2F-%2Fdocs%2Fterminal-user-interface.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
| overlay.expression = overlay.history[*index].expression.clone(); | ||
| overlay.history_visible = false; | ||
| overlay.history_query.clear(); | ||
| overlay.history_cursor = 0; | ||
| update_calculator_preview(&mut overlay); |
There was a problem hiding this comment.
History recall changes saved answers
Evaluate sin(30) in degrees, switch to radians, then select that calculation with F9 and Enter. History shows 0.5, but recall copies only the expression and recalculates it in radians, showing about -0.988. Save the angle mode with each entry and restore it before recalculating.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-tui/src/tui/app/calculator.rs
Line: 196-200
Comment:
**History recall changes saved answers**
Evaluate `sin(30)` in degrees, switch to radians, then select that calculation with F9 and Enter. History shows `0.5`, but recall copies only the expression and recalculates it in radians, showing about `-0.988`. Save the angle mode with each entry and restore it before recalculating.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
No description provided.