scripts kernel working
[scripta.git] / scripta~
blobb32a8c5c70ecc240b163cb0e78a5b947e4cd0fbf
1 #!/bin/bash
2 # Copyright 2009, Roberto Previdi
3 # GPL v2 or later
5 ZENITY="zenity --width=400 --height=400"
6 MSGBOX="$ZENITY --info --text "
7 ERROR="$ZENITY --error --text "
8 ENTRY="$ZENITY --entry --text "
9 EDITFILE="$ZENITY --text-info --editable --filename "
10 TMP="/tmp"
11 ROOT_MENU_DIR="menu"
12 SEP="==================="
13 TITLE="scripta"
15 # arg1: relative/absolute path to switch to
16 function change_dir()
18 echo "change_dir($1)"
19 cd $1
20 MENU_DIR=$(pwd)
23 function new_menu()
25 NAME="$($ENTRY "enter the name of the menu" | sed 's/ /_/')"
26 if [ $? -gt 0 ]; then
27 $MSGBOX operation cancelled
28 else
29 mkdir "$NAME"
33 function new_script()
35 NAME="$($ENTRY "enter the name of the script"| sed 's/ /_/')"
36 if [ $? -gt 0 ]; then
37 $MSGBOX operation cancelled
38 else
39 touch "$NAME"
40 # ---> #
41 cat >> "$NAME" << _END
42 function run()
44 \$MSGBOX "edit me!"
46 _END
47 # ---> #
51 #arg1: the file/dir to rename
52 function rename_file()
54 NEW_NAME=$($ZENITY --entry --text "enter new name" --entry-text $1)
55 if [ $? -gt 0 ]; then
56 $MSGBOX operation cancelled
57 else
58 mv $1 $NEW_NAME
62 #arg1: menu to edit
63 function edit_menu()
65 ACTION=$($ZENITY --column "Select action" --list rename remove)
66 if [ $? -gt 0 ]; then
67 $MSGBOX operation cancelled
68 elif [ "$ACTION" == "remove" ]; then
69 PASSWORD=$($ZENITY --entry --text "enter \"DELETE this MENU\"" --entry-text "pleeease, don't kill meeee")
70 if [ $? -gt 0 -o $PASSWORD != "DELETE this MENU"]; then
71 $MSGBOX operation cancelled
72 else
73 rm -rf $1
75 elif [ "$ACTION" == "rename" ]; then
76 rename_file $1
77 fi
80 #arg1: file to edit
81 function edit_file()
83 ACTION=$($ZENITY --column "Select action" --list edit rename remove)
84 if [ $? -gt 0 ]; then
85 $MSGBOX "operation cancelled"
86 elif [ "$ACTION" == "edit" ]; then
87 $EDITFILE $1 > $1.tmp
88 mv -f $1.tmp $1
89 elif [ "$ACTION" == "remove" ]; then
90 PASSWORD="$($ZENITY --entry --text "enter \"DELETE this FILE\"" --entry-text "noooo, save meeee")"
91 if [ $? -gt 0 -o $PASSWORD != "DELETE this FILE"]; then
92 $MSGBOX operation cancelled
93 else
94 rm -rf $1
97 elif [ "$ACTION" == "rename" ]; then
98 rename_file $1
102 #edit the menus in the current path
103 function edit_fn()
105 while [ 1 ]; do
106 TITLE="scripta - EDIT"
107 menu_items --desc-prefix "edit--" back new_submenu new_script quit
109 if [ "$SELECTED" == $SEP ]; then
110 do_nothing
111 elif [ "$SELECTED" == "back" ]; then
112 return 0
113 elif [ "$SELECTED" == "new_submenu" ]; then
114 new_menu
115 elif [ "$SELECTED" == "new_script" ]; then
116 new_script
117 elif [ -d $SELECTED ]; then
118 edit_menu $SELECTED
119 elif [ -f $SELECTED ]; then
120 edit_file $SELECTED
122 done
126 # arg1: bash script which defines a "run" function
127 function execute()
129 function run()
131 $ERROR "Error, the file doesn't define a run() function"
134 source ./$1
140 function do_nothing()
142 DUMMY=$DUMMY
145 # arg1: return value of zenity
146 # arg2: selected command
147 function parse_menu_commons()
149 if [ $1 -gt 0 ]; then
150 echo exiting with status $1
151 exit $1
154 if [ "$2" == "quit" ]; then
155 exit 0
159 #arg1: prefix for the description part
160 function get_items()
162 SUBMENUS=$(
163 find -maxdepth 1 -type d |
164 sort |
165 sed 's/.*\/\([^/]*\)/\1/' |
166 sed 's/[.]//' |
167 sed -e :a -e '$ P; /$/N; s/\n/ /; ta;'|
168 head -n 1 |
169 sed 's/$/ /' |
170 sed "s/\([a-z]\+\) /\1 $1\1... /g"
172 MENU_ITEMS=$(
173 find -maxdepth 1 -type f |
174 sort |
175 sed "s/.*\/\([^/]*\)/\1 $1\1/" |
176 sed -e :a -e '$ P; /$/N; s/\n/ /; ta;'|
177 head -n 1)
180 # arg1-n: additional menu entries
181 function menu_items()
183 cd $MENU_DIR
184 ADDITIONAL=""
185 DESC_PREFIX=""
186 while [ $# -gt 0 ]; do
187 if [ "$1" == "--desc-prefix" ]; then
188 shift
189 DESC_PREFIX="$1"
190 else
191 ADDITIONAL="$ADDITIONAL $1 $1"
193 shift
194 done
196 get_items "$DESC_PREFIX"
198 SELECTED=$($ZENITY --title "$TITLE" --hide-column 1 --column Command --column Operation --list $SUBMENUS $MENU_ITEMS $SEP $SEP $ADDITIONAL)
199 parse_menu_commons $? "$SELECTED"
203 change_dir $ROOT_MENU_DIR
204 while [ 1 ]; do
205 TITLE="scripta"
206 menu_items back edit quit
208 if [ "$SELECTED" == $SEP ]; then
209 do_nothing
210 elif [ "$SELECTED" == "edit" ]; then
211 edit_fn
212 elif [ "$SELECTED" == "back" ]; then
213 change_dir ..
214 elif [ -d $SELECTED ]; then
215 change_dir $SELECTED
216 elif [ -f $SELECTED ]; then
217 execute $SELECTED
219 done