Help: minify JSON string

This commit is contained in:
李通洲 2023-11-21 11:03:43 +08:00 committed by 李通洲
parent fa90e01bc5
commit 02bdcd355c
3 changed files with 27 additions and 21 deletions

View File

@ -204,25 +204,31 @@ endif()
# Text data #
#############
function(fastfetch_load_text FILENAME OUTVAR)
file(READ "${FILENAME}" TEMP)
string(REGEX REPLACE "\n$" "" TEMP "${TEMP}") # Remove trailing newline
string(REPLACE "\n" "\\n" TEMP "${TEMP}") # Replace newlines with \n
string(REPLACE "\"" "\\\"" TEMP "${TEMP}") # Replace quotes with \"
string(REPLACE "$\\" "" TEMP "${TEMP}") # Remove $\, so we can unescape some things
set("${OUTVAR}" "${TEMP}" PARENT_SCOPE)
endfunction(fastfetch_load_text)
function(fastfetch_load_raw_text FILENAME OUTVAR)
file(READ "${FILENAME}" TEMP)
string(REGEX REPLACE "\n$" "" TEMP "${TEMP}") # Remove trailing newline
function(fastfetch_encode_c_string STR OUTVAR)
string(REGEX REPLACE "\n$" "" TEMP "${STR}") # Remove trailing newline
string(REPLACE "\\" "\\\\" TEMP "${TEMP}") # Escape backslashes
string(REPLACE "\n" "\\n" TEMP "${TEMP}") # Replace newlines with \n
string(REPLACE "\"" "\\\"" TEMP "${TEMP}") # Replace quotes with \"
set(${OUTVAR} "\"${TEMP}\"" PARENT_SCOPE)
endfunction(fastfetch_load_raw_text)
endfunction(fastfetch_encode_c_string)
fastfetch_load_raw_text(src/data/help.json DATATEXT_JSON_HELP)
function(fastfetch_load_text FILENAME OUTVAR)
file(READ "${FILENAME}" TEMP)
fastfetch_encode_c_string("${TEMP}" TEMP)
set(${OUTVAR} "${TEMP}" PARENT_SCOPE)
endfunction(fastfetch_load_text)
find_package(Python)
if(Python_FOUND)
# Minify JSON string. `io.open(0,encoding='utf-8')` is needed to avoid encoding issues on Windows
execute_process(COMMAND ${Python_EXECUTABLE} -c "import io,json,sys;json.dump(json.load(io.open(0,encoding='utf-8')),sys.stdout,separators=(',', ':'),ensure_ascii=False)"
INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/data/help.json"
OUTPUT_VARIABLE DATATEXT_JSON_HELP)
else()
file(READ "src/data/help.json" TEMP)
endif()
fastfetch_encode_c_string("${DATATEXT_JSON_HELP}" DATATEXT_JSON_HELP)
fastfetch_load_text(src/data/structure.txt DATATEXT_STRUCTURE)
fastfetch_load_text(src/data/help.txt DATATEXT_HELP)
fastfetch_load_text(src/data/help_color.txt DATATEXT_HELP_COLOR)
@ -245,7 +251,7 @@ configure_file(doc/fastfetch.1.in fastfetch.1 @ONLY)
file(GLOB LOGO_FILES "src/logo/ascii/*.txt")
set(LOGO_BUILTIN_H "#pragma once\n\n")
foreach(file ${LOGO_FILES})
fastfetch_load_raw_text("${file}" content)
fastfetch_load_text("${file}" content)
get_filename_component(file "${file}" NAME_WLE)
string(TOUPPER "${file}" file)
string(REGEX REPLACE "\\$\\{c([0-9]+)\\}" "$\\1" content "${content}")

View File

@ -22,7 +22,7 @@ For example to print a fallback for a second value if it is not set, use "{?2}{2
To stop formatting at any point in the format string, use "{-}".
To print something with color, start a placeholder with a '#' and then the linux terminal color encoding.
"\\033[" at the start, and an 'm' at the end is automatically added, so don't do that.
"\033[" at the start, and an 'm' at the end is automatically added, so don't do that.
A "{#}" is equivalent to a "{#0}" and resets everything to normal.
For example to print something pink and underline, use "{#4;35}...{#}".
Information about what the numbers mean can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Sele

View File

@ -4,10 +4,10 @@
#define FASTFETCH_INDLUDED_fastfetch_datatext_h_in
#define FASTFETCH_DATATEXT_JSON_HELP @DATATEXT_JSON_HELP@
#define FASTFETCH_DATATEXT_STRUCTURE "@DATATEXT_STRUCTURE@"
#define FASTFETCH_DATATEXT_HELP "@DATATEXT_HELP@"
#define FASTFETCH_DATATEXT_HELP_COLOR "@DATATEXT_HELP_COLOR@"
#define FASTFETCH_DATATEXT_HELP_FORMAT "@DATATEXT_HELP_FORMAT@"
#define FASTFETCH_DATATEXT_HELP_CONFIG "@DATATEXT_HELP_CONFIG@"
#define FASTFETCH_DATATEXT_STRUCTURE @DATATEXT_STRUCTURE@
#define FASTFETCH_DATATEXT_HELP @DATATEXT_HELP@
#define FASTFETCH_DATATEXT_HELP_COLOR @DATATEXT_HELP_COLOR@
#define FASTFETCH_DATATEXT_HELP_FORMAT @DATATEXT_HELP_FORMAT@
#define FASTFETCH_DATATEXT_HELP_CONFIG @DATATEXT_HELP_CONFIG@
#endif