Locale: refactor

This commit is contained in:
李通洲 2022-12-16 11:21:14 +08:00
parent d601cc823c
commit a3cb6608ce
No known key found for this signature in database
GPG Key ID: 3570F9F0F4410388
4 changed files with 77 additions and 54 deletions

View File

@ -239,6 +239,7 @@ set(LIBFASTFETCH_SRC
src/detection/font/font.c
src/detection/gpu/gpu.c
src/detection/host/host.c
src/detection/locale/locale.c
src/detection/media/media.c
src/detection/memory/memory.c
src/detection/os/os.c

View File

@ -0,0 +1,60 @@
#include "detection/locale/locale.h"
#include "common/properties.h"
#include "common/parsing.h"
#include <stdlib.h>
#include <locale.h>
static void getLocaleFromEnv(FFstrbuf* locale)
{
ffStrbufAppendS(locale, getenv("LANG"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_ALL"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_MESSAGES"));
}
static void getLocaleFromStdFn(FFstrbuf* locale)
{
ffStrbufAppendS(locale, setlocale(LC_ALL, NULL));
#ifdef LC_MESSAGES
if(locale->length > 0)
return;
ffStrbufAppendS(locale, setlocale(LC_MESSAGES, NULL));
#endif
}
void ffDetectLocale(FFstrbuf* result)
{
#if !(defined(__APPLE__) || defined(_WIN32))
//Ubuntu (and deriviates) use a non standard locale file.
//Parse it first, because on distributions where it exists, it takes precedence.
//Otherwise use the standard etc/locale.conf file.
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/default/locale", "LANG =", result);
if(result->length > 0)
return;
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/locale.conf", "LANG =", result);
if(result->length > 0)
return;
#endif
#ifndef _WIN32
getLocaleFromEnv(result);
if(result->length > 0)
return;
#endif
getLocaleFromStdFn(result);
}

View File

@ -0,0 +1,10 @@
#pragma once
#ifndef FF_INCLUDED_detection_locale_locale
#define FF_INCLUDED_detection_locale_locale
#include "fastfetch.h"
void ffDetectLocale(FFstrbuf* result);
#endif

View File

@ -1,68 +1,20 @@
#include "fastfetch.h"
#include "common/properties.h"
#include "common/printing.h"
#include "common/caching.h"
#include "common/parsing.h"
#include <stdlib.h>
#include <locale.h>
#include "common/printing.h"
#include "detection/locale/locale.h"
#define FF_LOCALE_MODULE_NAME "Locale"
#define FF_LOCALE_NUM_FORMAT_ARGS 1
static void getLocaleFromEnv(FFstrbuf* locale)
{
ffStrbufAppendS(locale, getenv("LANG"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_ALL"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_MESSAGES"));
}
static void getLocaleFromCmd(FFstrbuf* locale)
{
ffStrbufAppendS(locale, setlocale(LC_ALL, NULL));
#ifdef LC_MESSAGES
if(locale->length > 0)
return;
ffStrbufAppendS(locale, setlocale(LC_MESSAGES, NULL));
#endif
}
void ffPrintLocale(FFinstance* instance)
{
if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS))
if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS))
return;
FFstrbuf locale;
FFstrbuf locale;
ffStrbufInit(&locale);
//Ubuntu (and deriviates) use a non standard locale file.
//Parse it first, because on distributions where it exists, it takes precedence.
//Otherwise use the standard etc/locale.conf file.
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/default/locale", "LANG =", &locale);
if(locale.length == 0)
{
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/locale.conf", "LANG =", &locale);
}
if(locale.length == 0)
{
getLocaleFromEnv(&locale);
}
if(locale.length == 0)
{
getLocaleFromCmd(&locale);
}
ffDetectLocale(&locale);
if(locale.length == 0)
{
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, "No locale found");
@ -73,5 +25,5 @@ void ffPrintLocale(FFinstance* instance)
{FF_FORMAT_ARG_TYPE_STRBUF, &locale}
});
ffStrbufDestroy(&locale);
ffStrbufDestroy(&locale);
}