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      public JLineIO() throws Exception {
043        ConsoleReader reader = new ConsoleReader();
044        Method method = ConsoleReader.class.getDeclaredMethod("getKeyForAction", short.class);
045        method.setAccessible(true);
046    
047    
048        Field f = ConsoleReader.class.getDeclaredField("keybindings");
049        f.setAccessible(true);
050        short[] keyBindings = (short[])f.get(reader);
051    
052    
053        //
054        this.reader = reader;
055        this.keyBindings = keyBindings;
056      }
057    
058      public int read() throws IOException {
059        int i = reader.readVirtualKey();
060        return i;
061      }
062    
063      public int getWidth() {
064        return reader.getTermwidth();
065      }
066    
067      public CodeType decode(int code) {
068        short action = keyBindings[code];
069        switch (action) {
070          case ConsoleReader.COMPLETE:
071            return CodeType.TAB;
072          case ConsoleReader.DELETE_PREV_CHAR:
073            return CodeType.BACKSPACE;
074          case ConsoleReader.PREV_CHAR:
075            return CodeType.LEFT;
076          case ConsoleReader.NEXT_CHAR:
077            return CodeType.RIGHT;
078          case ConsoleReader.EXIT:
079            return CodeType.CLOSE;
080          case ConsoleReader.PREV_HISTORY:
081            return CodeType.UP;
082          case ConsoleReader.NEXT_HISTORY:
083            return CodeType.DOWN;
084          default:
085            return CodeType.CHAR;
086        }
087      }
088    
089      public void close() {
090    
091      }
092    
093      public void flush() throws IOException {
094    
095      }
096    
097      public void write(String s) throws IOException {
098        System.out.print(s);
099      }
100    
101      public void write(char c) throws IOException {
102        System.out.print(c);
103      }
104    
105      public void writeDel() throws IOException {
106        System.out.print("\b \b");
107      }
108    
109      public void writeCRLF() throws IOException {
110        System.out.println();
111      }
112    
113      public boolean moveRight(char c) throws IOException {
114        System.out.print(c);
115        return true;
116      }
117    
118      public boolean moveLeft() throws IOException {
119        System.out.print("\b");
120        return true;
121      }
122    }