Last active
November 10, 2024 16:43
-
-
Save jhyland87/faafd0f86535c7e5e89fc3a6163e4cc1 to your computer and use it in GitHub Desktop.
Prusa sliced gcode diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Set fg color with \e[38;5;${COLOR}m: | |
# echo -e '\e[38;5;17mTest\e[0m' | |
# echo -e '\e[38;5;226mYellow text\e[0m' | |
# Set bg color with \e[48;5;${COLOR}m: | |
# echo -e '\e[48;5;17mBlue background\e[0m' | |
# Change the fg and bg colors: | |
# echo -e '\e[48;5;226m\e[38;5;234mDark text on yellow fg\e[0m' | |
# Execute the function "showcolors" (defined in ~/.bash_functions) to see | |
# a list of all possible colors, as well as their color codes | |
# https://github.com/chalk/ansi-styles/blob/main/index.js#L9-L64 | |
# Just some variables for some common colors | |
# This resets everything (colors and styles) | |
_none="\e[0m" | |
_red="\e[31m" | |
_green="\e[32m" | |
_yellow="\e[33m" | |
_blue="\e[34m" | |
_pink="\e[35m" | |
_cyan="\e[36m" | |
_grey="\e[37m" | |
_orange="\e[38;5;214m" | |
_olivedrab="\e[38;5;64m" | |
_olive="\e[38;5;58m" | |
_dirtyyellow="\e[38;5;142m" | |
_nocolor="\e[39m" | |
_bold="\e[1m" | |
_nobold="\e[22m" | |
_bold="\e[1m" | |
_nobold="\e[22m" | |
_dim="\e[2m" | |
_nodim="\e[22m" | |
_xdim="\e[22m" | |
_italic="\e[3m" | |
_noitalic="\e[23m" | |
_underline="\e[4m" | |
_nounderline="\e[24m" | |
_overline="\e[53m" | |
_nooverline="\e[55m" | |
_inverse="\e[7m" | |
_xinverse="\e[27m" | |
_hide="\e[8m" | |
_xhide="\e[28m" | |
_strike="\e[9m" | |
_xstrike="\e[29m" | |
_xcolr="\e[39m" | |
# Shorter aliases | |
_grn=${_green} | |
_yel=${_yellow} | |
_blu=${_blue} | |
_pnk=${_pink} | |
_cyn=${_cyan} | |
_gry=${_grey} | |
_ora=${_orange} | |
_odg=${_olivedrab} | |
_olv=${_olive} | |
_dyel=${_dirtyyellow} | |
_bld=${_bold} | |
_xbld=${_nobold} | |
_ital=${_italic} | |
_xital=${_noitalic} | |
_undl=${_underline} | |
_xundl=${_nounderline} | |
_strk=${_strike} | |
_xstrk=${_nostrike} | |
function _boldn { | |
printf "%b%s" "${_bold}" "${*}" "${_nobold} \n" | |
} | |
function _boldred { | |
printf "%b%s" "${_bold}${_red}" "${*}" "${_none}${_nobold} \n" | |
} | |
function _boldyellow { | |
printf "%b%s" "${_bold}${_dyel}" "${*}" "${_none}${_nobold} \n" | |
} | |
function _dimn { | |
echo -en "\e[2m${*}\e[0m" | |
} | |
function _italn { | |
echo -en "\e[3m${*}\e[0m" | |
printf "%b%s" "${_ital}" "${*}" "${_nital} \n" | |
} | |
function _uscoren { # Underscore | |
echo -en "\e[4m${*}\e[0m" | |
} | |
function _hln { # highlight (reverse bg/fg) | |
echo -en "\e[7m${*}\e[0m" | |
} | |
function _redn { | |
echo -en "\e[1;31m${*}\e[0m" | |
} | |
function _greenn { | |
echo -en "\e[1;32m${*}\e[0m" | |
} | |
function _yellown { | |
echo -en "\e[1;33m${*}\e[0m" | |
} | |
function _whiten { | |
echo -en "\e[1;37m${*}\e[0m" | |
} | |
function _greyn { | |
echo -en "\e[2;37m${*}\e[0m" | |
} | |
function _bold { | |
_boldn "${*}" | |
echo | |
} | |
function _red { | |
#echo -e "\e[1;31m${*}\e[0m" | |
_redn "${*}" | |
echo | |
} | |
function _green { | |
#echo -e "\e[1;32m${*}\e[0m" | |
_greenn "${*}" | |
echo | |
} | |
function _yellow { | |
#echo -e "\e[1;33m${*}\e[0m" | |
_yellown "${*}" | |
echo | |
} | |
function _white { | |
#echo -e "\e[1;37m${*}\e[0m" | |
_whiten "${*}" | |
echo | |
} | |
function _grey { | |
#echo -e "\e[2;37m${*}\e[0m" | |
_greyn "${*}" | |
echo | |
} | |
function showcolors { | |
#curl -s https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/e50a28ec54188d2413518788de6c6367ffcea4f7/print256colours.sh | bash | |
~/Documents/scripts/bash/print256colours.sh | |
} | |
function _colors { | |
_red "This is _red" | |
_green "This is _green" | |
_yellow "This is _yellow" | |
_white "This is _white" | |
_grey "This is _grey" | |
printf "Only red is %s when using _redn\n" "$(_redn red)" | |
} | |
# printf "Testing %i as \e[38;5;%imFOREGROUND\e[0m and \e[48;5;%imBACKGROUND\e[0m\n" 213 213 213 | |
function print_colour { | |
local colour="$1" contrast | |
contrast=$(contrast_colour "$1") | |
printf "\e[48;5;%sm" "$colour" # Start block of colour | |
printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number | |
printf "\e[0m " # Reset colour | |
printf "\t${_dim}Example:${_none} ${_italic}echo -e '${_none}${_yellow}\\\\e[48;5;${_bold}%s${_none}${_yellow}m${_none}Hello World${_yellow}\\\\e[0m${_none}${_italic}'${_none}\n" "${colour}" | |
echo "\$1: ${1}" | |
echo "colour: ${colour}" | |
echo "contrast: ${contrast}" | |
#printf "\n\nExample: ${_dim}echo -e '${_none}${_yellow}\\\\e[48;5;${_bold}%s${_none}${_yellow}${_yellow}m${_none}${_dim}Hello World${_none}${_yellow}\\\\e[0m${_none}${_dim}'${_none}\n" "${colour}" | |
#printf "\e[48;5;%sm" 16; printf "\e[38;5;%sm%3d" 15 16 ; printf "\e[0m " | |
} | |
mode2header(){ | |
#### For 16 Million colors use \e[0;38;2;R;G;Bm each RGB is {0..255} | |
printf '\e[mR\n' # reset the colors. | |
printf '\n\e[m%59s\n' "Some samples of colors for r;g;b. Each one may be 000..255" | |
printf '\e[m%59s\n' "for the ansi option: \e[0;38;2;r;g;bm or \e[0;48;2;r;g;bm :" | |
} | |
mode2colors(){ | |
# foreground or background (only 3 or 4 are accepted) | |
local fb="$1" | |
[[ $fb != 3 ]] && fb=4 | |
local samples=(0 63 127 191 255) | |
for r in "${samples[@]}"; do | |
for g in "${samples[@]}"; do | |
for b in "${samples[@]}"; do | |
printf '\e[0;%s8;2;%s;%s;%sm%03d;%03d;%03d ' "$fb" "$r" "$g" "$b" "$r" "$g" "$b" | |
done; printf '\e[m\n' | |
done; printf '\e[m' | |
done; printf '\e[mReset\n' | |
} | |
function tputcolors { | |
mode2header | |
mode2colors 3 | |
mode2colors 4 | |
} | |
function contrast_colour { | |
local r g b luminance | |
colour="$1" | |
if (( colour < 16 )); then # Initial 16 ANSI colours | |
(( colour == 0 )) && printf "15" || printf "0" | |
return | |
fi | |
# Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8 | |
if (( colour > 231 )); then # Greyscale ramp | |
(( colour < 244 )) && printf "15" || printf "0" | |
return | |
fi | |
# All other colours: | |
# 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5] | |
# See http://stackoverflow.com/a/27165165/5353461 | |
# r=$(( (colour-16) / 36 )) | |
g=$(( ((colour-16) % 36) / 6 )) | |
# b=$(( (colour-16) % 6 )) | |
# If luminance is bright, print number in black, white otherwise. | |
# Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601 | |
(( g > 2)) && printf "0" || printf "15" | |
return | |
# Uncomment the below for more precise luminance calculations | |
# # Calculate percieved brightness | |
# # See https://www.w3.org/TR/AERT#color-contrast | |
# # and http://www.itu.int/rec/R-REC-BT.601 | |
# # Luminance is in range 0..5000 as each value is 0..5 | |
# luminance=$(( (r * 299) + (g * 587) + (b * 114) )) | |
# (( $luminance > 2500 )) && printf "0" || printf "15" | |
} | |
function bold { | |
printf "%b%s%b" "${_bold}" "${1}" "${_nobold}" | |
} | |
function ital { | |
printf "%b%s%b" "${_italic}" "${1}" "${_noitalic}" | |
} | |
function undrln { | |
printf "%b%s%b" "${_underline}" "${1}" "${_nounderline}" | |
} | |
function biu { | |
printf "%b%s%b" "${_bold}${_italic}${_underline}" "${1}" "${_nounderline}${_noital}${_nobold}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
cwd=$(dirname "${BASH_SOURCE[0]}") | |
. ${cwd}/bash_colors.sh | |
# Simple function to remove any escape characters (eg: color, unicode, etc) | |
function decolor { | |
#sed $'s/\033\[[0-9;]*m//g' | |
if [[ ! -t 0 ]]; then | |
cat - | sed $'s/\033\[[0-9;]*m//g' | |
#_data=$(cat < /dev/stdin) | |
if [[ -n ${_data} ]]; then | |
echo -n "${_data}" | sed $'s/\033\[[0-9;]*m//g' | |
return 0 | |
fi | |
fi | |
if [[ $# -gt 0 ]]; then | |
_data=${@} | |
if [[ -n ${_data} ]]; then | |
echo -n "${_data}" | sed $'s/\033\[[0-9;]*m//g' | |
return 0 | |
fi | |
fi | |
} | |
#printf "\033[1;30m\\\033[%b%i%b;%im %b %s %b : %b %s %b\n" "\033[1;30m" "\033[1;33m" "1" "31" "\033[1;33m" "Should be yellow" "\033[0m" "\033[1;31m" "Hello World" "\033[0m" | |
#hex2rgb 000000 003784 004700 004d9f 006000 0064ba 00791e 007cd6 050508 05050b | |
function hex2rgb { | |
hexValues=$(getinput $*) | |
for hex in ${hexValues}; do | |
hex=${hex#"0x"} | |
hex=${hex#"#"} | |
r=$(printf '0x%0.2s' "$hex") | |
g=$(printf '0x%0.2s' ${hex#??}) | |
b=$(printf '0x%0.2s' ${hex#????}%) | |
result=$(printf '%03d' "$(( (r<75?0:(r-35)/40)*6*6 + | |
(g<75?0:(g-35)/40)*6 + | |
(b<75?0:(b-35)/40) + 16 ))") | |
printf "%0.2s %0.2s %0.2s %-4s \e[48;5;%dm%s\e[0m \n" "$hex" "${hex#??}" "${hex#????}" "${result}" "${result}" "Test" | |
done | |
} | |
function colorscale { | |
for rgb in $@; do | |
#echo "Testing $rgb" | |
r=$(echo "$rgb" | cut -d, -f1) | |
g=$(echo "$rgb" | cut -d, -f2) | |
b=$(echo "$rgb" | cut -d, -f3) | |
#printf "r: %s\n" $r >&2 | |
#printf "g: %s\n" $g >&2 | |
#printf "b: %s\n" $b >&2 | |
rgb2x256 $r $g $b | |
done; | |
} | |
#colorrange 231 0 20 | |
function colorrange { | |
# printf "\x1b[;38;2;255;231;0mTesting %s\x1b[0m\n" "Regular yellow" | |
# printf "\x1b[;38;2;255;231;99999mTesting %s\x1b[0m\n" "Light yellow" | |
color=${1} | |
#colorB=${2} | |
a=${2:-0} | |
b=${3:-255} | |
otherCol=${4:-255} | |
if [[ $a -gt $b ]]; then | |
start=$a | |
end=$b | |
else | |
start=$b | |
end=$a | |
fi | |
test $start -lt 0 && start=0 | |
test $end -gt 255 && end=255 | |
echo "Start: ${start}" | |
echo "End: ${end}" | |
for c in $(seq ${start} ${end}); do | |
# printf "\x1b[;38;2;255;%s;%smTesting %s\x1b[0m\n" 1 70 "a color" | |
#printf "\x1b[;38;2;255;%s;%sm█\x1b[0m" "${color}" "${c}" | |
printf "\x1b[;38;2;%s;%s;%sm█\x1b[0m" "${otherCol}" "${color}" "${c}" | |
# | |
done | |
echo | |
} | |
# Examples: | |
# colorrange 21 10 225 # Red to magenta | |
# colorrange 231 10 225 # Yellow to white | |
# for c in $(seq 1 150); do test $(($c % 5)) -eq 0 && colorrange $c 20 220; done # Generates a continuous range | |
# for c in $(seq 0 255); do test $(($c % 5)) -eq 0 && colorrange $c 0 225; done | |
function colorrange_old { | |
# printf "\x1b[;38;2;255;231;0mTesting %s\x1b[0m\n" "Regular yellow" | |
# printf "\x1b[;38;2;255;231;99999mTesting %s\x1b[0m\n" "Light yellow" | |
color=${1} | |
#colorB=${2} | |
start=${2:-0} | |
end=${3:-$(($start+100))} | |
otherCol=${4:-255} | |
test $end -gt 255 && end=255 | |
# test $(($c % 5)) -eq 0 | |
for c in $(seq ${start} ${end}); do | |
# printf "\x1b[;38;2;255;%s;%smTesting %s\x1b[0m\n" 1 70 "a color" | |
#printf "\x1b[;38;2;255;%s;%sm█\x1b[0m" "${color}" "${c}" | |
printf "\x1b[;38;2;%s;%s;%sm█\x1b[0m" "${otherCol}" "${color}" "${c}" | |
# | |
done | |
echo | |
} | |
export colorrange | |
# rgb2x256 135 255 175 | |
function rgb2x256 { | |
r=${1:-0} | |
g=${2:-0} | |
b=${3:-0} | |
[[ $r -lt 75 ]] && [[ $r -ne 0 ]] && return | |
[[ $g -lt 75 ]] && [[ $g -ne 0 ]] && return | |
[[ $b -lt 75 ]] && [[ $b -ne 0 ]] && return | |
#printf "%2s: %4s (%4s)\n" 'r' $r $(( (r<75?0:(r-35)/40)*6*6)) >&2 | |
#printf "%2s: %4s (%4s)\n" 'g' $g $(( (g<75?0:(g-35)/40)*6 )) >&2 | |
#printf "%2s: %4s (%4s)\n" 'b' $b $(( (b<75?0:(b-35)/40) )) >&2 | |
_r=$(( (r<75?0:(r-35)/40)*6*6 )) | |
_g=$(( (g<75?0:(g-35)/40)*6 )) | |
_b=$(( (b<75?0:(b-35)/40) + 16 )) | |
#echo "_r: ${_r}" | |
#echo "_g: ${_g}" | |
#echo "_b: ${_b}" | |
_rgb=$(( $_r + $_g + $_b )) | |
#echo "r+g+b: ${_rgb}" | |
colorCode=$(printf '%03d' "$(( (r<75?0:(r-35)/40)*6*6 + | |
(g<75?0:(g-35)/40)*6 + | |
(b<75?0:(b-35)/40) + 16 ))") | |
testColor ${colorCode} | |
} | |
function testColor { | |
bg=${1:-16} # Black | |
fg=${2:-15} # grey | |
printf "\e[38;5;%sm\e[48;5;%smrgb(%s,%s,%s) => %s\e[0m\n" "${fg}" "${bg}" $r $g $b "${bg}" | |
} | |
function fromhex { | |
hex=${1#"0x"} | |
hex=${hex#"#"} | |
r=$(printf '0x%0.2s' "$hex") | |
g=$(printf '0x%0.2s' ${hex#??}) | |
b=$(printf '0x%0.2s' ${hex#????}) | |
#printf "%s: %s\n\t%s\n" "r" "${r}" $(( (r<75?0:(r-35)/40)*6*6 )) | |
#printf "%s: %s\n\t%s\n" "g" "${g}" $(( (g<75?0:(g-35)/40)*6 )) | |
#printf "%s: %s\n\t%s\n" "b" "${b}" $(( (b<75?0:(b-35)/40) )) | |
result=$(printf '%03d' "$(( (r<75?0:(r-35)/40)*6*6 + | |
(g<75?0:(g-35)/40)*6 + | |
(b<75?0:(b-35)/40) + 16 ))") | |
printf "\e[30;1m\x1b[48;5;%sm%s\x1b[0m\n" ${result} ${result} | |
} | |
#printf "%b\\\033[%s %b %s %b : %b %s %b\n" "\033[1;30m" "1;33" "\033[1;33m" | |
#printf "%b%s%b%s%b : %b%s%b\n" "\033[1;30m" "\033[1;33m" "\033[0m" "test" "\033[1;30m" "\033[1;30m" "test" "\033[0m" | |
#echo -e "\033[1;33mone\033[0m \033[2;33mtwo\033[0m \033[3;33mthree\033[0m \033[4;33mfour\033[0m \033[5;33mfive\033[0m \033[7;33mseven\033[0m \033[9;33mnine\033[0m \033[10;33mten\033[0m \033[11;33meleven\033[0m" | |
#echo -e "\033[1;33mone \033[2mtwo \033[3mthree\033[0m \033[4;33mfour\033[0m \033[5;33mfive\033[0m \033[7;33mseven\033[0m \033[9;33mnine\033[0m \033[10;33mten\033[0m \033[11;33meleven\033[0m" | |
#printf "%b%s%b : %b%s%b\n" "\033[1;30m" "\033[1;33m" "\033[0m" "\033[1;30m" "test" "\033[0m" | |
function _blah { | |
testString="Hello ${USER}" | |
_none="\033[0m" | |
_red="\033[31m" | |
_gre="\033[32m" | |
_yel="\033[33m" | |
_blu="\033[34m" | |
_pnk="\033[35m" | |
_cyn="\033[36m" | |
_gry="\033[37m" | |
_bld="\033[1m" | |
_dim="\033[2m" | |
_ital="\033[3m" | |
_undr="\033[4m" | |
#echo -e "\033[33;2mThis is\033[0m \033[33;1mYellow\033[0m \033[33;2m color\033[0m" | |
#echo -e "\033[33;2mThis is \033[33;1mYellow \033[33;2m color\033[0m" | |
for style in $(seq 0 8); do | |
for color in $(seq 30 37); do | |
styleinit="\033[${style};${color}m" | |
styleend="\033[0m" | |
# DIM | |
# \033[1;30m test \033[0m | |
# Yellow | |
# \033[1;33m | |
# Red | |
# \033[1;31m test \033[0m | |
# Works | |
#printf "${_dim}\\\033[${_none}${_bld}%i${_none}${_dim};${_none}${_bld}%i${_none}${_dim}m${_none}${_ital} %s ${_none}${_dim}\\\033[0m \t: \033[%i;%im %s${_none}\n" "${style}" "${color}" "${testString}" "${style}" "${color}" "${testString}" | |
# Works - includes echo command demo | |
printf "\033[2;37mecho -e \"${_none}${_dim}\\\033[${_none}${_bld}%i${_none}${_dim};${_none}${_bld}%i${_none}${_dim}m${_none}${_ital} %s ${_none}${_dim}\\\033[0m\033[2;37m\" #${_none} \tOutput: \033[%i;%im%s${_none}\n" "${style}" "${color}" "${testString}" "${style}" "${color}" "${testString}" | |
#printf "\033[2;37mecho -e \"${_none}${_dim}\\\033[${_none}${_bld}%i${_none}${_dim};${_none}${_bld}%i${_none}${_dim}m${_none}${_ital} %s ${_none}${_dim}\\\033[0m\033[2;37m\" \t: \033[%i;%im %s${_none}\n" "${style}" "${color}" "${testString}" "${style}" "${color}" "${testString}" | |
#printf "printf \"%%b %%s %%b\\\\n\" \"\\\033[8;34m\" \"Hello World\" \"\\\033[0m\"\n" | |
#printf "\033[2;37mprintf \"${_none}${_dim}%%b %%s %%b\\\\n${_none}\033[2;37m\" \"${_none}${_bld}\\\033[${style};${color}m\033[2;37m\" \"Hello World\" \"${_none}${_bld}\\\033[0m\033[2;37m\"\n" | |
#\033[1;37mHello jhyland\033[0m | |
#printf "\033[1;30m\\\033[\033[0m%s\033[0m\t %s\033[0m\n" "1;31m" | |
# \033[1;30m\\\033[\033[0m%s\033[0m \t \033[0m | |
# \033[1;30m\\\033[\033[0m%s\033[0m \t %b%s%b \033[0m | |
# \033[1;30m test \033[0m | |
#printf "\033[1;37m%s\033[0m \033[1;30m%s\033[0m %-10s : %b%s%b\n" "${styleinit}" "test" "${styleend}" "${styleinit}" "test" "${styleend}" | |
#printf "\033[1;37m%s\033[0m \033[1;30m%s\033[0m %-10s : %b%s%b\n" "${styleinit}" "test" "${styleend}" "${styleinit}" "test" "${styleend}" | |
#echo -e "This was \033[${style};${color}mstyle ${style} and color ${color}\033[0m \t \\\033\[${style};${color}m"; | |
done | |
done | |
#printf "\\\033[%s %-10s : %b%s%b\n" "8;35m" "test" "\033[7;31m" "test" "\033[0m" | |
} | |
#printf "${_dim}\\\033[${_reg}${_bld}%i${_dim};${_reg}${_bld}%i${_dim}m${_reg}Hello World${_dim}\\\033[0m \t: \033[%i;%im %s${_reg}\n" "7" "31" "7" "31" "Hello World" | |
function _democolor { | |
# Needs to be between 30 and 37 | |
color="${1:?"No integer provided - provide an int between 30 and 37"}" | |
if [[ -z ${color} ]]; then | |
color=$(shuf -i 30-37 -n 1) | |
echo "No color chosen - Randomly selected ${color}" | |
fi | |
declare -a fontstyles=( | |
[0]="normal" | |
[1]="bold" | |
[2]="dim" | |
[3]="italic" | |
[4]="underscore" | |
[5]="idk" | |
[6]="SIX" | |
[7]="highlight" | |
[8]="EIGHT" | |
[9]="strikethrough" | |
[10]="TEN" | |
[11]="ELEVEN" | |
) | |
for n in ${!fontstyles[*]}; do | |
echo -e "Style #${n}: \033[${n};${color}m${fontstyles[${n}]}\033[0m " | |
done | |
} | |
function _instyle { | |
declare -a styles=( | |
["normal"]="\033[0m" | |
["bold"]="\033[1m" | |
["dim"]="\033[2m" | |
["italic"]="\033[3m" | |
["underscore"]="\033[4m" | |
["highlight"]="\033[7m" | |
) | |
echo -e "\e[31;1mTester\e[0m" | |
#style="${1:?'No style provided'}" | |
#str="${2:-'Hello World'}" | |
#endstyle="${3:-\e[0m}" | |
printf "%b%s%b\n" "${1:?'No style provided'}" "${2:-'Hello World'}" "${3:=\e[0m}" | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Examples: | |
# ./compare-gcode-files.sh MINI-Inner_Trans_Cowl_Lining_test_36m_0.20mm_225C_PETG_ENDER3S1PRO.gcode MINI-Inner_Trans_Cowl_Lining_test_39m_0.20mm_235C_PETG_ENDER3S1PRO.gcode | |
# ./compare-gcode-files.sh MINI-Inner_Trans_Cowl_Lining_test_36m_0.20mm_225C_PETG_ENDER3S1PRO.gcode MINI-Inner_Trans_Cowl_Lining_test_39m_0.20mm_235C_PETG_ENDER3S1PRO.gcode | |
# ./compare-gcode-files.sh MINI-Inner_Trans_Cowl_Lining_test_28m_0.20mm_240C_PETG_ENDER3S1PRO.gcode MINI-Inner_Trans_Cowl_Lining_test_39m_0.20mm_235C_PETG_ENDER3S1PRO.gcode | |
cwd=$(dirname "${BASH_SOURCE[0]}") | |
. ${cwd}/bash_functions.sh | |
. ${cwd}/color_functions.sh | |
declare -A display_fmt | |
display_fmt['fn']="${_dirtyyellow}${_ital}%s${_none}" | |
display_fmt['fn_bold']="${_bold}${display_fmt['fn']}" | |
display_fmt['bold']="${_bold}%s${_none}" | |
display_fmt['ital']="${_ital}%s${_none}" | |
display_fmt['dim']="${_dim}%s${_none}" | |
display_fmt['dimint']="${_int}${display_fmt['dim']}" | |
display_fmt['url']="${_undl}${_ital}%s${_none}" | |
display_fmt['int']="${_bold}${_cyan}%s${_none}" | |
display_fmt['success']="${_green}${_bold}%7s${_none}" | |
display_fmt['failed']="${_red}${_bold}%7s${_none}" | |
display_fmt['File_A']="${_blue}${_bold}" | |
display_fmt['File_B']="${_orange}${_bold}" | |
timestamp=$(date +%m.%d.%Y-%s) | |
gcode_file_A="${1:?'No file A provided'}" | |
gcode_file_B="${2:?'No file B provided'}" | |
gcode_diff_file="./gcode_settings_diff.${timestamp}.txt" | |
gcode_file_A=$(echo "${gcode_file_A}" | sed -e 's%^/%%') | |
gcode_file_B=$(echo "${gcode_file_B}" | sed -e 's%^/%%') | |
# Pattern of properties to ignore (filament cost, used, start/end, etc) | |
ignore_cfg_pattern="(filament[ _](cost|used)|(start|end)_(filament_)?gcode|printer_(model|notes)|filament_(multitool|ramming_parameters|spool_weight)|template_custom_gcode|thumbnails(_format)?|toolchange|pause_print_gcode|notes|inherits_cummulative|(layer|color_change)_gcode|gcode_(comments|flavor|label_objects|resolution)|binary_gcode|estimated|bed_(custom|shape)|host_type)" | |
gcode_save_dir="./gcode" | |
# Function to get the line count of a file (outputs just the numerical value, no formatting or filename) | |
function count_lines { | |
[[ -f "${1:?'No file specified to get line count from'}" ]] || (_errf "Unable to locate a file named ${display_fmt["fn_bold"]}" "${1}"; exit 1) | |
wc -l "${1}" | awk '{printf("%s", $1)}' | |
} | |
# Get char width of a string | |
function count_chars { | |
echo -n "${1}" | wc -c | awk '{printf("%s", $1)}' | |
} | |
# Grab the date the file was generated (should be in the first line of the file) | |
function get_gcode_gen_date { | |
head -n 1 "${1}" | grep -Eo 'generated by .* on [0-9]{4}\-[0-9]{2}\-[0-9]{2} at [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]+' | |
} | |
# Get the size of a file (returns bytes or megabytes) | |
function sizeof { | |
local bytes=$(stat -f "%z" "${1}") | |
[[ ${bytes} -lt 1024 ]] && echo -n "${bytes} b" && return | |
local mbytes=$(bc <<< "scale=2;${bytes}/1024") | |
echo -n "${mbytes} mb" | |
} | |
# Parse a gcode file and get all of the slicer settings | |
function get_slicer_settings { | |
local file="${1:?'No gcode file specified to parse'}" | |
local gcode_filename=$(basename "${file}") | |
local gcode_output_filename="${gcode_save_dir}/${gcode_filename}" | |
local outfile="./${gcode_filename}.cfg" | |
printf " %25s... " "Parsing slicer settings" | |
# Use awk to get the lines between (and including) the 'prusaslicer_config' patterns, and | |
# remove the comment character as well as remove the 2 'prusaslicer_config' lines, | |
# Then get rid of the unwanted patterns using grep, and sort the results. | |
awk '/prusaslicer_config = begin/,/prusaslicer_config = end/ { print substr($0, 3) }' "${gcode_output_filename}" | sed -E -e '1d' -e '$ d' | grep -Ev "${ignore_cfg_pattern}" | sort -k1 > "${outfile}" | |
#grep -Eo '^; .* = .*$' "${gcode_file}" | sed -E 's/^; //g' | grep -Ev "${ignore_cfg_pattern}" | sort -k1 > "${outfile}" | |
local _rt=$? | |
if [[ ${_rt} != 0 ]]; then | |
printf "${display_fmt['failed']} ${_ital}grep${_none} failed with exit code ${_bold}%s${_none}\n" "ERROR" "${_rt}" | |
exit 1 | |
fi | |
local _sizeof=$(sizeof "${outfile}") | |
local _linecount=$(count_lines "${outfile}") | |
printf "${display_fmt['success']} ${_bold}${_dim}(%s) %s${_none}${_ital}${_dim} settings found${_none}\n" "SUCCESS" "${_sizeof}" "${_linecount}" | |
} | |
function download_gcodes { | |
local gcode_url_path="${1:?'No filename specified'}" | |
local gcode_filename=$(basename "${gcode_url_path}") | |
local gcode_url="http://192.168.0.96:7125/server/files/gcodes/${gcode_url_path}" | |
local gcode_output_filename="${gcode_save_dir}/${gcode_filename}" | |
#local downloaded_filename=${filename} | |
#printf "%30s:%s\n" "gcode_url_path" "${gcode_url_path}" | |
#printf "%30s:%s\n" "gcode_filename" "${gcode_filename}" | |
#printf "%30s:%s\n" "gcode_url" "${gcode_url}" | |
#printf "%30s:%s\n" "gcode_output_filename" "${gcode_output_filename}" | |
printf " %25s... " "Downloading gcode file" | |
wget "${gcode_url}" --output-document="${gcode_output_filename}" | |
local _rt=$? | |
if [[ ${_rt} != 0 ]]; then | |
printf "${display_fmt['failed']} ${_ital}wget${_none} failed with exit code ${_bold}%s${_none}\n" "ERROR" "${_rt}" | |
exit 1 | |
fi | |
printf "${display_fmt['success']} ${_bold}${_dim}(%s) %s${_none}${_ital}${_dim} lines downloaded${_none}\n" "SUCCESS" "$(sizeof ${gcode_output_filename})" $(count_lines "${gcode_output_filename}") | |
local generated_date=$(get_gcode_gen_date "${gcode_output_filename}") | |
test -n "${generated_date}" && printf "\t${_ital}${_dim}%s${_none}\n" "${generated_date}" | |
} | |
# Look for the largest line in the diff output, and return the character count for that line | |
function get_diff_col_size { | |
awk -F'|' '{l=length($1); if(l > max) max = l}END{ print max}' ${1} | |
} | |
test -d "${gcode_save_dir}" || mkdir "${gcode_save_dir}" | |
printf "\n${_ital}Processing${_none} ${display_fmt["fn_bold"]}...\n" "${gcode_file_A}" | |
download_gcodes "${gcode_file_A}" | |
get_slicer_settings "${gcode_file_A}" | |
printf "\n${_ital}Processing${_none} ${display_fmt["fn_bold"]}...\n" "${gcode_file_B}" | |
download_gcodes "${gcode_file_B}" | |
get_slicer_settings "${gcode_file_B}" | |
#table_width=$(bc<<< $(printf "%s+%s+5" $(count_chars "${gcode_file_A}") $(count_chars "${gcode_file_B}"))) | |
table_width=150 | |
printf "\n${_ital}Comparing files...${_none} " | |
diff -W ${table_width} -ydbwBa --suppress-common-lines "${gcode_file_A}.cfg" "${gcode_file_B}.cfg" > "${gcode_diff_file}" | |
if [[ ! -f "${gcode_diff_file}" ]]; then | |
printf "${display_fmt['failed']} ${_ital}diff${_none} failed (no file created)\n" "ERROR" | |
exit 1 | |
fi | |
printf "${display_fmt['success']} ${_bold}${_dim}%s${_none}${_ital}${_dim} differences found${_none}\n" "SUCCESS" $(count_lines ./gcode_settings_diff.${timestamp}.txt) | |
echo -e "" | |
col_width=75 | |
header_width=$((${col_width}-5)) | |
log_header=$(printf "$%-${col_a}s | %s" "${gcode_file_A}" "${gcode_file_B}") | |
# Insert a header at the top of the diff file. | |
echo -e "${log_header}\n$(cat ${gcode_diff_file})" > "${gcode_diff_file}" | |
printf "${_dirtyyellow}${_bold}%-${col_a}s${_none} | ${_dirtyyellow}${_bold}%s${_none}\n" "${gcode_file_A}" "${gcode_file_B}" | |
# Cleanup | |
rm -f "${gcode_file_A}" "${gcode_file_B}" "${gcode_file_A}.cfg" "${gcode_file_B}.cfg" | |
# Display the results | |
cat "${gcode_diff_file}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment