p-ranav / indicators

Activity Indicators for Modern C++
MIT License
3.04k stars 237 forks source link

indicators changes locale #126

Open albertocasagrande opened 1 year ago

albertocasagrande commented 1 year ago

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 commented 4 months ago

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!