scripta first commit
[scripta.git] / scripta
blob7e0c0b0d3ec6959cd7b794e268243f5bf5b6a8c8
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")"
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")"
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: menu to edit
52 function edit_menu()
54 ACTION=$($ZENITY --column "Select action" --list rename remove)
55 if [ $? -gt 0 ]; then
56 $MSGBOX operation cancelled
57 elif [ "$ACTION" == "remove" ]; then
58 PASSWORD=$($ZENITY --entry --text "enter \"DELETE this MENU\"" --entry-text "pleeease, don't kill meeee")
59 if [ $? -gt 0 -o $PASSWORD != "DELETE this MENU"]; then
60 $MSGBOX operation cancelled
61 else
62 rm -rf $1
64 elif [ "$ACTION" == "rename" ]; then
65 NEW_NAME=$($ENTRY "enter new name" --entry-text $1)
66 if [ $1 -gt 0 ]; then
67 $MSGBOX operation cancelled
68 else
69 mv $1 $NEW_NAME
71 fi
74 #arg1: file to edit
75 function edit_file()
77 ACTION=$($ZENITY --column "Select action" --list edit rename remove)
78 if [ $? -gt 0 ]; then
79 $MSGBOX operation cancelled
80 elif [ "$ACTION" == "edit" ]; then
81 $EDITFILE $1 > $1.tmp
82 mv -f $1.tmp $1
83 elif [ "$ACTION" == "remove" ]; then
84 PASSWORD="$($ZENITY --entry --text "enter \"DELETE this FILE\"" --entry-text "noooo, save meeee")"
85 if [ $? -gt 0 -o $PASSWORD != "DELETE this FILE"]; then
86 $MSGBOX operation cancelled
87 else
88 rm -rf $1
91 elif [ "$ACTION" == "rename" ]; then
92 NEW_NAME=$($ZENITY --entry --text "enter new name" --entry-text $1)
93 if [ $? -gt 0 ]; then
94 $MSGBOX operation cancelled
95 else
96 mv $1 $NEW_NAME
101 #edit the menus in the current path
102 function edit_fn()
104 while [ 1 ]; do
105 TITLE="scripta - EDIT"
106 menu_items --desc-prefix "edit--" back new_submenu new_script quit
108 if [ "$SELECTED" == $SEP ]; then
109 do_nothing
110 elif [ "$SELECTED" == "back" ]; then
111 return 0
112 elif [ "$SELECTED" == "new_submenu" ]; then
113 new_menu
114 elif [ "$SELECTED" == "new_script" ]; then
115 new_script
116 elif [ -d $SELECTED ]; then
117 edit_menu $SELECTED
118 elif [ -f $SELECTED ]; then
119 edit_file $SELECTED
121 done
125 # arg1: bash script which defines a "run" function
126 function execute()
128 function run()
130 $ERROR "Error, the file doesn't define a run() function"
133 . $1
139 function do_nothing()
141 DUMMY=$DUMMY
144 # arg1: return value of zenity
145 # arg2: selected command
146 function parse_menu_commons()
148 if [ $1 -gt 0 ]; then
149 echo exiting with status $1
150 exit $1
153 if [ "$2" == "quit" ]; then
154 exit 0
158 #arg1: prefix for the description part
159 function get_items()
161 SUBMENUS=$(
162 find -maxdepth 1 -type d |
163 sort |
164 sed 's/.*\/\([^/]*\)/\1/' |
165 sed 's/[.]//' |
166 sed -e :a -e '$ P; /$/N; s/\n/ /; ta;'|
167 head -n 1 |
168 sed 's/$/ /' |
169 sed "s/\([a-z]\+\) /\1 $1\1... /g"
171 MENU_ITEMS=$(
172 find -maxdepth 1 -type f |
173 sort |
174 sed "s/.*\/\([^/]*\)/\1 $1\1/" |
175 sed -e :a -e '$ P; /$/N; s/\n/ /; ta;'|
176 head -n 1)
179 # arg1-n: additional menu entries
180 function menu_items()
182 cd $MENU_DIR
183 ADDITIONAL=""
184 DESC_PREFIX=""
185 while [ $# -gt 0 ]; do
186 if [ "$1" == "--desc-prefix" ]; then
187 shift
188 DESC_PREFIX="$1"
189 else
190 ADDITIONAL="$ADDITIONAL $1 $1"
192 shift
193 done
195 get_items "$DESC_PREFIX"
197 SELECTED=$($ZENITY --title "$TITLE" --hide-column 1 --column Command --column Operation --list $SUBMENUS $MENU_ITEMS $SEP $SEP $ADDITIONAL)
198 parse_menu_commons $? "$SELECTED"
202 change_dir $ROOT_MENU_DIR
203 while [ 1 ]; do
204 TITLE="scripta"
205 menu_items back edit quit
207 if [ "$SELECTED" == $SEP ]; then
208 do_nothing
209 elif [ "$SELECTED" == "edit" ]; then
210 edit_fn
211 elif [ "$SELECTED" == "back" ]; then
212 change_dir ..
213 elif [ -d $SELECTED ]; then
214 change_dir $SELECTED
215 elif [ -f $SELECTED ]; then
216 execute $SELECTED
218 done