diff --git a/AUTHORS.md b/AUTHORS.md index 487a3838e..f09ba9bb7 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -165,6 +165,7 @@ date of first contribution): * [Johan Verrept](https://github.com/JoveToo) * [Nicu Surdu](https://github.com/surdu) * [Zack Lewis](https://github.com/lima3w) + * [Billy Richardson](https://github.com/richardsondev) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index fcea464ef..fd0faecba 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -652,13 +652,25 @@ function cleanTemperature(temp, offThreshold) { return temp; } -function formatTemperature(temp, showF, offThreshold) { +function formatTemperature(temp, showF, offThreshold, returnUnicode) { if (temp === undefined || !_.isNumber(temp)) return "-"; if (offThreshold !== undefined && temp < offThreshold) return gettext("off"); + + var degreeSymbol = "°"; + if (returnUnicode !== undefined && returnUnicode) { + degreeSymbol = "\u00B0"; + } + if (showF) { - return _.sprintf("%.1f°C (%.1f°F)", temp, (temp * 9) / 5 + 32); + return _.sprintf( + "%.1f%sC (%.1f%sF)", + temp, + degreeSymbol, + (temp * 9) / 5 + 32, + degreeSymbol + ); } else { - return _.sprintf("%.1f°C", temp); + return _.sprintf("%.1f%sC", temp, degreeSymbol); } } diff --git a/src/octoprint/templates/tabs/temperature.jinja2 b/src/octoprint/templates/tabs/temperature.jinja2 index 57c21fb87..438ae097a 100644 --- a/src/octoprint/templates/tabs/temperature.jinja2 +++ b/src/octoprint/templates/tabs/temperature.jinja2 @@ -49,7 +49,7 @@ + + + + diff --git a/tests/static/js/test-helpers.js b/tests/static/js/test-helpers.js index f2cdc9ac9..89b947a06 100644 --- a/tests/static/js/test-helpers.js +++ b/tests/static/js/test-helpers.js @@ -85,3 +85,107 @@ QUnit.cases( "As expected: " + String(params.expected) ); }); + +QUnit.module("formatTemperature"); +QUnit.cases( + (function () { + var cases = []; + + var params = [ + { + input: undefined, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "-" + }, + { + input: "", + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "-" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: 1.1, + expected: "off" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: 1.0, + expected: "1.0°C" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.0°C" + }, + { + input: 1.0, + showF: true, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.0°C (33.8°F)" + }, + { + input: 1.0, + showF: true, + useUnicode: true, + offThreshold: undefined, + expected: "1.0\u00B0C (33.8\u00B0F)" + }, + { + input: 1.0, + showF: undefined, + useUnicode: false, + offThreshold: undefined, + expected: "1.0°C" + }, + { + input: 1.0, + showF: undefined, + useUnicode: true, + offThreshold: undefined, + expected: "1.0\u00B0C" + }, + { + input: 1.1, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.1°C" + } + ]; + + var param, i; + for (i = 0; i < params.length; i++) { + param = params[i]; + param["title"] = + String(param.input) + + String(param.showF) + + String(param.offThreshold) + + String(param.useUnicode); + cases.push(param); + } + + return cases; + })() +).test("formatTemperature", function (params, assert) { + assert.equal( + params.expected, + formatTemperature( + params.input, + params.showF, + params.offThreshold, + params.useUnicode + ), + "As expected: " + String(params.expected) + ); +});