Skip to content

indicators changes locale #126

Open
Open
@albertocasagrande

Description

@albertocasagrande

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

Activity

eiriklegernaes

eiriklegernaes commented on Jun 27, 2024

@eiriklegernaes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @albertocasagrande@eiriklegernaes

        Issue actions

          indicators changes locale · Issue #126 · p-ranav/indicators