feat: Dynamically adjust result display width based on font
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -12,7 +12,6 @@ require("FontDylex7x13").add(Graphics);
|
|||||||
|
|
||||||
var DEFAULT_SELECTION_NUMBERS = '5';
|
var DEFAULT_SELECTION_NUMBERS = '5';
|
||||||
var RESULT_HEIGHT = 40;
|
var RESULT_HEIGHT = 40;
|
||||||
var RESULT_MAX_LEN = Math.floor((g.getWidth() - 20) / 16);
|
|
||||||
var COLORS = {
|
var COLORS = {
|
||||||
// [normal, selected]
|
// [normal, selected]
|
||||||
DEFAULT: ['#7F8183', '#A6A6A7'],
|
DEFAULT: ['#7F8183', '#A6A6A7'],
|
||||||
@@ -218,6 +217,7 @@ function doMath(x, y, operator) {
|
|||||||
function displayOutput(num) {
|
function displayOutput(num) {
|
||||||
g.setBgColor(0).clearRect(0, 0, g.getWidth(), RESULT_HEIGHT-1);
|
g.setBgColor(0).clearRect(0, 0, g.getWidth(), RESULT_HEIGHT-1);
|
||||||
g.setColor(-1);
|
g.setColor(-1);
|
||||||
|
g.setFont('Dylex7x13', 2);
|
||||||
if (num === Infinity || num === -Infinity || isNaN(num)) {
|
if (num === Infinity || num === -Infinity || isNaN(num)) {
|
||||||
// handle division by 0
|
// handle division by 0
|
||||||
if (num === Infinity) {
|
if (num === Infinity) {
|
||||||
@@ -235,7 +235,6 @@ function displayOutput(num) {
|
|||||||
operator = null;
|
operator = null;
|
||||||
specials.R.val = 'AC';
|
specials.R.val = 'AC';
|
||||||
if (!swipeEnabled) drawKey('R', specials.R);
|
if (!swipeEnabled) drawKey('R', specials.R);
|
||||||
g.setFont('Dylex7x13', 2);
|
|
||||||
} else {
|
} else {
|
||||||
// might not be a number due to display of dot "."
|
// might not be a number due to display of dot "."
|
||||||
var numNumeric = Number(num);
|
var numNumeric = Number(num);
|
||||||
@@ -253,27 +252,36 @@ function displayOutput(num) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var numStr = num.toString();
|
var numStr = num.toString();
|
||||||
if (typeof num === 'number' && (numStr.length > RESULT_MAX_LEN || (num !== 0 && Math.abs(num) < 1e-4))) {
|
var displayStr = addSeparators(numStr);
|
||||||
// dynamic precision based on exponent length
|
|
||||||
var exp = Math.floor(Math.log(Math.abs(num)) / Math.LN10);
|
if (typeof num === 'number' && (g.stringWidth(displayStr) > g.getWidth() - 20 || (num !== 0 && Math.abs(num) < 1e-4))) {
|
||||||
var expLen = String(Math.abs(exp)).length;
|
// try to format as scientific notation
|
||||||
var signLen = (num < 0) ? 1 : 0;
|
let precision = 10; // start with high precision
|
||||||
// Total length is sign + first-digit + dot + fraction + E + exp-sign + exp-digits
|
while (precision >= 0) {
|
||||||
// So precision for fraction is MAX - (sign + first-digit + dot + E + exp-sign + exp-digits)
|
let scientificStr = toExponential(num, precision).replace("e", "E");
|
||||||
let precision = RESULT_MAX_LEN - (signLen + 1 + 1 + 1 + 1 + expLen);
|
if (g.stringWidth(scientificStr) <= g.getWidth() - 20) {
|
||||||
if (precision < 0) precision = 0;
|
displayStr = scientificStr;
|
||||||
num = toExponential(num, precision).replace("e", "E");
|
break;
|
||||||
} else {
|
|
||||||
num = addSeparators(numStr);
|
|
||||||
}
|
}
|
||||||
|
precision--;
|
||||||
|
}
|
||||||
|
if (precision < 0) { // if it still doesn't fit
|
||||||
|
displayStr = toExponential(num, 0).replace("e", "E");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num = displayStr;
|
||||||
|
|
||||||
|
// final check for truncation
|
||||||
|
if (g.stringWidth(num) > g.getWidth() - 20) {
|
||||||
|
while(g.stringWidth(num+'...') > g.getWidth() - 20 && num.length > 1) {
|
||||||
|
num = num.slice(0,-1);
|
||||||
|
}
|
||||||
|
num += '...';
|
||||||
|
}
|
||||||
|
|
||||||
if (num.charAt(0) === '-') {
|
if (num.charAt(0) === '-') {
|
||||||
num = '- ' + num.substr(1);
|
num = '- ' + num.substr(1);
|
||||||
}
|
}
|
||||||
g.setFont('Dylex7x13', 2);
|
|
||||||
if (num.length > RESULT_MAX_LEN) {
|
|
||||||
if (num.indexOf("E") < 0)
|
|
||||||
num = num.substr(0, RESULT_MAX_LEN - 1)+'...';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
g.setFontAlign(1,0);
|
g.setFontAlign(1,0);
|
||||||
g.drawString(num, g.getWidth()-20, RESULT_HEIGHT/2);
|
g.drawString(num, g.getWidth()-20, RESULT_HEIGHT/2);
|
||||||
|
|||||||
Reference in New Issue
Block a user