OSDN Git Service

update
[bashlib/bashlib-develop.git] / menu
1 #!/bin/bash -eE
2                   #// Content-Type: text/plain; charset=utf-8
3                   #// -eE オプションは、エラーが起きたら中断します
4                   #// -x オプションは、1行ずつコマンドを表示します
5
6 #// bashlib-prompt is provided under 3-clause BSD license.
7 #// Copyright (C) 2011 Sofrware Design Gallery "Sage Plaisir 21" All Rights Reserved.
8
9 function  Main_func()
10 {
11   local  AppKey="$2"
12   local  obj="g_InputCommandOpt"
13
14   #//=== call InputCommand_func
15   SetAttr_func  $obj  Class  "InputCommandOpt"
16   SetAttr_func  $obj  Lead  "bashlib menu - shorthand prompt"
17
18   SetAttr_as_AssociativeArrayName_func  $obj  CommandReplace  \
19     "1" "Help" \
20     "2" "SearchFile" \
21     "3" "NewSh" \
22     "4" "NewMenu" \
23     "5" "Extract" \
24     "6" "chmod_x" \
25     "7" "Test" \
26   \
27     "Help"  "Help_sth_func" \
28     "SearchFile"  "SearchFile_sth_func" \
29     "NewSh" "NewSh_sth_func" \
30     "NewMenu" "NewMenu_sth_func" \
31     "Extract" "Extract_sth_func" \
32     "chmod_x" "chmod_x_sth_func" \
33     "Test" "Test_sth_func" \
34
35   SetAttr_as_AssociativeArrayName_func  $obj  MenuCaption  \
36     "1" "ヘルプを開く (HTML5+SVG 対応ブラウザ Chrome, Safari で見えます) [Help]" \
37     "2" "ファイルを検索する [SearchFile]" \
38     "3" "新規作成する - bashlib が使えるシェル・スクリプト・ファイル [NewSh]" \
39     "4" "新規作成する - ショートハンド・プロンプトのメニュー [NewMenu]" \
40     "5" "圧縮ファイルを解凍する。ファイル名を一覧する [Extract]" \
41     "6" "拡張子から実行属性を設定/解除する [chmod_x]" \
42     "7" "bashlib の自動テストを実行する [Test]" \
43
44   InputCommand_func  $obj  ""  "$1"  "$AppKey"
45 }
46
47
48 if [ "${BASH_VERSINFO[0]}" -ge "4" ];then
49   declare_AssociativeArrayClass="declare -A"
50 else
51   declare_AssociativeArrayClass="declare"
52 fi
53
54 export  g_AssociativeArrayMaxLength=100
55 $declare_AssociativeArrayClass  g_InputCommandOpt
56
57
58  
59 #//*********************************************************************
60 #// <<< [Help_sth_func] >>> 
61 #//*********************************************************************
62 function  Help_sth_func()
63 {
64   local  abs_path ; GetAbsPath_func  "document/bashlib.html" ; abs_path="$g_Ret"
65   local  commands
66   local  apps
67
68   apps=( "Google Chrome"  "Safari" )
69   GetUsableApplicationsForMac_func  commands  "${apps[@]}"  #//[out] commands
70   if [ "${commands[0]}" != "" ];then
71     echo  ${commands[0]}  "file://$abs_path"
72     eval  ${commands[0]}  "file://$abs_path" &
73     sleep  1
74     return  0
75   fi
76
77   apps=( "google-chrome"  "firefox" )
78   GetUsableCommands_func  commands  "${apps[@]}"  #//[out] commands
79   if [ "${commands[0]}" != "" ];then
80     echo  ${commands[0]}  "file://$abs_path"
81     eval  ${commands[0]}  "file://$abs_path" &
82     sleep  1
83   else
84     echo  "$abs_path"
85   fi
86 }
87
88
89  
90 #//*********************************************************************
91 #// <<< [SearchFile_sth_func] >>> 
92 #//*********************************************************************
93 function  SearchFile_sth_func()
94 {
95   local  search_path
96   local  keyword
97   local  filter
98
99   echo  ""
100   echo  "Enter のみ: $g_StartInPath"
101   InputPath_func  "検索するフォルダーのパス >"  \
102      --ChkFolderExists  --AllowEnterOnly ; search_path="$g_Ret"
103   if [ "$search_path" == "" ];then  search_path="$g_StartInPath"  ;fi
104   StringClass.cutLastOf_method  "$search_path"  "/" ; search_path="$g_Ret"
105
106   echo  ""
107   echo  "Enter のみ: すべてのファイル"
108   Input_func  "ファイル名のフィルター 例 *.txt >" ; filter="$g_Ret"
109
110   echo  ""
111   echo  "Enter のみ: 内容は問わない"
112   Input_func  "キーワード >" ; keyword="$g_Ret"
113
114   echo_line_func
115   echo  "Search $keyword in $search_path/$filter"
116
117   if [ "$keyword" == "" ];then
118     if [ "$filter" == "" ];then
119       find  "$search_path" -print
120     else
121       find  "$search_path" -name "$filter" -print
122     fi
123   else
124     StringEscapeUtilsClass.escapeGrep_method  "$keyword" ; keyword="$g_Ret"
125     if [ "$filter" == "" ];then
126       grep -rni "$keyword" "$search_path"  || :  #// : ignores exit status
127     else
128       grep -rni --include="$filter" "$keyword" "$search_path"  || :  #// : ignores exit status
129     fi
130   fi
131 }
132
133
134  
135 #//*********************************************************************
136 #// <<< [NewSh_sth_func] >>> 
137 #//*********************************************************************
138 function  NewSh_sth_func()
139 {
140   NewShSub_sth_func  "sample/Sample.sh"  "$@"
141 }
142
143
144 function  NewShSub_sth_func()
145 {
146   local  TemplatePath="$1"
147   local  AppKey="$3"
148   local  path
149   local  parent_path
150   local  name
151   local  default_name="a.sh"
152
153   while true; do
154     echo  "Enterのみ : カレントの $default_name"
155     InputPath_func  "新しいファイルのパス >"  --AllowEnterOnly ; path="$g_Ret"
156     if [ "$path" == "" ];then  path="$default_name"  ;fi
157     GetAbsPath_func  "$path"  "$g_StartInPath" ; path="$g_Ret"
158     if [ ! -e "$path" ];then  break  ;fi
159     if [ -d "$path" ];then  path="$path/$default_name" ; break  ;fi
160     echo  "<ERROR msg=\"すでにファイルが存在します。\"/>" >&2
161   done ; done_func $?
162
163   GetParentAbsPath_func  "$path"
164   parent_path="$g_Ret"
165   AppKeyClass.newWritable_method  "$AppKey"  "$parent_path"
166
167   #// make <Sample.sh>
168   mkdir_func  "$parent_path"
169   cp  "$TemplatePath"  "$path"
170
171   echo  "$path を作成しました。"
172
173   #// search scriptlib folder
174   while true ;do
175     if [ -e "$parent_path/scriptlib" ];then  break  ;fi
176     if [ "$parent_path" == "/" ];then  parent_path="" ; break ;fi
177     parent_path=`dirname "$parent_path"`
178   done ; done_func $?
179
180   #// make scriptlib folder
181   if [ "$parent_path" == "" ];then
182     GetParentAbsPath_func  "$path"
183     parent_path="$g_Ret"
184     cp -Rap  "scriptlib"  "$parent_path"
185     echo  "$parent_path/scriptlib フォルダーを作成しました。"
186   fi
187
188   #// guide
189   name=`basename  "$path"`
190   echo  "シェルから、『./$name』と入力すると起動できます。"
191 }
192
193
194  
195 #//*********************************************************************
196 #// <<< [NewMenu_sth_func] >>> 
197 #//*********************************************************************
198 function  NewMenu_sth_func()
199 {
200   NewShSub_sth_func  "sample/Menu.sh"  "$@"
201 }
202
203
204  
205 #//*********************************************************************
206 #// <<< [Extract_sth_func] >>> 
207 #//*********************************************************************
208 function  Extract_sth_func()
209 {
210   local  AppKey="$2"
211   local  package_path
212   local  target_path
213
214   InputPath_func  "圧縮ファイルのパス >"  --ChkFileExists ; package_path="$g_Ret"
215
216   while true ;do
217
218     echo  "Enterのみ : ファイル名を一覧する"
219     echo  "[Ctrl]+[C] : 中断"
220     InputPath_func  "解凍先フォルダーのパス >"  --AllowEnterOnly ; target_path="$g_Ret"
221
222     if [ "$target_path" == "" ];then
223       echo  "$package_path | less"
224       sleep  1
225       ListUpIn_func  "$package_path" | less
226     else
227       AppKeyClass.newWritable_method  "$AppKey"  "$target_path"
228       Extract_func  "$package_path"  "$target_path"
229       break
230     fi
231   done ; done_func $?
232 }
233
234
235  
236 #//*********************************************************************
237 #// <<< [chmod_x_sth_func] >>> 
238 #//*********************************************************************
239 function  chmod_x_sth_func()
240 {
241   local  work_folder
242   local  set_paths
243   local  unset_paths
244   local  defaults
245
246   defaults="$g_StartInPath"
247   echo  ""
248   echo  "Enterのみ : $defaults"
249   InputPath_func  "処理を行うフォルダーのパス >"  --AllowEnterOnly  --ChkFolderExists
250   work_folder="$g_Ret"
251   if [ "$work_folder" == "" ];then  work_folder="$defaults"  ;fi
252
253   defaults="*.sh"
254   echo  ""
255   echo  "Enterのみ : $defaults"
256   Input_func  "実行属性を設定するファイル(\".\"=設定しない, CSV形式)>" ; set_paths="$g_Ret"
257   if [ "$set_paths" == "" ];then  set_paths="$defaults"  ;fi
258
259   defaults="*.txt, *.html, *.svg"
260   echo  ""
261   echo  "Enterのみ : $defaults"
262   Input_func  "実行属性を解除するファイル(\".\"=解除しない, CSV形式)>" ; unset_paths="$g_Ret"
263   if [ "$unset_paths" == "" ];then  unset_paths="$defaults"  ;fi
264
265   chmod_x_func  "$work_folder"  "$set_paths"  "$unset_paths"
266 }
267
268
269  
270 #//*********************************************************************
271 #// <<< [Test_sth_func] >>> 
272 #//*********************************************************************
273 function  Test_sth_func()
274 {
275   if [ -e "test_patch" ];then
276     cp -Rap  "../test"  "."
277     cp -Rap  test_patch/*  "test"
278     rm -r  "test_patch"
279   fi
280
281   test/Test.sh
282 }
283
284
285  
286 #//*********************************************************************
287 #// <<< [T_Error_sth_func] >>> 
288 #//*********************************************************************
289 function  T_Error_sth_func()
290 {
291   Error_func  "エラーメッセージのテスト"
292 }
293
294
295  
296
297
298
299
300
301
302 #//--- start of bashlib include ------------------------------------------------------ 
303
304 #// <<< set up bashlib and call Main_func >>> 
305
306 #// bashlib is provided under 3-clause BSD license.
307 #// Copyright (C) 2011 Sofrware Design Gallery "Sage Plaisir 21" All Rights Reserved.
308
309 g_BashLibPath="scriptlib/bashlib_inc.sh"; g_Ver="1.0"
310 g_StartInPath=`pwd`; cd "`dirname "$BASH_SOURCE"`"; g_Arguments=( "$BASH_SOURCE" "$@" )
311 for (( i = 0; i < 20; i ++ ));do
312   if [ -e "$g_BashLibPath" ];then break ;else  g_BashLibPath="../$g_BashLibPath" ;fi ;done
313 if [ "$i" == "20" ];then  echo "${g_BashLibPath##*../} が見つかりません。
314 bashlib $g_Ver をダウンロードして scriptlib フォルダーをコピーしてください。"; exit 1 ;fi
315 source  "$g_BashLibPath"  #// include
316 CallMain_func
317 #//--- end of bashlib include --------------------------------------------------------
318