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.spi.jline;
021    
022    import jline.ConsoleReader;
023    import org.crsh.term.CodeType;
024    import org.crsh.term.spi.TermIO;
025    
026    import java.io.IOException;
027    import java.lang.reflect.Field;
028    import java.lang.reflect.Method;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    public class JLineIO implements TermIO {
035    
036      /** . */
037      private final ConsoleReader reader;
038    
039      /** . */
040      private final short[] keyBindings;
041    
042      /** . */
043      private StringBuffer buffer = new StringBuffer();
044    
045      public JLineIO() throws Exception {
046        ConsoleReader reader = new ConsoleReader();
047        Method method = ConsoleReader.class.getDeclaredMethod("getKeyForAction", short.class);
048        method.setAccessible(true);
049    
050    
051        Field f = ConsoleReader.class.getDeclaredField("keybindings");
052        f.setAccessible(true);
053        short[] keyBindings = (short[])f.get(reader);
054    
055    
056        //
057        this.reader = reader;
058        this.keyBindings = keyBindings;
059      }
060    
061      public int read() throws IOException {
062        int i = reader.readVirtualKey();
063        return i;
064      }
065    
066      public int getWidth() {
067        return reader.getTermwidth();
068      }
069    
070      public String getProperty(String name) {
071        return null;
072      }
073    
074      public CodeType decode(int code) {
075        short action = keyBindings[code];
076        switch (action) {
077          case ConsoleReader.COMPLETE:
078            return CodeType.TAB;
079          case ConsoleReader.DELETE_PREV_CHAR:
080            return CodeType.BACKSPACE;
081          case ConsoleReader.PREV_CHAR:
082            return CodeType.LEFT;
083          case ConsoleReader.NEXT_CHAR:
084            return CodeType.RIGHT;
085          case ConsoleReader.EXIT:
086            return CodeType.CLOSE;
087          case ConsoleReader.PREV_HISTORY:
088            return CodeType.UP;
089          case ConsoleReader.NEXT_HISTORY:
090            return CodeType.DOWN;
091          default:
092            return CodeType.CHAR;
093        }
094      }
095    
096      public void close() {
097        //
098      }
099    
100      public void flush() throws IOException {
101        System.out.print(buffer);
102        buffer.setLength(0);
103      }
104    
105      public void write(String s) throws IOException {
106        buffer.append(s);
107      }
108    
109      public void write(char c) throws IOException {
110        buffer.append(c);
111      }
112    
113      public void writeDel() throws IOException {
114        buffer.append("\b \b");
115      }
116    
117      public void writeCRLF() throws IOException {
118        buffer.append("\n");
119      }
120    
121      public boolean moveRight(char c) throws IOException {
122        buffer.append(c);
123        return true;
124      }
125    
126      public boolean moveLeft() throws IOException {
127        buffer.append("\b");
128        return true;
129      }
130    }