Skip to content

Commit 02bdcd3

Browse files
committed
Help: minify JSON string
1 parent fa90e01 commit 02bdcd3

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,31 @@ endif()
204204
# Text data #
205205
#############
206206

207-
function(fastfetch_load_text FILENAME OUTVAR)
208-
file(READ "${FILENAME}" TEMP)
209-
string(REGEX REPLACE "\n$" "" TEMP "${TEMP}") # Remove trailing newline
210-
string(REPLACE "\n" "\\n" TEMP "${TEMP}") # Replace newlines with \n
211-
string(REPLACE "\"" "\\\"" TEMP "${TEMP}") # Replace quotes with \"
212-
string(REPLACE "$\\" "" TEMP "${TEMP}") # Remove $\, so we can unescape some things
213-
set("${OUTVAR}" "${TEMP}" PARENT_SCOPE)
214-
endfunction(fastfetch_load_text)
215-
216-
function(fastfetch_load_raw_text FILENAME OUTVAR)
217-
file(READ "${FILENAME}" TEMP)
218-
string(REGEX REPLACE "\n$" "" TEMP "${TEMP}") # Remove trailing newline
207+
function(fastfetch_encode_c_string STR OUTVAR)
208+
string(REGEX REPLACE "\n$" "" TEMP "${STR}") # Remove trailing newline
219209
string(REPLACE "\\" "\\\\" TEMP "${TEMP}") # Escape backslashes
220210
string(REPLACE "\n" "\\n" TEMP "${TEMP}") # Replace newlines with \n
221211
string(REPLACE "\"" "\\\"" TEMP "${TEMP}") # Replace quotes with \"
222212
set(${OUTVAR} "\"${TEMP}\"" PARENT_SCOPE)
223-
endfunction(fastfetch_load_raw_text)
213+
endfunction(fastfetch_encode_c_string)
214+
215+
function(fastfetch_load_text FILENAME OUTVAR)
216+
file(READ "${FILENAME}" TEMP)
217+
fastfetch_encode_c_string("${TEMP}" TEMP)
218+
set(${OUTVAR} "${TEMP}" PARENT_SCOPE)
219+
endfunction(fastfetch_load_text)
220+
221+
find_package(Python)
222+
if(Python_FOUND)
223+
# Minify JSON string. `io.open(0,encoding='utf-8')` is needed to avoid encoding issues on Windows
224+
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)"
225+
INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/data/help.json"
226+
OUTPUT_VARIABLE DATATEXT_JSON_HELP)
227+
else()
228+
file(READ "src/data/help.json" TEMP)
229+
endif()
224230

225-
fastfetch_load_raw_text(src/data/help.json DATATEXT_JSON_HELP)
231+
fastfetch_encode_c_string("${DATATEXT_JSON_HELP}" DATATEXT_JSON_HELP)
226232
fastfetch_load_text(src/data/structure.txt DATATEXT_STRUCTURE)
227233
fastfetch_load_text(src/data/help.txt DATATEXT_HELP)
228234
fastfetch_load_text(src/data/help_color.txt DATATEXT_HELP_COLOR)
@@ -245,7 +251,7 @@ configure_file(doc/fastfetch.1.in fastfetch.1 @ONLY)
245251
file(GLOB LOGO_FILES "src/logo/ascii/*.txt")
246252
set(LOGO_BUILTIN_H "#pragma once\n\n")
247253
foreach(file ${LOGO_FILES})
248-
fastfetch_load_raw_text("${file}" content)
254+
fastfetch_load_text("${file}" content)
249255
get_filename_component(file "${file}" NAME_WLE)
250256
string(TOUPPER "${file}" file)
251257
string(REGEX REPLACE "\\$\\{c([0-9]+)\\}" "$\\1" content "${content}")

src/data/help_format.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For example to print a fallback for a second value if it is not set, use "{?2}{2
2222
To stop formatting at any point in the format string, use "{-}".
2323

2424
To print something with color, start a placeholder with a '#' and then the linux terminal color encoding.
25-
"\\033[" at the start, and an 'm' at the end is automatically added, so don't do that.
25+
"\033[" at the start, and an 'm' at the end is automatically added, so don't do that.
2626
A "{#}" is equivalent to a "{#0}" and resets everything to normal.
2727
For example to print something pink and underline, use "{#4;35}...{#}".
2828
Information about what the numbers mean can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Sele

src/fastfetch_datatext.h.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#define FASTFETCH_INDLUDED_fastfetch_datatext_h_in
55

66
#define FASTFETCH_DATATEXT_JSON_HELP @DATATEXT_JSON_HELP@
7-
#define FASTFETCH_DATATEXT_STRUCTURE "@DATATEXT_STRUCTURE@"
8-
#define FASTFETCH_DATATEXT_HELP "@DATATEXT_HELP@"
9-
#define FASTFETCH_DATATEXT_HELP_COLOR "@DATATEXT_HELP_COLOR@"
10-
#define FASTFETCH_DATATEXT_HELP_FORMAT "@DATATEXT_HELP_FORMAT@"
11-
#define FASTFETCH_DATATEXT_HELP_CONFIG "@DATATEXT_HELP_CONFIG@"
7+
#define FASTFETCH_DATATEXT_STRUCTURE @DATATEXT_STRUCTURE@
8+
#define FASTFETCH_DATATEXT_HELP @DATATEXT_HELP@
9+
#define FASTFETCH_DATATEXT_HELP_COLOR @DATATEXT_HELP_COLOR@
10+
#define FASTFETCH_DATATEXT_HELP_FORMAT @DATATEXT_HELP_FORMAT@
11+
#define FASTFETCH_DATATEXT_HELP_CONFIG @DATATEXT_HELP_CONFIG@
1212

1313
#endif

0 commit comments

Comments
 (0)