private void searchForwards() throws IOException { String input = readLine('/'); int lineNo = topLineNo; if (input.length() <= 1) { if (regex == null) { setPrompt(str_no_prev); return; } lineNo++; } else { try { regex = Pattern.compile(input.substring(1)); } catch (PatternSyntaxException ex) { setPrompt(str_inv_regex); return; } matcher = regex.matcher(""); } while (true) { String line = lineStore.getLine(lineNo); if (line == null) { gotoLastPage(); setPrompt(str_not_found); return; } matcher.reset(line); if (matcher.find()) { prepare(lineNo, 0).output(); return; } lineNo++; } }
private void prompt() { if (prompt == null) { prompt( lineStore.isLastLineNo(bottomLineNo) ? "(END)" : (topLineNo + ", " + bottomLineNo + ": ")); } else { prompt(prompt); prompt = null; } }
/** * Prepare lines for output by painting them to our private buffer in the reverse direction * starting at a given end line number and (rendered) subline number. */ private ScreenBuffer prepareReverse(int endLineNo, int endSublineNo) { ScreenBuffer buffer = new ScreenBuffer(false); int lineNo = endLineNo; String line = null; boolean more = true; while (more && lineNo >= 0) { line = lineStore.getLine(lineNo); more = prepareLine(line, lineNo, buffer); lineNo--; } if (buffer.adjust(endLineNo, endSublineNo)) { return buffer; } else { return prepare(0, 0); } }
/** * Prepare lines for output by painting them to our private buffer in the forward direction * starting at a given line number and (rendered) subline number. * * @param startLineNo */ private ScreenBuffer prepare(int startLineNo, int startSublineNo) { while (true) { ScreenBuffer buffer = new ScreenBuffer(true); int lineNo = startLineNo; boolean more; do { String line = lineStore.getLine(lineNo); if (line == null) { break; } more = prepareLine(line, lineNo, buffer); lineNo++; } while (more); if (buffer.adjust(startLineNo, startSublineNo) || startLineNo == 0) { return buffer; } else { startLineNo -= 1; startSublineNo = LAST_SUBLINE; } } }
private void gotoLastPage() throws IOException { prepareReverse(lineStore.getLastLineNo(), LAST_SUBLINE).output(); }
/** * Do the paging, reading commands from our private console input pipe to figure out what to do * next. * * @param r the source of data to be paged. * @throws IOException */ private void pager() throws IOException { // Output first page. console.clear(); bottomLineNo = -1; boolean exit = false; nextPage(); // Process commands until we reach the EOF on the data source or // the command pipe. while (!exit) { prompt(); int ch = pr.read(); erasePrompt(); switch (ch) { case -1: exit = true; break; case ' ': case 'f': if (lineStore.isLastLineNo(bottomLineNo)) { exit = true; } else { nextPage(); } break; case 'b': prevPage(); break; case 'k': case 'y': prevLine(); break; case '\n': if (lineStore.isLastLineNo(bottomLineNo)) { exit = true; } else { nextLine(); } break; case 'u': prevScreenLine(); break; case 'd': nextScreenLine(); break; case '<': gotoPage(0); break; case '>': gotoLastPage(); break; case '/': searchForwards(); break; case '?': searchBackwards(); break; case '\004': // ^D case 'q': exit = true; break; case 'h': help(); default: // ignore } } }