Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indicators changes locale #126

Open
albertocasagrande opened this issue Aug 9, 2023 · 1 comment
Open

indicators changes locale #126

albertocasagrande opened this issue Aug 9, 2023 · 1 comment

Comments

@albertocasagrande
Copy link

set_progress and mark_as_completed change the program locale and set it to the shell one.

Demo

#include <iostream>
#include <locale>

#include "single_include/indicators/indicators.hpp"

void main() {
  indicators::indicators::ProgressBar bar;
    
  std::cout << std::setlocale(LC_ALL,nullptr) << std::endl;  // print "C"

  bar.set_progress(50);
  //bar.mark_as_completed();
 
  std::cout << std::setlocale(LC_ALL,nullptr) << std::endl;  // print the shell locale, e.g., "en_US.UTF-8"
}

Bug location and possible solution

In the function(s) utf8_decode(const std::string&) in include/indicators/display_width.hpp and single_include\indicators\indicators.hpp the line

std::string curLocale = setlocale(LC_ALL, "");

should save the program locale in curLocale for restoring it later. Instead, it sets the program locale to the shell locale. The line shoud be replaced by the two lines

std::string curLocale = setlocale(LC_ALL, nullptr);
setlocale(LC_ALL, "");

The first one save the program locale and the second one set the shell locale for UTF8 decoding.

bug_locale.patch

@eiriklegernaes
Copy link

Just wanted to add that this bug caused problems when parsing floating point numbers using https://github.com/nlohmann/json. The suggested two-line fix worked great. Thanks @albertocasagrande!

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

No branches or pull requests

2 participants