this post was submitted on 09 Oct 2025
33 points (100.0% liked)

Funhole

977 readers
309 users here now

Welcome to Funhole! The first and only hole on SDF exclusively for fun content! Are you a content creator? Then come post your content and join the fun!

founded 2 years ago
MODERATORS
 

Source code


#!/bin/sh



# Setup:
# $ chmod +x painterm

# Usage:
# $ . painterm "/path/to/file"

# Description:
# - Reads an ANSCII picture within a file and converts selected characters to
#   the specified character with optional colour tiles

# Features:
# - Extremely inefficient
# - Option to add randomized colour tiles



# SHELLCHECK
# shellcheck disable=SC2034 # Warning for unused variables
# shellcheck disable=SC2086 # Warning for globbing/word splitting



##### USER VARIABLES
# - FILE LOCATION
#   - Set by first command line script argument
file_location="${1}"
# - RANDOM NUMBER CREATOR INTERVAL
#   - Higher number = faster preloading of randomized numbers
number_creator_interval=1000



##### FUNCTIONS
select_random_colour() { # NOTE Arguments: "${current_random_number}"
# COLOUR SELECTION

    # VARIABLES
    colour="${1}"
    # - COLOURS
    colour0="\033[30m\033[41m"
    colour1="\033[31m\033[42m"
    colour2="\033[32m\033[41m"
    colour3="\033[33m\033[42m"
    colour4="\033[34m\033[42m"
    colour5="\033[35m\033[44m"
    colour6="\033[36m\033[44m"
    colour7="\033[37m\033[44m"
    colour8="\033[44m"
    colour9="\033[33m\033[44m"

    # CODE
    # CHOOSE COLOUR BASED ON CURRENT VARIABLE DIGIT [0-9]
    if [ "${colour}" ]; then
        case "${colour}" in
        0)
            printf "%b" "${colour0}"
        ;;
        1)
            printf "%b" "${colour1}"
        ;;
        2)
            printf "%b" "${colour2}"
        ;;
        3)
            printf "%b" "${colour3}"
        ;;
        4)
            printf "%b" "${colour4}"
        ;;
        5)
            printf "%b" "${colour5}"
        ;;
        6)
            printf "%b" "${colour6}"
        ;;
        7)
            printf "%b" "${colour7}"
        ;;
        8)
            printf "%b" "${colour0}"
        ;;
        9)
        printf "%b" "${colour1}"
        ;;
        esac
    fi
}

initialization() {
# CHECK SCRIPT FOR ERRORS WITH SHELLCHECK (IF AVAILABLE) AND OTHER REQUIREMENTS

    # CHECK IF SHELLCHECK IS INSTALLED
    if [ -n "$(command -v "shellcheck")" ]; then
        # IF INSTALLED, RUN SHELLCHECK AND EXIT IF ERRORS ARE FOUND
        if ! shellcheck painterm; then
            return 1
        fi
    fi

    # CHECK IF FILE LOCATION ARGUMENT IS EMPTY
    if [ -z "${file_location}" ]; then
        # IF EMPTY, DISPLAY ERROR MESSAGE AND EXIT
        printf "Argument required: File location\n"
        return 1
    fi

    # CLEAR SCREEN IF ALL CHECKS PASS
    clear
}

