notcurses/tools/neofetch-ripper

143 lines
4.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2021-01-10 14:40:19 +00:00
# Shamelessly extract ASCII distro logos from neofetch
# (https://github.com/dylanaraps/neofetch)
# and convert them into C string constants.
# Requires bash for associative arrays, bleh.
2021-01-10 14:40:19 +00:00
# Takes a single argument: path to neofetch binary
set -e
usage () { echo "usage: `basename $0` path-to-neofetch" ; }
2021-02-03 05:43:15 +00:00
[ $# -eq 1 ] || { usage >&2 ; exit 1 ; }
2021-01-10 14:40:19 +00:00
# Obviously, we're very sensitive to the exact contents of the neofetch
# script. We rely on:
#
# ...arbitrary text...
# ^get_distro_ascii() {$
# We set STARTED to non-empty upon hitting "get_distro_ascii()"
STARTED=""
# We set LOGOSTARTED to the line where the current logo starts, and unset it
# upon the logo's termination (discovered via ^EOF$).
LOGOSTARTED=""
LINE=0
LOGOS=0
# don't emit the same key more than once, or we'll get multiple definitions
declare -A KEYSSEEN
2021-01-10 14:40:19 +00:00
while IFS= read -r line ; do
LINE=$((LINE + 1))
if [ -z "$STARTED" ] ; then
if [ "$line" = 'get_distro_ascii() {' ] ; then
STARTED="$LINE"
2021-02-03 05:43:15 +00:00
# FIXME copy the copyright directly from neofetch?
cat << EOHEADER
#include "ncart.h"
#include <stdlib.h>
#include <strings.h>
// Output of "`basename $0` $1"
// Generated on `date -u`
// Copyright Dylan Araps under an MIT License
// The MIT License (MIT)
//
// Copyright (c) 2015-2021 Dylan Araps
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Found get_distro_ascii at line $LINE
EOHEADER
2021-01-10 14:40:19 +00:00
fi
elif [ -z "$NAME" ] ; then
NAME=`echo "$line" | sed -n -e 's/.*"\([^|]*\)"\*\?)$/\1/p'`
if [ -n "$NAME" ] ; then
echo "// Logo #$LOGOS: $NAME..."
NAME=`echo $NAME | sed -e 's/\\\\/\\\\\\\\/g' | tr \ _ | tr / _ | tr -- - _`
if [ -z "${KEYSSEEN[$NAME]}" ] ; then
KEYSSEEN[$NAME]="$LINE"
PRESERVE="$NAME"
else
echo "Warning, found duplicate name $NAME" >&2
fi
elif [ "$line" = "}" ] ; then # are we done with get_distro_ascii()?
DONE="$LINE"
echo "// Closed get_distro_ascii at line $LINE"
break
2021-01-10 14:40:19 +00:00
fi
2021-01-10 15:08:14 +00:00
elif [ -z "$LOGOSTARTED" ]; then
2021-01-10 14:40:19 +00:00
if echo "$line" | grep '^ *read -rd ' > /dev/null ; then
LOGOS=$((LOGOS + 1)) # found the start of a new logo!
LOGOSTARTED="$LINE"
if [ -n "$PRESERVE" ] ; then
echo -n "static const char $NAME[] = \""
fi
2021-01-10 14:40:19 +00:00
fi
2021-01-10 15:08:14 +00:00
elif [ -n "$LOGOSTARTED" ]; then
2021-01-10 14:40:19 +00:00
if [ "$line" = "EOF" ] ; then
if [ -n "$PRESERVE" ] ; then
echo "\";"
echo "// $NAME: $((LINE - LOGOSTARTED)) lines, done at line $LINE."
fi
2021-01-10 14:40:19 +00:00
LOGOSTARTED=""
NAME=""
PRESERVE=""
2021-01-10 14:40:19 +00:00
else
if [ -n "$PRESERVE" ] ; then
2021-01-12 05:33:03 +00:00
# FIXME don't eliminate the color templating once we carry colors
/bin/echo -E -n "$line" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/${c.}//g' && /bin/echo -E -n '\n'
fi
fi
2021-01-10 14:40:19 +00:00
fi
done < "$1"
2021-01-10 21:39:11 +00:00
# if DONE is defined, we ran to completion. yay! emit a table providing
# access to these strings via key comparison.
if [ -n "$DONE" ] ; then
echo
echo "struct neofetch_art { const char* oskey; const char* ncart; };"
echo
echo "const struct neofetch_art ncarts[] = {"
for k in "${!KEYSSEEN[@]}" ; do
echo " { \"$k\", $k },"
done
echo " { \"ubuntu\", \"ubuntu_old\" },"
2021-01-10 21:39:11 +00:00
echo " { NULL, NULL }"
echo "};"
echo
echo "const char* get_neofetch_art(const char* oskey){"
echo " for(const struct neofetch_art* nfa = ncarts ; nfa->oskey ; ++nfa){"
2021-01-12 04:46:56 +00:00
echo " if(strcasecmp(nfa->oskey, oskey) == 0){"
2021-01-10 21:39:11 +00:00
echo " return nfa->ncart;"
echo " }"
echo " }"
echo " return NULL;"
echo "}"
exit 0
fi
2021-01-10 14:40:19 +00:00
# unsurprisingly, this incredibly brittle code has broken. crap!
if [ -z "$STARTED" ] ; then
echo "Failed; never found get_distro_ascii(). Alas..." >&2
else
echo "Failed; never closed out get_distro_ascii(). Hrmm..." >&2
fi
exit 1