p-ranav / indicators

Activity Indicators for Modern C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

indicators changes locale

albertocasagrande opened this issue · comments

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