From a3cb6608ceaa6980599bcd6e6a0c5c23da7aa74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 16 Dec 2022 11:21:14 +0800 Subject: [PATCH] Locale: refactor --- CMakeLists.txt | 1 + src/detection/locale/locale.c | 60 +++++++++++++++++++++++++++++++++++ src/detection/locale/locale.h | 10 ++++++ src/modules/locale.c | 60 ++++------------------------------- 4 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 src/detection/locale/locale.c create mode 100644 src/detection/locale/locale.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c733fbe72..e3046731b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/detection/locale/locale.c b/src/detection/locale/locale.c new file mode 100644 index 000000000..6108f7e51 --- /dev/null +++ b/src/detection/locale/locale.c @@ -0,0 +1,60 @@ +#include "detection/locale/locale.h" + +#include "common/properties.h" +#include "common/parsing.h" +#include +#include + +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); +} diff --git a/src/detection/locale/locale.h b/src/detection/locale/locale.h new file mode 100644 index 000000000..6ca84ab6f --- /dev/null +++ b/src/detection/locale/locale.h @@ -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 diff --git a/src/modules/locale.c b/src/modules/locale.c index dc2b2b650..cd8f0cd5f 100644 --- a/src/modules/locale.c +++ b/src/modules/locale.c @@ -1,68 +1,20 @@ #include "fastfetch.h" -#include "common/properties.h" -#include "common/printing.h" #include "common/caching.h" -#include "common/parsing.h" - -#include -#include +#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); }