From: Vincent Le Gallic Date: Sun, 29 Sep 2013 16:57:50 +0000 (+0200) Subject: Petits scripts pour faire joujou avec les couleurs dans le terminal. X-Git-Url: http://gitweb.pimeys.fr/?p=scripts-20-100.git;a=commitdiff_plain;h=e0190cf86f82bc2d8ef71032717b43a3300ace2f Petits scripts pour faire joujou avec les couleurs dans le terminal. --- diff --git a/colors.sh b/colors.sh new file mode 100644 index 0000000..dc23beb --- /dev/null +++ b/colors.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# Définit des variables cool pour utiliser des couleurs. +# À sourcer, évidemment. + +# Reset +No_Color='\e[0m' # Text Reset + +# Regular Colors +Black='\e[0;30m' # Black +Red='\e[0;31m' # Red +Green='\e[0;32m' # Green +Yellow='\e[0;33m' # Yellow +Blue='\e[0;34m' # Blue +Purple='\e[0;35m' # Purple +Cyan='\e[0;36m' # Cyan +White='\e[0;37m' # White + +regular_colors_names=(Black Red Green Yellow Blue Purple Cyan White) + +# Bold +BBlack='\e[1;30m' # Black +BRed='\e[1;31m' # Red +BGreen='\e[1;32m' # Green +BYellow='\e[1;33m' # Yellow +BBlue='\e[1;34m' # Blue +BPurple='\e[1;35m' # Purple +BCyan='\e[1;36m' # Cyan +BWhite='\e[1;37m' # White + +bold_colors_names=(BBlack BRed BGreen BYellow BBlue BPurple BCyan BWhite) + +front_colors_names=(Black Red Green Yellow Blue Purple Cyan White + BBlack BRed BGreen BYellow BBlue BPurple BCyan BWhite) + +# Underline +UBlack='\e[4;30m' # Black +URed='\e[4;31m' # Red +UGreen='\e[4;32m' # Green +UYellow='\e[4;33m' # Yellow +UBlue='\e[4;34m' # Blue +UPurple='\e[4;35m' # Purple +UCyan='\e[4;36m' # Cyan +UWhite='\e[4;37m' # White + +underline_colors_names=(UBlack URed UGreen UYellow UBlue UPurple UCyan UWhite) + +# Background +On_Black='\e[40m' # Black +On_Red='\e[41m' # Red +On_Green='\e[42m' # Green +On_Yellow='\e[43m' # Yellow +On_Blue='\e[44m' # Blue +On_Purple='\e[45m' # Purple +On_Cyan='\e[46m' # Cyan +On_White='\e[47m' # White + +background_colors_names=(On_Black On_Red On_Green On_Yellow On_Blue On_Purple On_Cyan On_White) + +# High Intensity +IBlack='\e[0;90m' # Black +IRed='\e[0;91m' # Red +IGreen='\e[0;92m' # Green +IYellow='\e[0;93m' # Yellow +IBlue='\e[0;94m' # Blue +IPurple='\e[0;95m' # Purple +ICyan='\e[0;96m' # Cyan +IWhite='\e[0;97m' # White + +# Elles sont toutes dans la même liste + +# Bold High Intensity +BIBlack='\e[1;90m' # Black +BIRed='\e[1;91m' # Red +BIGreen='\e[1;92m' # Green +BIYellow='\e[1;93m' # Yellow +BIBlue='\e[1;94m' # Blue +BIPurple='\e[1;95m' # Purple +BICyan='\e[1;96m' # Cyan +BIWhite='\e[1;97m' # White + +high_intensity_colors_names=(IBlack IRed IGreen IYellow IBlue IPurple ICyan IWhite + BIBlack BIRed BIGreen BIYellow BIBlue BIPurple BICyan BIWhite) + +# High Intensity backgrounds +On_IBlack='\e[0;100m' # Black +On_IRed='\e[0;101m' # Red +On_IGreen='\e[0;102m' # Green +On_IYellow='\e[0;103m' # Yellow +On_IBlue='\e[0;104m' # Blue +On_IPurple='\e[0;105m' # Purple +On_ICyan='\e[0;106m' # Cyan +On_IWhite='\e[0;107m' # White + +background_high_intensity_colors_names=(On_IBlack On_IRed On_IGreen On_IYellow On_IBlue On_IPurple On_ICyan On_IWhite) + +Test1="\e[0;38m" +Test2="\e[0;39m" +tests=(Test1 Test2) +all_colors_names=(`echo ${front_colors_names[@]} ${underline_colors_names[@]} ${background_colors_names[@]} ${high_intensity_colors_names[@]} ${background_high_intensity_colors_names[@]} ${all_colors_names[@]} ${tests[@]}`) diff --git a/create_color.py b/create_color.py new file mode 100644 index 0000000..17fc082 --- /dev/null +++ b/create_color.py @@ -0,0 +1,94 @@ +#!/usr/bin/python +# -*- coding:utf8 -*- + +""" Petit script pour générer une chaîne de caractère magique + qui permet de changer la couleur d'affichage du texte dans le shell +""" + +# Les paramètres sont les suivants : +# 1 : le type de sortie prompt ou pas +# si c'est prompt, la sortie est enadrée de \[ et \], sinon non +# 2 : fg ou bg ou both +# 3 : la couleur (black, red, green, yellow, blue, purple, cyan, white) +# 4 et suivants : des paramètres +# liste exhaustive des combinaisons possibles : +# [] +# bold +# underline +# high +# bold underline +# bold underline high #(a prori ça garde que bold) +# underline high +# avec ground = both +# [fg_params] , bg_color [bg_params] + + +import sys +args=sys.argv + + + +dicocolor={"black":"0","Black":"0","noir":"0","Noir":"0","BLACK":"0","NOIR":"0", + "red":"1","Red":"1","rouge":"1","Rouge":"1","RED":"1","ROUGE":"1", + "green":"2","Green":"2","vert":"2","Vert":"2","GREEN":"2","VERT":"2", + "yellow":"3","Yellow":"3","jaune":"3","Jaune":"3","YELLOW":"3","JAUNE":"3", + "blue":"4","Blue":"4","bleu":"4","Bleu":"4","BLUE":"4","BLEU":"4", + "purple":"5","Purple":"5","violet":"5","Violet":"5","PURPLE":"5","VIOLET":"5", + "cyan":"6","Cyan":"6","CYAN":"6", + "white":"7","White":"7","blanc":"7","Blanc":"7","WHITE":"7","BLANC":"7"} + +def isBold(liste): + liste_bold=set(["bold","Bold","BOLD","gras","Gras","GRAS"]) + return not(liste_bold.isdisjoint(set(liste))) + +def isUnderline(liste): + liste_underline=set(["underline","Underline","UNDERLINE","souligne","Souligne","SOULIGNE"]) + return not(liste_underline.isdisjoint(set(liste))) + +def isHigh(liste): + liste_high=set(["high","High","HIGH","high_intensity","High_Intensity","HIGH_INTENSITY"]) + return not(liste_high.isdisjoint(set(liste))) + +def get_fg_color(color,bold,underline,high): + prefix=("4;"*underline + +"1;"*bold) + highoupas="9"*high+"3"*(not(high)) + return prefix+highoupas+dicocolor[color] + +def get_bg_color(color,high): + highoupas="4"*high+"10"*(not(high)) + return highoupas+dicocolor[color] + +# Let's go +out,ground,color=args[1],args[2],args[3] +params=args[4:] # éventuellement vide + +if out=="prompt": + deb,fin="\\[\\e[","m\\]" +else: + deb,fin="\\e[","m" + + + +if ground=="fg": + bold = isBold(params) + underline = isUnderline(params) + high = isHigh(params) + print deb+get_fg_color(color,bold,underline,high)+fin +elif ground=="bg": + high = isHigh(params) + print deb+get_bg_color(color,high)+fin +elif ground=="both": + # On cherche le séparateur + virgule=params.index(",") + fg_params=params[:virgule] + bg_color=params[virgule+1] + bg_params=params[virgule+2:] + out = deb + out += get_fg_color(color,isBold(fg_params),isUnderline(fg_params),isHigh(fg_params)) + out += fin+deb + out += get_bg_color(bg_color,isHigh(bg_params)) + out += fin + print out +else: + print "" diff --git a/test_base_colors.sh b/test_base_colors.sh new file mode 100755 index 0000000..7e4ffbc --- /dev/null +++ b/test_base_colors.sh @@ -0,0 +1,13 @@ +#/bin/bash + +# Script idiot pour tester toutes les combinaisons de couleurs + +source /home/vincent/scripts/colors.sh + + +for coul in "${all_colors_names[@]}" +do + col=${!coul} + aff="$coul : ${col}plop$No_Color" + echo -e $aff +done