main() { # NOTE Arguments: "<file location>"
# MAIN SCRIPT

    # VARIABLES
    create_random_numbers=
    random_numbers_total=

    # CODE
    # CHECK IF CHARACTER COUNT IN FILE IS MORE THAN 0 CHARACTERS
    while [ -n "$(wc -c ${file_location} | tr -dc '[:digit:]')" ]; do
        # CREATE NEW SET OF RANDOM NUMBERS (VALUE BASED ON VARIABLE "${number_creator_interval}")
        create_random_numbers="$(cat < /dev/urandom | tr -dc '[:digit:]' | fold -w "${number_creator_interval}" | head -n 1)"
        # CHECK IF TOTAL COUNT OF RANDOM NUMBERS ARE LESS THAN FILE CHARACTER COUNT
        if [ "${#random_numbers_total}" -lt "$(wc -c ${file_location} | tr -dc '[:digit:]')" ]; then
            # IF LESS, ADD NEW SET OF RANDOM NUMBERS TO TOTAL
            random_numbers_total="${random_numbers_total}${create_random_numbers}"
            printf "."
        else
            # IF MORE, CLEAR SCREEN & STOP & CONTINUE WITH SCRIPT
            clear
            break
        fi
    done

    # VARIABLES
    file="${1}"
    line_i=0
    line_count="$(cat ${file} | wc -l)"
    char_i=0
    char_count=
    char_new_line="$((char_count - 1))"
    total_count=1
    current_random_number="$(echo "${random_numbers_total}" | cut -c "${total_count}")"

    # FUNCTIONS
    tile_swap() { # NOTE Arguments: "<Character to find>" "<Character replacement>" "<Colour variable/function>"
    # FIND SPECIFIC CHARACTER AND SWAP TO CHOSEN CHARACTER WITH RANDOM COLOUR TILE

        # VARIABLES
        character_search="${1}"
        character_swap="${2}"
        character_colour="${3}"
        reset="\x1b[0m"
        total_count="$((total_count + 1))"
        current_random_number="$(echo "${random_numbers_total}" | cut -c "${total_count}")"

        # CODE
        # REPLACE "${character_search}" CHARACTER WITH COLOURED "${character_swap}" CHARACTER TILE
        if [ "$(sed "${line_i}q;d" "${file}" | cut -c "${char_i}")" = "${character_search}" ]; then
            printf "%b%b%b" "${character_colour}" "$(sed "${line_i}q;d" "${file}" | cut -c "${char_i}" | sed "s/${character_search}/${character_swap}/")" "${reset}"
        fi
    }

    # CODE
    # REPEAT THROUGH NUMBER OF LINES FROM SOURCED FILE
    while [ "${line_i}" -ne "${line_count}" ]; do
        line_i="$((line_i + 1))"
        char_count="$(sed "${line_i}q;d" "${file}" | wc -c)"
        # REPEAT THROUGH NUMBER OF CHARACTERS IN THE CURRENT LINE FROM SOURCED FILE
        while [ "${char_i}" -ne "${char_count}" ]; do
            char_i="$((char_i + 1))"
            # SWAP CHARACTER AND COLOUR TILE
            character_current="$(sed "${line_i}q;d" "${file}" | cut -c "${char_i}")"
            case "${character_current}" in
                "+")
                    # SWAP "+" TO "O" WITH RANDOM COLOURED TILE
                    tile_swap "+" "O" "$(select_random_colour "${current_random_number}")"
                ;;
                "@")
                    # SWAP "@" TO "+" WITH NO COLOUR CHANGE
                    tile_swap "@" "+" ""
                ;;
                *)
                    # UNSPECIFIED CHARACTER: INSERT UNCHANGED CHARACTER TILE
                    printf "%b" "$(sed "${line_i}q;d" "${file}" | cut -c "${char_i}")"
                ;;
            esac
        done
        # INSERT NEW LINE AFTER REACHING LAST CHARACTER IN THE CURRENT LINE FROM SOURCED FILE
        if [ "${char_i}" -ge "${char_new_line}" ]; then
            char_i=0
            printf "\n"
        fi
    done
}



##### START

# CODE #

if ! initialization; then
    return 1
fi

main "${file_location}"

##### FINISH

Drawing


            _________
           |_-_-_-_-_|
           |_________|
            )_______(
           (_________)                                                ++++++++
           |         |  Biodegrade me, Daddy!                      ++++++++++++++
           /         \   /                                       ++++++++++++++++++
          /           \ /                                       ++++++++++++++++++++
         /    0   0    \                                   No.  ++++++++++++++++++++
        /               \                                    \  ++++++  +++  +++++++
       /========0========\                                    \ ++++++++++++++++++++
      /                   \                                     ++++++++++++++++++++
     (_____________________)                                    ++++++++   +++++++++
      )___________________(                                     +++++++ +++ ++++++++
     (_____________________)                                    ++++++++++++++++++++
     |                     |                                    ++++++++++++++++++++
     |_____________________|                                    ++++++++++++++++++++
      )___________________(                                     ++++++++++++++++++++
     |_____________________|                                    ++++++++++++++++++++
      )___________________(                                     ++++++++++++++++++++
     |                     |                                    ++++++++++++++++++++
     |_____________________|                                    ++++++++++++++++++++
      )___________________(                                     ++++++++++++++++++++
     |_____________________|                                    ++++++++++++++++++++
      )___________________(                                     ++++++++++++++++++++
     |                     |                                    ++++++++++++++++++++
     |                     |                                    ++++++++++++++++++++
     |_____________________|                                    ++++++++++++++++++++
     (_____________________)                                    ++++++++++++++++++++
     |_____________________|                                     ++++++++++++++++++
     (_____________________)                                     ++++++++++++++++++
     |                     |                                     ++++++++++++++++++
     |                     |                                      ++++++++++++++++
     \_____________________/                                        |          |
      '-------------------'

Art

you are viewing a single comment's thread
view the rest of the comments
[โ€“] confusedpuppy@lemmy.dbzer0.com 4 points 4 days ago (1 children)

Thank you for publicly disclosing my private medical records!

[โ€“] cathfish@lemmy.world 4 points 3 days ago

you're welcome. glad to help