ConsolePicocalcの補完機能修正、Contextの切り出し
@@ -18,7 +18,7 @@ | ||
18 | 18 | <property name="exeDir" value="exe"/> |
19 | 19 | <property name="libDir" value="lib"/> |
20 | 20 | |
21 | - <property name="jpicosheetJar" value="C:/Users/nishikawa.MEGATRADE/Apps/Eclipse-workspace-private/consolePicocalc/jarfile/JPicosheet-0.1.4.jar"/> | |
21 | + <property name="jpicosheetJar" value="C:\Users\Yusuke\Develop\Java\PicoSheet-Workspace\JPicosheet\jarfile\JPicosheet-0.1.4.jar"/> | |
22 | 22 | <!-- <property name="jpicosheetJar" value="/home/yusuke/Develop/Eclipse-workspaces/PicoSheet-Workspace/JPicosheet/jarfile/JPicosheet-0.1.4.jar"/ --> |
23 | 23 | |
24 | 24 | <target name="all" depends="init,makedir,copyJarFile,copylib,jar,makeExe,clean" /> |
@@ -1,11 +1,11 @@ | ||
1 | 1 | echo off |
2 | 2 | |
3 | 3 | set EXEDIR=%~dp0 |
4 | -set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.9.jar | |
4 | +set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.14.2.jar | |
5 | 5 | |
6 | 6 | echo %CLASSPATH% |
7 | 7 | |
8 | -java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc | |
8 | +java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc %1 | |
9 | 9 | rem java -classpath %CLASSPATH% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc |
10 | 10 | |
11 | 11 | pause |
@@ -1,11 +1,15 @@ | ||
1 | 1 | echo off |
2 | 2 | |
3 | 3 | set EXEDIR=%~dp0 |
4 | -set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.9.jar | |
4 | +set CLASSPATH=%EXEDIR%\ConsolePicocalc-0.1.jar;%EXEDIR%\JPicosheet-0.1.4.jar;%EXEDIR%\jline-2.14.2.jar | |
5 | 5 | |
6 | 6 | echo %CLASSPATH% |
7 | 7 | |
8 | +set SYSPROP=-Djline.internal.Log.trace=true | |
9 | + | |
10 | +echo %SYSPROP% | |
11 | + | |
8 | 12 | rem java -classpath %CLASSPATH% com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc |
9 | -java -classpath %CLASSPATH% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc | |
13 | +java -classpath %CLASSPATH% %SYSPROP% -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y com.nissy_ki_chi.ConsolePicocalc.ConsolePicocalc %1 | |
10 | 14 | |
11 | 15 | pause |
@@ -8,7 +8,7 @@ | ||
8 | 8 | {".help", "doHelp", ".help\nヘルプを表示します。"}, |
9 | 9 | |
10 | 10 | {".history", "doHistory", ".history\nコマンド履歴を表示します。"}, |
11 | - {".!", "doExecHistory", ".! <[履歴番号]>\n指定した履歴番号の履歴を再実行します。"}, | |
11 | +/* {".!", "doExecHistory", ".! <[履歴番号]>\n指定した履歴番号の履歴を再実行します。"},*/ | |
12 | 12 | |
13 | 13 | {".book.add", "doBookAdd", ".book.add <Book名>\n新しいブックを追加します。"}, |
14 | 14 | {".book.current", "doBookCurrent", ".book.current <Book名>\nカレントブックを変更します。"}, |
@@ -16,12 +16,14 @@ | ||
16 | 16 | |
17 | 17 | @Override |
18 | 18 | public int complete(String buffer, int cursor, List<CharSequence> candidates) { |
19 | + int completed = -1; | |
19 | 20 | for (String cellName: _sheet.getCellNames()) { |
20 | 21 | if (cellName.startsWith(buffer)) { |
21 | 22 | candidates.add(cellName); |
23 | + completed = 0; | |
22 | 24 | } |
23 | 25 | } |
24 | - return 0; | |
26 | + return completed; | |
25 | 27 | } |
26 | 28 | |
27 | 29 | } |
@@ -0,0 +1,28 @@ | ||
1 | +package com.nissy_ki_chi.ConsolePicocalc; | |
2 | + | |
3 | +import java.util.List; | |
4 | + | |
5 | +import jline.console.completer.Completer; | |
6 | + | |
7 | + | |
8 | +public class CompleterOfCommands implements Completer { | |
9 | + | |
10 | + String[] _commandNames = null; | |
11 | + | |
12 | + public CompleterOfCommands(CommandFragment commands) { | |
13 | + _commandNames = commands.getCommandNameList(); | |
14 | + } | |
15 | + | |
16 | + @Override | |
17 | + public int complete(String buffer, int cursor, List<CharSequence> candidates) { | |
18 | + int completed = -1; | |
19 | + for (String commandName: _commandNames) { | |
20 | + if (commandName.startsWith(buffer)) { | |
21 | + candidates.add(commandName); | |
22 | + completed = 0; | |
23 | + } | |
24 | + } | |
25 | + return completed; | |
26 | + } | |
27 | + | |
28 | +} |
@@ -7,7 +7,6 @@ | ||
7 | 7 | import java.io.IOException; |
8 | 8 | import java.io.InputStreamReader; |
9 | 9 | import java.io.UnsupportedEncodingException; |
10 | -import java.lang.reflect.Array; | |
11 | 10 | import java.lang.reflect.InvocationTargetException; |
12 | 11 | import java.lang.reflect.Method; |
13 | 12 | import java.nio.file.Files; |
@@ -14,16 +13,16 @@ | ||
14 | 13 | import java.nio.file.Paths; |
15 | 14 | import java.util.ArrayList; |
16 | 15 | import java.util.Arrays; |
17 | -import java.util.Collections; | |
18 | 16 | import java.util.HashMap; |
19 | 17 | import java.util.LinkedList; |
20 | 18 | import java.util.List; |
21 | -import java.util.Map; | |
22 | -import java.util.SortedSet; | |
23 | 19 | import java.util.TreeSet; |
20 | +import java.util.regex.Pattern; | |
24 | 21 | |
25 | 22 | import com.nissy_ki_chi.jpicosheet.core.Book; |
23 | +import com.nissy_ki_chi.jpicosheet.core.Cell; | |
26 | 24 | import com.nissy_ki_chi.jpicosheet.core.ConcurrentBookWrapper; |
25 | +import com.nissy_ki_chi.jpicosheet.core.Group; | |
27 | 26 | import com.nissy_ki_chi.jpicosheet.core.Sheet; |
28 | 27 | import com.nissy_ki_chi.jpicosheet.util.SimpleReader; |
29 | 28 | import com.nissy_ki_chi.jpicosheet.util.SimpleWriter; |
@@ -32,27 +31,15 @@ | ||
32 | 31 | import jline.console.completer.ArgumentCompleter; |
33 | 32 | import jline.console.completer.ArgumentCompleter.ArgumentDelimiter; |
34 | 33 | import jline.console.completer.ArgumentCompleter.ArgumentList; |
35 | -import jline.console.completer.Completer; | |
36 | -import jline.console.completer.StringsCompleter; | |
34 | +import jline.console.history.History; | |
35 | +import jline.console.history.History.Entry; | |
37 | 36 | |
38 | -public class ConsolePicocalc { | |
39 | 37 | |
40 | 38 | |
41 | - Map<String, Book> _books = new HashMap<String, Book>(); | |
42 | - String _currentBookName = null; | |
39 | +public class ConsolePicocalc { | |
43 | 40 | |
44 | - BookCommand _bookCommand = new BookCommand(); | |
45 | - SheetCommand _sheetCommand = new SheetCommand(); | |
46 | - CellCommand _cellCommand = new CellCommand(); | |
47 | - GroupCommand _groupCommand = new GroupCommand(); | |
48 | - TableCommand _tableCommand = new TableCommand(); | |
49 | - | |
50 | - LinkedList<String> _cmdHistory = new LinkedList<>(); | |
51 | - LinkedList<String> _aggregatedHistory = new LinkedList<>(); | |
52 | - String _nextCommand = null; | |
41 | + Context ctx = null; | |
53 | 42 | |
54 | - CommandFragment _commandStorage = new CommandFragment("."); | |
55 | - | |
56 | 43 | private static String DEFAULT_BOOK_NAME = "myBook"; |
57 | 44 | private static String DEFAULT_SHEET_NAME = "Sheet1"; |
58 | 45 |
@@ -82,8 +69,28 @@ | ||
82 | 69 | |
83 | 70 | |
84 | 71 | private void setup() { |
72 | + | |
73 | + ctx = new Context(); | |
74 | + ctx.setIn(System.in); | |
75 | + ctx.setOut(System.out); | |
85 | 76 | |
77 | + ctx.set_currentBookName(DEFAULT_BOOK_NAME); | |
78 | + ctx.set_books(new HashMap<String, Book>()); | |
79 | + ctx.get_books().put(ctx.get_currentBookName(), new Book(ctx.get_currentBookName())); | |
80 | + ctx.set_bookCommand(new BookCommand()); | |
81 | + ctx.set_sheetCommand(new SheetCommand()); | |
82 | + ctx.set_cellCommand(new CellCommand()); | |
83 | + ctx.set_groupCommand(new GroupCommand()); | |
84 | + ctx.set_tableCommand(new TableCommand()); | |
85 | + ctx.set_nextCommand(null); | |
86 | + | |
87 | + ctx.set_cmdHistory(new LinkedList<>()); | |
88 | + ctx.set_aggregatedHistory(new LinkedList<>()); | |
89 | + | |
90 | + CommandFragment _commandStorage = new CommandFragment("."); | |
86 | 91 | makeCommandStorage(_commandStorage); |
92 | + ctx.set_commandStorage(_commandStorage); | |
93 | + | |
87 | 94 | } |
88 | 95 | |
89 | 96 | private void makeCommandStorage(CommandFragment commandStorage) { |
@@ -98,19 +105,19 @@ | ||
98 | 105 | |
99 | 106 | public void start(String filePath) { |
100 | 107 | |
101 | - Book book = null; | |
108 | +// Book book = null; | |
102 | 109 | // 引数としてファイルが渡された場合はそれをロードする |
103 | 110 | if (filePath != null && Files.exists(Paths.get(filePath))) { |
104 | - _currentBookName = DEFAULT_BOOK_NAME; | |
105 | - book = new Book(_currentBookName); | |
106 | - _bookCommand.load(book, filePath); | |
111 | +// _currentBookName = DEFAULT_BOOK_NAME; | |
112 | +// book = new Book(_currentBookName); | |
113 | + ctx.get_bookCommand().load(ctx.getCurrentBook(), filePath); | |
107 | 114 | } else { |
108 | 115 | // そうでない場合はデフォルトシートを作成 |
109 | - book = new Book(DEFAULT_BOOK_NAME); | |
110 | - book.addSheet(DEFAULT_SHEET_NAME); | |
111 | - this._currentBookName = book.getName(); | |
116 | +// book = new Book(DEFAULT_BOOK_NAME); | |
117 | + ctx.getCurrentBook().addSheet(DEFAULT_SHEET_NAME); | |
118 | + ctx.set_currentBookName(ctx.getCurrentBook().getName()); | |
112 | 119 | } |
113 | - this._books.put(book.getName(), book); | |
120 | +// this._books.put(book.getName(), book); | |
114 | 121 | |
115 | 122 | // コンソールから1行づつ読み込んで処理する |
116 | 123 | InputStreamReader isr = null; |
@@ -117,10 +124,11 @@ | ||
117 | 124 | // BufferedReader br = null; |
118 | 125 | ConsoleReader consoleReader = null; |
119 | 126 | try { |
120 | - consoleReader = new ConsoleReader(); | |
127 | +// consoleReader = new ConsoleReader(); | |
128 | + ctx.setConsole(new ConsoleReader()); | |
121 | 129 | // List<Completer> completers = new LinkedList<>(); |
122 | 130 | |
123 | - String[] commands = this._commandStorage.getCommandNameList(); | |
131 | + String[] commands = ctx.get_commandStorage().getCommandNameList(); | |
124 | 132 | |
125 | 133 | ArgumentDelimiter ad = new ArgumentDelimiter() { |
126 | 134 |
@@ -131,10 +139,47 @@ | ||
131 | 139 | |
132 | 140 | @Override |
133 | 141 | public ArgumentList delimit(CharSequence arg0, int arg1) { |
134 | - String[] arguments = arg0.toString().split("\\s"); | |
135 | - ArgumentList al = new ArgumentList( new String[] {"appl", "bppl"}, 0, 0, 2); | |
136 | -// ArgumentList al = new ArgumentList(arguments, arguments.length-1, 0, arg0.length()); | |
137 | -// ArgumentList al = new ArgumentList(arguments, 0, arg0.length()-1, arg0.length()-1); | |
142 | + | |
143 | + int cursorArgumentIndex = -1; | |
144 | + int argumentPosition = -1; | |
145 | + int bufferPosition = arg1; | |
146 | + ArrayList<String> list = new ArrayList<>(); | |
147 | + StringBuilder sb = new StringBuilder(); | |
148 | + for (int i = 0; i < arg0.length(); i++) { | |
149 | + if (!isDelimiter(arg0, i)) { | |
150 | + sb.append(arg0.charAt(i)); | |
151 | + if (i == arg1-1) { | |
152 | + cursorArgumentIndex = list.size()-1; | |
153 | + } | |
154 | + } else { | |
155 | + list.add(sb.toString()); | |
156 | + sb.setLength(0); | |
157 | + if (i == arg1-1) { | |
158 | + argumentPosition = sb.length(); | |
159 | + } | |
160 | + } | |
161 | + } | |
162 | + if (sb.length() > 0) { | |
163 | + list.add(sb.toString()); | |
164 | + argumentPosition = sb.length(); | |
165 | + } | |
166 | + | |
167 | + if (cursorArgumentIndex == -1) { | |
168 | + cursorArgumentIndex = 0; | |
169 | + } | |
170 | + if (argumentPosition == -1) { | |
171 | + argumentPosition = 0; | |
172 | + } | |
173 | + ArgumentList al = new ArgumentList(list.toArray(new String[]{}), cursorArgumentIndex, argumentPosition, bufferPosition); | |
174 | + | |
175 | + | |
176 | +// Pattern p = Pattern.compile("(([^\\s]+)((\\s+)|$)+)"); | |
177 | +// | |
178 | +// String[] arguments = arg0.toString().split("\\s+"); | |
179 | +// int posOfArguent = 0; | |
180 | +// int posOfBuffer = arg1; | |
181 | +// int posOfWord = arguments[arguments.length -1].length() -1; | |
182 | +// ArgumentList al = new ArgumentList(arguments, arguments.length-1, arg0.length(), arg0.length()); | |
138 | 183 | return al; |
139 | 184 | } |
140 | 185 | }; |
@@ -142,9 +187,15 @@ | ||
142 | 187 | // ArgumentCompleter ac = new ArgumentCompleter(ad, sc); |
143 | 188 | // consoleReader.addCompleter(ac); |
144 | 189 | // consoleReader.addCompleter(new ConPicoCompleter()); |
145 | - CompleterOfCellNames cocn = new CompleterOfCellNames(_books.get(_currentBookName).getResolver().getCurrentSheet()); | |
146 | - ArgumentCompleter ac = new ArgumentCompleter(ad, cocn); | |
147 | - consoleReader.addCompleter(ac); | |
190 | + Book book = ctx.getCurrentBook(); | |
191 | + CompleterOfCellNames cocn = new CompleterOfCellNames(book.getResolver().getCurrentSheet()); | |
192 | +// ArgumentCompleter ac = new ArgumentCompleter(ad, cocn); | |
193 | +// consoleReader.addCompleter(ac); | |
194 | +// ctx.getConsole().addCompleter(ac); | |
195 | + | |
196 | + CompleterOfCommands cocmd = new CompleterOfCommands(ctx.get_commandStorage()); | |
197 | + ArgumentCompleter ac2 = new ArgumentCompleter(ad, cocn, cocmd); | |
198 | + ctx.getConsole().addCompleter(ac2); | |
148 | 199 | |
149 | 200 | } catch (IOException e2) { |
150 | 201 | // TODO 自動生成された catch ブロック |
@@ -167,11 +218,11 @@ | ||
167 | 218 | while(true) { |
168 | 219 | try { |
169 | 220 | |
170 | - Book currentBook = _books.get(_currentBookName); | |
221 | + Book currentBook = ctx.getCurrentBook(); | |
171 | 222 | // consoleReader.readLine(makePrompt(currentBook)); |
172 | 223 | |
173 | 224 | // ヒストリからの実行か? |
174 | - if (_nextCommand == null) { | |
225 | + if (ctx.get_nextCommand() == null) { | |
175 | 226 | // ヒストリではない。 |
176 | 227 | // 入力がなくなったら終了 |
177 | 228 |
@@ -180,57 +231,15 @@ | ||
180 | 231 | int p = 0; |
181 | 232 | int cellSearchedPos = 0; |
182 | 233 | |
183 | -// while(true) { | |
184 | -//// ch = (char) br.read(); | |
185 | -// // TODO: リターンキーを押さなくてもキーを入力できるようにする | |
186 | -//// ch = (char) isr.read(); | |
187 | -// ch = (char) System.in.read(); | |
188 | -// if (ch == '\r') { | |
189 | -// if ((char) isr.read() == '\n') { | |
190 | -// line = linebuf.toString(); | |
191 | -// break; | |
192 | -// } | |
193 | -// } | |
194 | -// if (ch != '\t') { | |
195 | -// linebuf.append(ch); | |
196 | -// // 補完機能関連の変数をリセット | |
197 | -// candiMode = false; | |
198 | -// cellSearchedPos = 0; | |
199 | -// cellNames = null; | |
200 | -// } else { | |
201 | -// if (!candiMode) { | |
202 | -// candiMode = true; | |
203 | -// // 候補文字列の取得 | |
204 | -// candidate = linebuf.substring(p); | |
205 | -// // カレントシートのセル名リストが無ければ作成 | |
206 | -// if (cellNames == null) { | |
207 | -// cellNames = new ArrayList<>(new TreeSet<>(_books.get(_currentBookName).getResolver().getCurrentSheet().getCellNames())); | |
208 | -// cellNamesPos = 0; | |
209 | -// } | |
210 | -// } | |
211 | -// // セル名リストの中でマッチするセル名を探す | |
212 | -// for (; cellSearchedPos < cellNames.size(); cellSearchedPos++) { | |
213 | -// String cellName = cellNames.get(cellSearchedPos); | |
214 | -// if (cellName.indexOf(candidate) == 0) { | |
215 | -// linebuf.replace(p, linebuf.length(), cellName); | |
216 | -// } | |
217 | -// } | |
218 | -// line = linebuf.toString(); | |
219 | -// } | |
220 | -// System.out.println(line); | |
221 | -// } | |
222 | - | |
223 | -// if ((line = br.readLine().trim()) == null) { | |
224 | -// break; | |
225 | -// } | |
226 | 234 | |
227 | - if ((line = consoleReader.readLine(makePrompt(currentBook))) == null ) { | |
235 | +// if ((line = consoleReader.readLine(makePrompt(currentBook))) == null ) { | |
236 | + if ((line = ctx.getConsole().readLine(makePrompt(currentBook))) == null ) { | |
228 | 237 | break; |
229 | 238 | } |
230 | 239 | } else { |
231 | 240 | // ヒストリからの実行 |
232 | - line = _nextCommand; | |
233 | - _nextCommand = null; | |
241 | + line = ctx.get_nextCommand(); | |
242 | + ctx.set_nextCommand(null); | |
234 | 243 | } |
235 | 244 | |
236 | 245 | // 未入力だったら何もしない |
@@ -244,10 +253,10 @@ | ||
244 | 253 | } |
245 | 254 | |
246 | 255 | // ヒストリからの実行でない場合、入力内容を履歴に保持 |
247 | - if (_nextCommand == null) { | |
248 | - _cmdHistory.add(line); | |
249 | - _aggregatedHistory.remove(line); | |
250 | - _aggregatedHistory.add(line); | |
256 | + if (ctx.get_nextCommand() == null) { | |
257 | + ctx.get_cmdHistory().add(line); | |
258 | + ctx.get_aggregatedHistory().remove(line); | |
259 | + ctx.get_aggregatedHistory().add(line); | |
251 | 260 | } |
252 | 261 | |
253 | 262 | // 1文字目がドットか否かでコマンドもしくはセル入力を判断 |
@@ -254,7 +263,7 @@ | ||
254 | 263 | if (line.charAt(0) == '.') { |
255 | 264 | processCommand(line); |
256 | 265 | } else { |
257 | - _cellCommand.processCell(_books.get(_currentBookName), line); | |
266 | + ctx.get_cellCommand().processCell(currentBook, line); | |
258 | 267 | } |
259 | 268 | |
260 | 269 |
@@ -277,7 +286,7 @@ | ||
277 | 286 | |
278 | 287 | |
279 | 288 | private String makePrompt(Book currentBook) { |
280 | - return "[" + _currentBookName + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": "; | |
289 | + return "[" + ctx.get_currentBookName() + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": "; | |
281 | 290 | } |
282 | 291 | |
283 | 292 |
@@ -299,16 +308,15 @@ | ||
299 | 308 | } |
300 | 309 | String[] cmdArr = cmdStr.split("\\."); |
301 | 310 | |
302 | - String methodName = _commandStorage.getMethodName(cmdStr.split("\\.")); | |
311 | + String methodName = ctx.get_commandStorage().getMethodName(cmdStr.split("\\.")); | |
303 | 312 | if (methodName == null) { |
304 | 313 | System.out.println("コマンド " + cmdStr + " はありません。"); |
305 | 314 | return; |
306 | 315 | } else { |
307 | 316 | try { |
308 | -// Method m = this.getClass().getMethod(methodName); | |
309 | - Method m = this.getClass().getDeclaredMethod(methodName, String.class); | |
317 | + Method m = this.getClass().getDeclaredMethod(methodName, String.class, Context.class); | |
310 | 318 | m.setAccessible(true); |
311 | - m.invoke(this, argStr); | |
319 | + m.invoke(this, argStr, ctx); | |
312 | 320 | } catch (NoSuchMethodException e) { |
313 | 321 | System.out.println("コマンド " + cmdStr + "に対応するメソッド " + methodName + " が定義されていません。"); |
314 | 322 | return; |
@@ -332,15 +340,15 @@ | ||
332 | 340 | |
333 | 341 | } |
334 | 342 | |
335 | - private void doQuit(String argStr) { | |
343 | + private void doQuit(String argStr, Context ctx) { | |
336 | 344 | _needQuit = true; |
337 | 345 | } |
338 | 346 | |
339 | - private void doHelp(String argStr) { | |
347 | + private void doHelp(String argStr, Context ctx) { | |
340 | 348 | if (argStr.isEmpty()){ |
341 | - System.out.println(_commandStorage.getHelp()); | |
349 | + System.out.println(ctx.get_commandStorage().getHelp()); | |
342 | 350 | } else { |
343 | - String helpString = _commandStorage.getHelp(argStr); | |
351 | + String helpString = ctx.get_commandStorage().getHelp(argStr); | |
344 | 352 | if (helpString != null) { |
345 | 353 | System.out.println(helpString); |
346 | 354 | } else { |
@@ -349,61 +357,60 @@ | ||
349 | 357 | } |
350 | 358 | } |
351 | 359 | |
352 | - private void doHistory(String argStr) { | |
353 | - StringBuilder sb = new StringBuilder(); | |
354 | - int cnt = 1; | |
355 | - for (String s : _cmdHistory) { | |
356 | - sb.append(cnt).append("\t").append(s).append("\n"); | |
357 | - cnt++; | |
360 | + private void doHistory(String argStr, Context ctx) { | |
361 | + History history = ctx.getConsole().getHistory(); | |
362 | + | |
363 | + for (Entry e : history) { | |
364 | + System.out.println(" " + Integer.toString(e.index()) + " " + e.value()); | |
358 | 365 | } |
359 | - if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') { | |
360 | - sb.deleteCharAt(sb.length() - 1); | |
361 | - } | |
362 | - System.out.println(sb); | |
363 | 366 | } |
364 | 367 | |
365 | - private void doExecHistory(String argStr) { | |
368 | + private void doExecHistory(String argStr, Context ctx) { | |
366 | 369 | int reqHistNo = 0; |
367 | 370 | try { |
368 | 371 | reqHistNo = Integer.parseInt(argStr); |
369 | 372 | } catch (Exception e) { |
373 | + // do nothing | |
370 | 374 | return; |
371 | 375 | } |
376 | + History history = ctx.getConsole().getHistory(); | |
372 | 377 | |
373 | - this._nextCommand = this._cmdHistory.get(reqHistNo - 1); | |
374 | - System.out.println(_nextCommand); | |
378 | + ctx.set_nextCommand(history.get(reqHistNo).toString()); | |
379 | + if (ctx.get_nextCommand() != null) { | |
380 | + System.out.println(ctx.get_nextCommand()); | |
381 | + } | |
375 | 382 | |
376 | 383 | } |
377 | 384 | |
378 | - private void doCellList(String argStr) { | |
379 | - Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet(); | |
380 | - _cellCommand.listCells(sheet); | |
381 | - _groupCommand.listGroups(sheet); | |
382 | - _tableCommand.listTables(sheet); | |
385 | + private void doCellList(String argStr, Context ctx) { | |
386 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
387 | + ctx.get_cellCommand().listCells(sheet); | |
388 | + ctx.get_groupCommand().listGroups(sheet); | |
389 | + ctx.get_tableCommand().listTables(sheet); | |
383 | 390 | return; |
384 | 391 | } |
385 | 392 | |
386 | - private void doBookAdd(String argStr) { | |
393 | + private void doBookAdd(String argStr, Context ctx) { | |
387 | 394 | Book newBook = new Book(argStr); |
388 | 395 | newBook.addSheet("sheet1"); |
389 | - _books.put(newBook.getName(), newBook); | |
396 | + ctx.get_books().put(newBook.getName(), newBook); | |
390 | 397 | } |
391 | 398 | |
392 | - private void doBookCurrent(String argStr) { | |
399 | + private void doBookCurrent(String argStr, Context ctx) { | |
393 | 400 | // カレントブックの変更 |
394 | - if (_books.containsKey(argStr)) { | |
395 | - this._currentBookName = argStr; | |
401 | + if (ctx.get_books().containsKey(argStr)) { | |
402 | + ctx.set_currentBookName(argStr); | |
396 | 403 | } |
397 | 404 | } |
398 | 405 | |
399 | - private void doBookList(String argStr) { | |
400 | - for (String bookName: new TreeSet<String>(_books.keySet())) { | |
406 | + private void doBookList(String argStr, Context ctx) { | |
407 | + for (String bookName: new TreeSet<String>(ctx.get_books().keySet())) { | |
401 | 408 | System.out.println(bookName); |
402 | 409 | } |
403 | 410 | } |
404 | 411 | |
405 | - private void doBookFunctions(String argStr) { | |
406 | - String[] functionNames = _books.get(_currentBookName).getFunctionNames(); | |
412 | + private void doBookFunctions(String argStr, Context ctx) { | |
413 | + String[] functionNames = ctx.getCurrentBook().getFunctionNames(); | |
407 | 414 | Arrays.sort(functionNames); |
408 | 415 | for (String funcName: Arrays.asList(functionNames)) { |
409 | 416 | System.out.println(funcName); |
@@ -410,15 +417,15 @@ | ||
410 | 417 | } |
411 | 418 | } |
412 | 419 | |
413 | - private void doBookLoad(String argStr) { | |
420 | + private void doBookLoad(String argStr, Context ctx) { | |
414 | 421 | String filePath = argStr; |
415 | - Book book = _books.get(_currentBookName); | |
422 | + Book book = ctx.getCurrentBook(); | |
416 | 423 | load(book, filePath); |
417 | 424 | } |
418 | 425 | |
419 | - private void doBookSave(String argStr) { | |
426 | + private void doBookSave(String argStr, Context ctx) { | |
420 | 427 | String filePath = argStr; |
421 | - Book book = _books.get(_currentBookName); | |
428 | + Book book = ctx.getCurrentBook(); | |
422 | 429 | SimpleWriter sw = new SimpleWriter(new ConcurrentBookWrapper(book)); |
423 | 430 | BufferedWriter w = null; |
424 | 431 | try { |
@@ -460,16 +467,86 @@ | ||
460 | 467 | } |
461 | 468 | |
462 | 469 | |
463 | - private void doSheetList(String argStr) { | |
464 | - BookCommand.listSheets(_books.get(_currentBookName), System.out); | |
470 | + private void doSheetList(String argStr, Context ctx) { | |
471 | + BookCommand.listSheets(ctx.getCurrentBook(), System.out); | |
465 | 472 | } |
466 | 473 | |
467 | - private void doSheetAdd(String argStr) { | |
468 | - _books.get(_currentBookName).addSheet(argStr); | |
474 | + private void doSheetAdd(String argStr, Context ctx) { | |
475 | + ctx.getCurrentBook().addSheet(argStr); | |
469 | 476 | } |
470 | 477 | |
471 | - private void doSheetCurrent(String argStr) { | |
472 | - Book book = _books.get(_currentBookName); | |
478 | + private void doSheetCurrent(String argStr, Context ctx) { | |
479 | + Book book = ctx.getCurrentBook(); | |
473 | 480 | book.getResolver().setCurrentSheet(book.getSheet(argStr)); |
474 | 481 | } |
482 | + | |
483 | + private void doGroupAdd(String argStr, Context ctx) { | |
484 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
485 | + if (sheet.groupExists(argStr)) { | |
486 | + return; | |
487 | + } | |
488 | + | |
489 | + try { | |
490 | + sheet.addGroup(argStr); | |
491 | + } catch (Exception e) { | |
492 | + e.printStackTrace(ctx.getOut()); | |
493 | + } | |
494 | + } | |
495 | + | |
496 | + private void doGroupList(String argStr, Context ctx) { | |
497 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
498 | + for (Group g : sheet.getGroups()) { | |
499 | + ctx.getOut().println(g.getName()); | |
500 | + } | |
501 | + } | |
502 | + | |
503 | + private void doGroupCellsAdd(String argStr, Context ctx) { | |
504 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
505 | + String[] args = argStr.split(" +"); | |
506 | + String groupName = args[0]; | |
507 | + if (!sheet.groupExists(groupName)) { | |
508 | + ctx.getOut().println("グループ " + groupName + " は存在しません。"); | |
509 | + return; | |
510 | + } | |
511 | + for (int i = 1; i < args.length; i++) { | |
512 | + String cellName = args[i]; | |
513 | + if (!sheet.cellExists(cellName)) { | |
514 | + ctx.getOut().println("セル " + cellName + " は存在しません。"); | |
515 | + return; | |
516 | + } | |
517 | + sheet.getGroup(groupName).addCell(cellName); | |
518 | + } | |
519 | + } | |
520 | + | |
521 | + private void doGroupListCells(String argStr, Context ctx) { | |
522 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
523 | + if (!sheet.groupExists(argStr)) { | |
524 | + ctx.getOut().println("グループ " + argStr + " は存在しません。"); | |
525 | + return; | |
526 | + } | |
527 | + Group g = sheet.getGroup(argStr); | |
528 | + for (Cell c: g.getCells()) { | |
529 | + ctx.getOut().println(c.getName()); | |
530 | + } | |
531 | + } | |
532 | + | |
533 | + private void doGroupCellsRemove(String argStr, Context ctx) { | |
534 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
535 | + String[] args = argStr.split(" +"); | |
536 | + String groupName = args[0]; | |
537 | + if (!sheet.groupExists(groupName)) { | |
538 | + ctx.getOut().println("グループ " + groupName + " は存在しません。"); | |
539 | + return; | |
540 | + } | |
541 | + Group g = sheet.getGroup(groupName); | |
542 | + for (int i = 1; i < args.length; i++) { | |
543 | + String cellName = args[i]; | |
544 | + sheet.getGroup(groupName).removeCell(cellName); | |
545 | + } | |
546 | + } | |
547 | + | |
548 | + private void doGroupDelete(String argStr, Context ctx) { | |
549 | + Sheet sheet = ctx.getCurrentBook().getResolver().getCurrentSheet(); | |
550 | + sheet.deleteGroup(argStr); | |
551 | + } | |
475 | 552 | } |
@@ -0,0 +1,148 @@ | ||
1 | +package com.nissy_ki_chi.ConsolePicocalc; | |
2 | + | |
3 | +import java.io.InputStream; | |
4 | +import java.io.PrintStream; | |
5 | +import java.util.HashMap; | |
6 | +import java.util.LinkedList; | |
7 | +import java.util.Map; | |
8 | + | |
9 | +import com.nissy_ki_chi.jpicosheet.core.Book; | |
10 | + | |
11 | +import jline.console.ConsoleReader; | |
12 | + | |
13 | +public class Context { | |
14 | + | |
15 | + private Map<String, Book> _books = new HashMap<String, Book>(); | |
16 | + private String _currentBookName = null; | |
17 | + | |
18 | + private BookCommand _bookCommand = new BookCommand(); | |
19 | + private SheetCommand _sheetCommand = new SheetCommand(); | |
20 | + private CellCommand _cellCommand = new CellCommand(); | |
21 | + private GroupCommand _groupCommand = new GroupCommand(); | |
22 | + private TableCommand _tableCommand = new TableCommand(); | |
23 | + | |
24 | + private LinkedList<String> _cmdHistory = new LinkedList<>(); | |
25 | + private LinkedList<String> _aggregatedHistory = new LinkedList<>(); | |
26 | + private String _nextCommand = null; | |
27 | + | |
28 | + private CommandFragment _commandStorage = new CommandFragment("."); | |
29 | + | |
30 | + private InputStream in = null; | |
31 | + private PrintStream out = null; | |
32 | + | |
33 | + private static String DEFAULT_BOOK_NAME = "myBook"; | |
34 | + private static String DEFAULT_SHEET_NAME = "Sheet1"; | |
35 | + | |
36 | + public Context() {}; | |
37 | + | |
38 | + public ConsoleReader getConsole() { | |
39 | + return console; | |
40 | + } | |
41 | + | |
42 | + public void setConsole(ConsoleReader console) { | |
43 | + this.console = console; | |
44 | + } | |
45 | + ConsoleReader console; | |
46 | + | |
47 | + public Map<String, Book> get_books() { | |
48 | + return _books; | |
49 | + } | |
50 | + | |
51 | + public Book getCurrentBook(){ | |
52 | + return _books.get(_currentBookName); | |
53 | + } | |
54 | + | |
55 | + | |
56 | + public String get_currentBookName() { | |
57 | + return _currentBookName; | |
58 | + } | |
59 | + public BookCommand get_bookCommand() { | |
60 | + return _bookCommand; | |
61 | + } | |
62 | + public SheetCommand get_sheetCommand() { | |
63 | + return _sheetCommand; | |
64 | + } | |
65 | + public CellCommand get_cellCommand() { | |
66 | + return _cellCommand; | |
67 | + } | |
68 | + public GroupCommand get_groupCommand() { | |
69 | + return _groupCommand; | |
70 | + } | |
71 | + public TableCommand get_tableCommand() { | |
72 | + return _tableCommand; | |
73 | + } | |
74 | + public LinkedList<String> get_cmdHistory() { | |
75 | + return _cmdHistory; | |
76 | + } | |
77 | + public LinkedList<String> get_aggregatedHistory() { | |
78 | + return _aggregatedHistory; | |
79 | + } | |
80 | + public String get_nextCommand() { | |
81 | + return _nextCommand; | |
82 | + } | |
83 | + public CommandFragment get_commandStorage() { | |
84 | + return _commandStorage; | |
85 | + } | |
86 | + public static String getDEFAULT_BOOK_NAME() { | |
87 | + return DEFAULT_BOOK_NAME; | |
88 | + } | |
89 | + public static String getDEFAULT_SHEET_NAME() { | |
90 | + return DEFAULT_SHEET_NAME; | |
91 | + } | |
92 | + public void set_books(Map<String, Book> _books) { | |
93 | + this._books = _books; | |
94 | + } | |
95 | + public void set_currentBookName(String _currentBookName) { | |
96 | + this._currentBookName = _currentBookName; | |
97 | + } | |
98 | + public void set_bookCommand(BookCommand _bookCommand) { | |
99 | + this._bookCommand = _bookCommand; | |
100 | + } | |
101 | + public void set_sheetCommand(SheetCommand _sheetCommand) { | |
102 | + this._sheetCommand = _sheetCommand; | |
103 | + } | |
104 | + public void set_cellCommand(CellCommand _cellCommand) { | |
105 | + this._cellCommand = _cellCommand; | |
106 | + } | |
107 | + public void set_groupCommand(GroupCommand _groupCommand) { | |
108 | + this._groupCommand = _groupCommand; | |
109 | + } | |
110 | + public void set_tableCommand(TableCommand _tableCommand) { | |
111 | + this._tableCommand = _tableCommand; | |
112 | + } | |
113 | + public void set_cmdHistory(LinkedList<String> _cmdHistory) { | |
114 | + this._cmdHistory = _cmdHistory; | |
115 | + } | |
116 | + public void set_aggregatedHistory(LinkedList<String> _aggregatedHistory) { | |
117 | + this._aggregatedHistory = _aggregatedHistory; | |
118 | + } | |
119 | + public void set_nextCommand(String _nextCommand) { | |
120 | + this._nextCommand = _nextCommand; | |
121 | + } | |
122 | + public void set_commandStorage(CommandFragment _commandStorage) { | |
123 | + this._commandStorage = _commandStorage; | |
124 | + } | |
125 | + public static void setDEFAULT_BOOK_NAME(String dEFAULT_BOOK_NAME) { | |
126 | + DEFAULT_BOOK_NAME = dEFAULT_BOOK_NAME; | |
127 | + } | |
128 | + public static void setDEFAULT_SHEET_NAME(String dEFAULT_SHEET_NAME) { | |
129 | + DEFAULT_SHEET_NAME = dEFAULT_SHEET_NAME; | |
130 | + } | |
131 | + | |
132 | + public InputStream getIn() { | |
133 | + return in; | |
134 | + } | |
135 | + | |
136 | + public void setIn(InputStream is) { | |
137 | + this.in = is; | |
138 | + } | |
139 | + | |
140 | + public PrintStream getOut() { | |
141 | + return out; | |
142 | + } | |
143 | + | |
144 | + public void setOut(PrintStream os) { | |
145 | + this.out = os; | |
146 | + } | |
147 | + | |
148 | +} |