feat(temperature): %temperature-k% token for Kelvin (#2784)

* added kelvin option for module/temperature

* changelog for the changes i made

* fixed typos

* fixed the temperature conversion to be more precise

* Update CHANGELOG.md

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>

* changed the calculation of the different temperatures

it now uses float as a initial value and makes m_temp temp_k and temp_f by converting and rounding with std::round

* std::lround makes more sense to use than std::round

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Quantenzitrone 2022-08-21 19:10:48 +02:00 committed by GitHub
parent b5764c8a93
commit 708bd9c891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `custom/text`: The `content` setting and all its properties are deprecated in favor of `format` with the same functionality. ([`#2676`](https://github.com/polybar/polybar/pull/2676)) - `custom/text`: The `content` setting and all its properties are deprecated in favor of `format` with the same functionality. ([`#2676`](https://github.com/polybar/polybar/pull/2676))
### Added ### Added
- `internal/temperature`: `%temperature-k%` token displays the temperature in kelvin ([`#2774`](https://github.com/polybar/polybar/discussions/2774), [`#2784`](https://github.com/polybar/polybar/pull/2784))
- `internal/pulseaudio`: `reverse-scroll` option ([`#2664`](https://github.com/polybar/polybar/pull/2664)) - `internal/pulseaudio`: `reverse-scroll` option ([`#2664`](https://github.com/polybar/polybar/pull/2664))
- `custom/script`: Repeat interval for script failure (`interval-fail`) and `exec-if` (`interval-if`) ([`#943`](https://github.com/polybar/polybar/issues/943), [`#2606`](https://github.com/polybar/polybar/issues/2606), [`#2630`](https://github.com/polybar/polybar/pull/2630)) - `custom/script`: Repeat interval for script failure (`interval-fail`) and `exec-if` (`interval-if`) ([`#943`](https://github.com/polybar/polybar/issues/943), [`#2606`](https://github.com/polybar/polybar/issues/2606), [`#2630`](https://github.com/polybar/polybar/pull/2630))
- `custom/text`: Loads the `format` setting, which supports the `<label>` tag, if the deprecated `content` is not defined ([`#1331`](https://github.com/polybar/polybar/issues/1331), [`#2673`](https://github.com/polybar/polybar/pull/2673), [`#2676`](https://github.com/polybar/polybar/pull/2676)) - `custom/text`: Loads the `format` setting, which supports the `<label>` tag, if the deprecated `content` is not defined ([`#1331`](https://github.com/polybar/polybar/issues/1331), [`#2673`](https://github.com/polybar/polybar/pull/2673), [`#2676`](https://github.com/polybar/polybar/pull/2676))

View File

@ -51,22 +51,27 @@ namespace modules {
} }
bool temperature_module::update() { bool temperature_module::update() {
m_temp = std::strtol(file_util::contents(m_path).c_str(), nullptr, 10) / 1000.0f + 0.5f; float temp = float(std::strtol(file_util::contents(m_path).c_str(), nullptr, 10)) / 1000.0;
int temp_f = floor(((1.8 * m_temp) + 32) + 0.5); m_temp = std::lround( temp );
int temp_f = std::lround( (temp * 1.8) + 32.0 );
int temp_k = std::lround( temp + 273.15 );
string temp_c_string = to_string(m_temp); string temp_c_string = to_string(m_temp);
string temp_f_string = to_string(temp_f); string temp_f_string = to_string(temp_f);
string temp_k_string = to_string(temp_k);
// Add units if `units = true` in config // Add units if `units = true` in config
if(m_units) { if(m_units) {
temp_c_string += "°C"; temp_c_string += "°C";
temp_f_string += "°F"; temp_f_string += "°F";
temp_k_string += "K";
} }
const auto replace_tokens = [&](label_t& label) { const auto replace_tokens = [&](label_t& label) {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%temperature-f%", temp_f_string); label->replace_token("%temperature-f%", temp_f_string);
label->replace_token("%temperature-c%", temp_c_string); label->replace_token("%temperature-c%", temp_c_string);
label->replace_token("%temperature-k%", temp_k_string);
// DEPRECATED: Will be removed in later release // DEPRECATED: Will be removed in later release
label->replace_token("%temperature%", temp_c_string); label->replace_token("%temperature%", temp_c_string);