private void help() throws IOException { String[] help = new String[] { help_fw_page, help_bw_page, help_fw_dline, help_bw_dline, help_fw_sline, help_bw_sline, help_go_start, help_go_end, help_fw_search1, help_fw_search2, help_bw_search1, help_bw_search2, help_help, help_quit }; // Remember the 'current' buffer so that we can repaint it // when we are done. ScreenBuffer prevBuffer = this.currentBuffer; // Prepare and paint the help screen ScreenBuffer buffer = new ScreenBuffer(true); for (int i = 0; i < help.length; i++) { prepareLine(help[i], i, buffer); } buffer.adjust(0, 0); buffer.output(); prompt(str_any_key); // Wait till the user is done, then repaint the previous screen. pr.read(); prompt(); prevBuffer.output(); }
/** * Prepare lines for output by painting them to our private buffer starting at the supplied * bufferLineOffset * * @param line the line to be prepared * @param lineNo the line's line number * @param buffer the ScreenBuffer we are preparing */ private boolean prepareLine(String line, int lineNo, ScreenBuffer buffer) { buffer.startLine(lineNo); int pos = 0; int len = line.length(); int startMatchPos = len; int endMatchPos = len; if (matcher != null) { matcher.reset(line); if (matcher.find(0)) { startMatchPos = matcher.start(); endMatchPos = matcher.end(); } } for (int i = 0; i < len; i++) { if (i == startMatchPos) { buffer.setColor(MATCH_COLOR); } else if (i == endMatchPos) { if (matcher.find(i)) { startMatchPos = matcher.start(); endMatchPos = matcher.end(); if (startMatchPos > i) { buffer.setColor(DEFAULT_COLOR); } } else { buffer.setColor(DEFAULT_COLOR); } } // FIXME - support different renderings, including ones where // control characters are rendered as visible characters? char ch = line.charAt(i); switch (ch) { case '\n': throw new AssertionError("no newlines expected"); case '\r': // ignore bare CRs. break; case '\t': int fill = tabSize - pos % pageWidth % tabSize; for (int j = 0; j < fill; j++) { buffer.putChar(' '); } pos += fill; break; default: if (ch >= ' ' && ch <= '\377' && ch != '\177') { buffer.putChar(ch); } else { buffer.putChar('?'); } pos++; } } buffer.setColor(DEFAULT_COLOR); buffer.endLine(); return !buffer.isComplete(); }
/** * 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; } } }