001: /*
002: * ShellFormatter.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: ShellFormatter.java,v 1.5 2003/12/04 15:17:06 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.RE;
025: import gnu.regexp.REMatch;
026:
027: public final class ShellFormatter extends Formatter {
028: // Formats.
029: private static final byte SHELL_FORMAT_TEXT = 0;
030: private static final byte SHELL_FORMAT_PROMPT = 1;
031: private static final byte SHELL_FORMAT_INPUT = 2;
032:
033: private final RE promptRE;
034:
035: public ShellFormatter(Buffer buffer) {
036: this .buffer = buffer;
037: promptRE = ((Shell) buffer).getPromptRE();
038: }
039:
040: public LineSegmentList formatLine(Line line) {
041: clearSegmentList();
042: if (line == null) {
043: addSegment("", SHELL_FORMAT_TEXT);
044: return segmentList;
045: }
046: final String text = getDetabbedText(line);
047: if (line.flags() == STATE_PROMPT) {
048: REMatch match = promptRE.getMatch(text);
049: if (match != null) {
050: final int end = match.getEndIndex();
051: addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
052: addSegment(text, end, SHELL_FORMAT_INPUT);
053: } else
054: addSegment(text, SHELL_FORMAT_PROMPT);
055: return segmentList;
056: }
057: if (line.flags() == STATE_PASSWORD_PROMPT) {
058: addSegment(text, SHELL_FORMAT_TEXT);
059: return segmentList;
060: }
061: if (line.flags() == STATE_OUTPUT) {
062: addSegment(text, SHELL_FORMAT_TEXT);
063: return segmentList;
064: }
065: if (promptRE != null) {
066: if (line.flags() == STATE_INPUT) {
067: REMatch match = promptRE.getMatch(text);
068: if (match != null) {
069: final int end = match.getEndIndex();
070: addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
071: addSegment(text, end, SHELL_FORMAT_INPUT);
072: } else {
073: // No prompt text.
074: addSegment(text, SHELL_FORMAT_INPUT);
075: }
076: return segmentList;
077: }
078: Line next = line.next();
079: if (next == null) {
080: // Last line of buffer. Check for prompt.
081: REMatch match = promptRE.getMatch(text);
082: if (match != null) {
083: line.setFlags(STATE_PROMPT);
084: final int end = match.getEndIndex();
085: addSegment(text, 0, end, SHELL_FORMAT_PROMPT);
086: addSegment(text, end, SHELL_FORMAT_INPUT);
087: } else
088: addSegment(text, SHELL_FORMAT_INPUT);
089: return segmentList;
090: }
091: }
092: addSegment(text, SHELL_FORMAT_TEXT);
093: return segmentList;
094: }
095:
096: public FormatTable getFormatTable() {
097: if (formatTable == null) {
098: formatTable = new FormatTable("ShellMode");
099: formatTable.addEntryFromPrefs(SHELL_FORMAT_TEXT, "text");
100: formatTable
101: .addEntryFromPrefs(SHELL_FORMAT_PROMPT, "prompt");
102: formatTable.addEntryFromPrefs(SHELL_FORMAT_INPUT, "input");
103: }
104: return formatTable;
105: }
106: }
|