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