Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Strip color codes for ascii length calc #1220

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion neofetch
Original file line number Diff line number Diff line change
Expand Up @@ -3450,6 +3450,25 @@ image_backend() {
[[ "$image_backend" != "off" ]] && printf '\e[%sA\e[9999999D' "${lines:-0}"
}

strip_escape_codes() {
local input="${1//\"/\\\"}" output="" i char within_code=0
for ((i=0; i < ${#input}; ++i)); do
char="${input:i:1}"
if (( within_code == 1 )); then
case "${char}" in
[a-zA-Z]) within_code=0 ;;
esac
continue
fi
if [[ "${char}" == $'\e' ]]; then
within_code=1
continue
fi
output+="${char}"
done
eval "$2=\"${output}\""
}

print_ascii() {
if [[ -f "$image_source" && ! "$image_source" =~ (png|jpg|jpeg|jpe|svg|gif) ]]; then
ascii_data="$(< "$image_source")"
Expand All @@ -3464,7 +3483,7 @@ print_ascii() {

# Calculate size of ascii file in line length / line count.
while IFS=$'\n' read -r line; do
line="${line//\\\\/\\}"
strip_escape_codes "${line}" line
((++lines,${#line}>ascii_len)) && ascii_len="${#line}"
done <<< "${ascii_data//\$\{??\}}"

Expand Down