001    /*
002     * Copyright (C) 2010 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;
021    
022    import org.crsh.term.console.Console;
023    import org.crsh.term.console.ViewWriter;
024    import org.crsh.term.spi.TermIO;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    import java.io.IOException;
029    import java.util.LinkedList;
030    
031    /**
032     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033     * @version $Revision$
034     */
035    public class BaseTerm implements Term {
036    
037      /** . */
038      private final Logger log = LoggerFactory.getLogger(BaseTerm.class);
039    
040      /** . */
041      private final LinkedList<CharSequence> history;
042    
043      /** . */
044      private CharSequence historyBuffer;
045    
046      /** . */
047      private int historyCursor;
048    
049      /** . */
050      private final TermIO io;
051    
052      /** . */
053      private final Console console;
054    
055      public BaseTerm(final TermIO io) {
056        this.history = new LinkedList<CharSequence>();
057        this.historyBuffer = null;
058        this.historyCursor = -1;
059        this.io = io;
060        this.console = new Console(new ViewWriter() {
061    
062          @Override
063          protected void flush() throws IOException {
064            io.flush();
065          }
066    
067          @Override
068          protected void writeCRLF() throws IOException {
069            io.writeCRLF();
070          }
071    
072          @Override
073          protected void write(CharSequence s) throws IOException {
074            io.write(s.toString());
075          }
076    
077          @Override
078          protected void write(char c) throws IOException {
079            io.write(c);
080          }
081    
082          @Override
083          protected void writeDel() throws IOException {
084            io.writeDel();
085          }
086    
087          @Override
088          protected boolean writeMoveLeft() throws IOException {
089            return io.moveLeft();
090          }
091    
092          @Override
093          protected boolean writeMoveRight(char c) throws IOException {
094            return io.moveRight(c);
095          }
096        });
097      }
098    
099      public int getWidth() {
100        return io.getWidth();
101      }
102    
103      public void setEcho(boolean echo) {
104        console.setEchoing(echo);
105      }
106    
107      public TermEvent read() throws IOException {
108    
109        //
110        while (true) {
111          int code = io.read();
112          CodeType type = io.decode(code);
113          switch (type) {
114            case CLOSE:
115              return new TermEvent.Close();
116            case BACKSPACE:
117              console.getViewReader().del();
118              break;
119            case UP:
120            case DOWN:
121              int nextHistoryCursor = historyCursor +  (type == CodeType.UP ? + 1 : -1);
122              if (nextHistoryCursor >= -1 && nextHistoryCursor < history.size()) {
123                CharSequence s = nextHistoryCursor == -1 ? historyBuffer : history.get(nextHistoryCursor);
124                CharSequence t = console.getViewReader().replace(s);
125                if (historyCursor == -1) {
126                  historyBuffer = t;
127                }
128                if (nextHistoryCursor == -1) {
129                  historyBuffer = null;
130                }
131                historyCursor = nextHistoryCursor;
132              }
133              break;
134            case RIGHT:
135              console.getViewReader().moveRight();
136              break;
137            case LEFT:
138              console.getViewReader().moveLeft();
139              break;
140            case BREAK:
141              log.debug("Want to cancel evaluation");
142              console.clearBuffer();
143              return new TermEvent.Break();
144            case CHAR:
145              if (code >= 0 && code < 128) {
146                console.getViewReader().write((char)code);
147              } else {
148                log.debug("Unhandled char " + code);
149              }
150              break;
151            case TAB:
152              log.debug("Tab");
153              return new TermEvent.Complete(console.getBufferToCursor());
154          }
155    
156          //
157          if (console.getReader().hasNext()) {
158            historyCursor = -1;
159            historyBuffer = null;
160            CharSequence input = console.getReader().next();
161            return new TermEvent.ReadLine(input);
162          }
163        }
164      }
165    
166      public void bufferInsert(CharSequence msg) throws IOException {
167        console.getViewReader().write(msg);
168      }
169    
170      public void addToHistory(CharSequence line) {
171        history.addFirst(line);
172      }
173    
174      public CharSequence getBuffer() {
175        return console.getBufferToCursor();
176      }
177    
178      public void close() {
179        try {
180          log.debug("Closing connection");
181          io.flush();
182          io.close();
183        } catch (IOException e) {
184          log.debug("Exception thrown during term close()", e);
185        }
186      }
187    
188      public void write(CharSequence msg) throws IOException {
189        console.getWriter().write(msg);
190      }
191    }