001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.term.console;
021    
022    import org.crsh.term.CodeType;
023    import org.crsh.term.Term;
024    import org.crsh.term.TermEvent;
025    import org.crsh.term.spi.TermIO;
026    import org.crsh.text.CLS;
027    import org.crsh.text.Chunk;
028    import org.crsh.text.Style;
029    import org.crsh.text.Text;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    import java.io.IOException;
034    import java.util.LinkedList;
035    
036    /**
037     * Implements the {@link Term interface}.
038     */
039    public class ConsoleTerm implements Term {
040    
041      /** . */
042      private final Logger log = LoggerFactory.getLogger(ConsoleTerm.class);
043    
044      /** . */
045      private final LinkedList<CharSequence> history;
046    
047      /** . */
048      private CharSequence historyBuffer;
049    
050      /** . */
051      private int historyCursor;
052    
053      /** . */
054      private final TermIO io;
055    
056      /** . */
057      private final TermIOBuffer buffer;
058    
059      /** . */
060      private final TermIOWriter writer;
061    
062      public ConsoleTerm(final TermIO io) {
063        this.history = new LinkedList<CharSequence>();
064        this.historyBuffer = null;
065        this.historyCursor = -1;
066        this.io = io;
067        this.buffer = new TermIOBuffer(io);
068        this.writer = new TermIOWriter(io);
069      }
070    
071      public int getWidth() {
072        return io.getWidth();
073      }
074    
075      public int getHeight() {
076        return io.getHeight();
077      }
078    
079      public String getProperty(String name) {
080        return io.getProperty(name);
081      }
082    
083      public void setEcho(boolean echo) {
084        buffer.setEchoing(echo);
085      }
086    
087      public TermEvent read() throws IOException {
088    
089        //
090        while (true) {
091          int code = io.read();
092          CodeType type = io.decode(code);
093          switch (type) {
094            case CLOSE:
095              return TermEvent.close();
096            case BACKSPACE:
097              buffer.del();
098              break;
099            case UP:
100            case DOWN:
101              int nextHistoryCursor = historyCursor +  (type == CodeType.UP ? + 1 : -1);
102              if (nextHistoryCursor >= -1 && nextHistoryCursor < history.size()) {
103                CharSequence s = nextHistoryCursor == -1 ? historyBuffer : history.get(nextHistoryCursor);
104                while (buffer.moveRight()) {
105                  // Do nothing
106                }
107                CharSequence t = buffer.replace(s);
108                if (historyCursor == -1) {
109                  historyBuffer = t;
110                }
111                if (nextHistoryCursor == -1) {
112                  historyBuffer = null;
113                }
114                historyCursor = nextHistoryCursor;
115              }
116              break;
117            case RIGHT:
118              buffer.moveRight();
119              break;
120            case LEFT:
121              buffer.moveLeft();
122              break;
123            case BREAK:
124              log.debug("Want to cancel evaluation");
125              buffer.clear();
126              return TermEvent.brk();
127            case CHAR:
128              if (code >= 0 && code < 128) {
129                buffer.append((char)code);
130              } else {
131                log.debug("Unhandled char " + code);
132              }
133              break;
134            case TAB:
135              log.debug("Tab");
136              return TermEvent.complete(buffer.getBufferToCursor());
137          }
138    
139          //
140          if (buffer.hasNext()) {
141            historyCursor = -1;
142            historyBuffer = null;
143            CharSequence input = buffer.next();
144            return TermEvent.readLine(input);
145          }
146        }
147      }
148    
149      public Appendable getInsertBuffer() {
150        return buffer;
151      }
152    
153      public void addToHistory(CharSequence line) {
154        history.addFirst(line);
155      }
156    
157      public CharSequence getBuffer() {
158        return buffer.getBufferToCursor();
159      }
160    
161      public void flush() {
162        try {
163          io.flush();
164        }
165        catch (IOException e) {
166          log.debug("Exception thrown during term flush()", e);
167        }
168      }
169    
170      public void close() {
171        try {
172          log.debug("Closing connection");
173          io.flush();
174          io.close();
175        } catch (IOException e) {
176          log.debug("Exception thrown during term close()", e);
177        }
178      }
179    
180      public Class<Chunk> getConsumedType() {
181        return Chunk.class;
182      }
183    
184      public void provide(Chunk element) throws IOException {
185        if (element == null) {
186          throw new NullPointerException("No null chunk accepted");
187        }
188        if (element instanceof Text) {
189          Text textChunk = (Text)element;
190          writer.write(textChunk.getText());
191        } else if (element instanceof Style) {
192          io.write(((Style)element));
193        } else if (element instanceof CLS) {
194          io.cls();
195        } else {
196          throw new UnsupportedOperationException("todo");
197        }
198      }
199    }