YAD Examples

Several complex YAD scripts

List of scripts:
Urxvt configurator
Frontend for find(1)
System information

Configurator for rxvt-unicode

This script read URxvt settings from Xresources database and write new ones to the ~/.Xresources or separate file specified from the command line

Code:

    1 #! /bin/bash
    2 # -*- mode: sh -*-
    3 
    4 
    5 KEY=$RANDOM
    6 
    7 res1=$(mktemp --tmpdir term-tab1.XXXXXXXX)
    8 res2=$(mktemp --tmpdir term-tab2.XXXXXXXX)
    9 res3=$(mktemp --tmpdir term-tab3.XXXXXXXX)
   10 
   11 out=$(mktemp --tmpdir term-out.XXXXXXXX)
   12 
   13 # cleanup
   14 trap "rm -f $res1 $res2 $res3 $out" EXIT
   15 
   16 export YAD_OPTIONS="--bool-fmt=t --separator='\n' --quoted-output"
   17 
   18 rc_file="${1:-$HOME/.Xresources}"
   19 
   20 # parse rc file
   21 while read ln; do
   22     case $ln in
   23         *allow_bold:*) bold=$(echo ${ln#*:}) ;;
   24         *font:*) font=$(echo ${ln#*:}) ;;
   25         *scrollBar:*) sb=$(echo ${ln#*:}) ;;
   26         *loginShell:*) ls=$(echo ${ln#*:}) ;;
   27         *title:*) title=$(echo ${ln#*:}) ;;
   28         *termName:*) term=$(echo ${ln#*:}) ;;
   29         *geometry:*) geom=$(echo ${ln#*:}) ;;
   30         *foreground:*) fg=$(echo ${ln#*:}) ;;
   31         *background:*) bg=$(echo ${ln#*:}) ;;
   32         *highlightColor:*) hl=$(echo ${ln#*:}) ;;
   33         *highlightTextColor:*) hlt=$(echo ${ln#*:}) ;;
   34         *color0:*) cl0=$(echo ${ln#*:}) ;;
   35         *color1:*) cl1=$(echo ${ln#*:}) ;;
   36         *color2:*) cl2=$(echo ${ln#*:}) ;;
   37         *color3:*) cl3=$(echo ${ln#*:}) ;;
   38         *color4:*) cl4=$(echo ${ln#*:}) ;;
   39         *color5:*) cl5=$(echo ${ln#*:}) ;;
   40         *color6:*) cl6=$(echo ${ln#*:}) ;;
   41         *color7:*) cl7=$(echo ${ln#*:}) ;;
   42         *color8:*) cl8=$(echo ${ln#*:}) ;;
   43         *color9:*) cl9=$(echo ${ln#*:}) ;;
   44         *color10:*) cl10=$(echo ${ln#*:}) ;;
   45         *color11:*) cl11=$(echo ${ln#*:}) ;;
   46         *color12:*) cl12=$(echo ${ln#*:}) ;;
   47         *color13:*) cl13=$(echo ${ln#*:}) ;;
   48         *color14:*) cl14=$(echo ${ln#*:}) ;;
   49         *color15:*) cl15=$(echo ${ln#*:}) ;;
   50         !*) ;; # skip comments
   51         "") ;; # skip empty lines
   52         *) misc+=$(echo "$ln\n") ;;
   53     esac
   54 done < <(xrdb -query | grep -i rxvt)
   55 
   56 width=${geom%%x*}
   57 height=${geom##*x}
   58 fn=$(pfd -p -- "$font")
   59 
   60 echo $font
   61 echo $fn
   62 
   63 # main page
   64 yad --plug=$KEY --tabnum=1 --form \
   65     --field=$"Title:" "${title:-Terminal}" \
   66     --field=$"Width::num" ${width:-80} \
   67     --field=$"Height::num" ${height:-25} \
   68     --field=$"Font::fn" "${fn:-Monospace}" \
   69     --field=$"Term:" "${term:-rxvt-256color}" \
   70     --field=$"Enable login shell:chk" ${ls:-false} \
   71     --field=$"Enable scrollbars:chk" ${sb:-false} \
   72     --field=$"Use bold font:chk" ${bold:-false} \
   73     --field=":lbl" "" \
   74     --field=$"Foreground::clr" ${fg:-#ffffff} \
   75     --field=$"Background::clr" ${bg:-#000000} \
   76     --field=$"Highlight::clr" ${hl:-#0000f0} \
   77     --field=$"Highlight text::clr" ${hlt:-#ffffff} > $res1 &
   78 
   79 # palette page
   80 yad --plug=$KEY --tabnum=2 --form --columns=2 \
   81     --field=$"Black::clr" ${cl0:-#2e3436} \
   82     --field=$"Red::clr" ${cl1:-#cc0000} \
   83     --field=$"Green::clr" ${cl2:-#4e9a06} \
   84     --field=$"Brown::clr" ${cl3:-#c4a000} \
   85     --field=$"Blue::clr" ${cl4:-#3465a4} \
   86     --field=$"Magenta::clr" ${cl5:-#75507b} \
   87     --field=$"Cyan::clr" ${cl6:-#06989a} \
   88     --field=$"Light gray::clr" ${cl7:-#d3d7cf} \
   89     --field=$"Gray::clr" ${cl8:-#555753} \
   90     --field=$"Light red::clr" ${cl9:-#ef2929} \
   91     --field=$"Light green::clr" ${cl10:-#8ae234} \
   92     --field=$"Yellow::clr" ${cl11:-#fce94f} \
   93     --field=$"Light blue::clr" ${cl12:-#729fcf} \
   94     --field=$"Light magenta::clr" ${cl13:-#ad7fa8} \
   95     --field=$"Light cyan::clr" ${cl14:-#34e2e2} \
   96     --field=$"White::clr" ${cl15:-#eeeeec} > $res2 &
   97 
   98 # misc page
   99 echo -e $misc | yad --plug=$KEY --tabnum=3 --text-info --editable > $res3 &
  100 
  101 # main dialog
  102 yad --window-icon=utilities-terminal \
  103     --notebook --key=$KEY --tab=$"Main" --tab=$"Palette" --tab=$"Misc" \
  104     --title=$"Terminal settings" --image=utilities-terminal \
  105     --width=400 --image-on-top --text=$"Terminal settings (URxvt)"
  106 
  107 # recreate rc file
  108 if [[ $? -eq 0 ]]; then
  109     mkdir -p ${rc_file%/*}
  110 
  111     eval TAB1=($(< $res1))
  112     eval TAB2=($(< $res2))
  113 
  114     echo -e "! urxvt settings\n" > $out
  115 
  116     # add main
  117     cat <<EOF >> $out
  118 URxvt.title: ${TAB1[0]}
  119 URxvt.geometry: ${TAB1[1]}x${TAB1[2]}
  120 URxvt.font: $(pfd "${TAB1[3]}")
  121 URxvt.termName: ${TAB1[4]}
  122 URxvt.loginShell: ${TAB1[5]}
  123 URxvt.scrollBar: ${TAB1[6]}
  124 URxvt.allow_bold: ${TAB1[7]}
  125 
  126 URxvt.foreground: ${TAB1[9]}
  127 URxvt.background: ${TAB1[10]}
  128 URxvt.highlightColor: ${TAB1[11]}
  129 URxvt.highlightTextColor: ${TAB1[12]}
  130 EOF
  131     # add palette
  132     echo >> $out
  133     for i in {0..15}; do
  134         echo "URxvt.color$i: ${TAB2[$i]}" >> $out
  135     done
  136     echo >> $out
  137 
  138     # add misc
  139     cat $res3 >> $out
  140     echo >> $out
  141 
  142     # load new settings
  143     #xrdb -merge $out
  144     
  145     if [[ $rc_file == $HOME/.Xresources ]]; then
  146         [[ -e $rc_file ]] && sed -i "/^URxvt.*/d" $rc_file
  147         cat $out >> $rc_file
  148     else
  149         mv -f $out $rc_file
  150     fi    
  151 fi

Screenshot:

Frontend for find(1) command

This script uses find(1) and grep(1) commands for finding files by specified conditions including search of content

Code:

    1 #! /bin/sh
    2 # -*- mode: sh -*-
    3 
    4 export find_cmd='@bash -c "run_find %1 %2 %3 %4 %5"'
    5 
    6 export fts=$(mktemp -u --tmpdir find-ts.XXXXXXXX)
    7 export fpipe=$(mktemp -u --tmpdir find.XXXXXXXX)
    8 mkfifo "$fpipe"
    9 
   10 trap "rm -f $fpipe $fts" EXIT
   11 
   12 fkey=$(($RANDOM * $$))
   13 
   14 function run_find
   15 {
   16     echo "6:@disable@"
   17     if [[ $2 != TRUE ]]; then
   18         ARGS="-name '$1'"
   19     else
   20         ARGS="-regex '$1'"
   21     fi
   22     if [[ -n "$4" ]]; then
   23         dt=$(echo "$4" | awk -F. '{printf "%s-%s-%s", $3, $2, $1}')
   24         touch -d "$dt" $fts
   25         ARGS+=" -newer $fts"
   26     fi
   27     if [[ -n "$5" ]]; then
   28         ARGS+=" -exec grep -q -E '$5' {} \;"
   29     fi
   30     ARGS+=" -printf '%p\n%s\n%M\n%TD %TH:%TM\n%u/%g\n'"
   31     echo -e '\f' >> "$fpipe"
   32     eval find "$3" $ARGS >> "$fpipe"
   33     echo "6:$find_cmd"
   34 }
   35 export -f run_find
   36 
   37 exec 3<> $fpipe
   38 
   39 yad --plug="$fkey" --tabnum=1 --form --field=$"Name" '*' --field=$"Use regex:chk" 'no' \
   40     --field=$"Directory:dir" '' --field=$"Newer than:dt" '' \
   41     --field=$"Content" '' --field="yad-search:fbtn" "$find_cmd" &
   42 
   43 yad --plug="$fkey" --tabnum=2 --list --no-markup --dclick-action="xdg-open '%s'" \
   44     --text $"Search results:" --column=$"Name" --column=$"Size:sz" --column=$"Perms" \
   45     --column=$"Date" --column=$"Owner" --search-column=1 --expand-column=1 <&3 &
   46 
   47 yad --paned --key="$fkey" --button="yad-close:1" --width=700 --height=500 \
   48     --title=$"Find files" --window-icon="system-search"
   49 
   50 exec 3>&-

Screenshot:

Gather and show information about system

This script shows information about system, such as hardware info, loaded kernel modules and so on

Code:

    1 #! /bin/sh
    2 # -*- mode: sh -*-
    3 
    4 YAD_OPTIONS="--window-icon='dialog-information' --name=IxSysinfo"
    5 
    6 KEY=$RANDOM
    7 
    8 function show_mod_info {
    9     TXT="\\n<span face='Monospace'>$(modinfo $1 | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')</span>"
   10     yad --title=$"Module information" --button="yad-close" --width=500 \
   11         --image="application-x-addon" --text="$TXT"
   12 }
   13 export -f show_mod_info
   14 
   15 # CPU tab
   16 lscpu | sed -r "s/:[ ]*/\n/" |\
   17     yad --plug=$KEY --tabnum=1 --image=cpu --text=$"CPU information" \
   18         --list --no-selection --column=$"Parameter" --column=$"Value" &
   19 
   20 # Memory tab
   21 sed -r "s/:[ ]*/\n/" /proc/meminfo |\
   22     yad --plug=$KEY --tabnum=2 --image=memory --text=$"Memory usage information" \
   23         --list --no-selection --column=$"Parameter" --column=$"Value" &
   24 
   25 # Harddrive tab
   26 df -T | tail -n +2 | awk '{printf "%s\n%s\n%s\n%s\n%s\n%s\n", $1,$7, $2, $3, $4, $6}' |\
   27     yad --plug=$KEY --tabnum=3 --image=drive-harddisk --text=$"Disk space usage" \
   28         --list --no-selection --column=$"Device" --column=$"Mountpoint" --column=$"Type" \
   29         --column=$"Total:sz" --column=$"Free:sz" --column=$"Usage:bar" &
   30 
   31 # PCI tab
   32 lspci -vmm | sed 's/\&/\&amp;/g' | grep -E "^(Slot|Class|Vendor|Device|Rev):" | cut -f2 |\
   33     yad --plug=$KEY --tabnum=4 --text=$"PCI bus devices" \
   34         --list --no-selection --column=$"ID" --column=$"Class" \
   35         --column=$"Vendor" --column=$"Device" --column=$"Rev" &
   36 
   37 # Modules tab
   38 awk '{printf "%s\n%s\n%s\n", $1, $3, $4}' /proc/modules | sed "s/[,-]$//" |\
   39     yad --plug=$KEY --tabnum=5 --text=$"Loaded kernel modules" \
   40         --image="application-x-addon" --image-on-top \
   41         --list --dclick-action='bash -c "show_mod_info %s"' \
   42         --column=$"Name" --column=$"Used" --column=$"Depends" &
   43 
   44 # Battery tab
   45 ( acpi -i ; acpi -a ) | sed -r "s/:[ ]*/\n/" | yad --plug=$KEY --tabnum=6 \
   46     --image=battery --text=$"Battery state" --list --no-selection \
   47     --column=$"Device" --column=$"Details" &
   48 
   49 # Sensors tab
   50 SENSORS=($(sensors | grep -E '^[^:]+$'))
   51 sid=1
   52 cid=1
   53 
   54 for s in ${SENSORS[@]}; do
   55     echo -e "s$sid\n<b>$s</b>\n"
   56     sensors -A "$s" | tail -n +2 | while read ln; do
   57         [[ $ln == "" ]] && continue
   58         echo "$cid:s$sid"
   59         echo $ln | sed -r 's/:[ ]+/\n/'
   60         ((cid++))
   61     done
   62     ((sid++))
   63 done | yad --plug=$KEY --tabnum=7 --text=$"Temperature sensors information" \
   64     --list --tree --tree-expanded --no-selection --column=$"Sensor" --column=$"Value" &
   65 
   66 # main dialog
   67 TXT=$"<b>Hardware system information</b>\\n\\n"
   68 TXT+=$"\\tOS: $(lsb_release -ds) on $(hostname)\\n"
   69 TXT+=$"\\tKernel: $(uname -sr)\\n\\n"
   70 TXT+="\\t<i>$(uptime)</i>"
   71 
   72 yad --notebook --width=600 --height=450 --title=$"System info" --text="$TXT" --button="yad-close" \
   73     --key=$KEY --tab=$"CPU" --tab=$"Memory" --tab=$"Disks" --tab=$"PCI" --tab=$"Modules" \
   74     --tab=$"Battery" --tab=$"Sensors" --active-tab=${1:-1}

Screenshot:

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *