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
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 throw new UnsupportedOperationException();
065 }
066
067 public int getWidth() {
068 // return reader.getTermwidth();
069 throw new UnsupportedOperationException();
070 }
071
072 public String getProperty(String name) {
073 return null;
074 }
075
076 public CodeType decode(int code) {
077 /*
078 short action = keyBindings[code];
079 switch (action) {
080 case ConsoleReader.COMPLETE:
081 return CodeType.TAB;
082 case ConsoleReader.DELETE_PREV_CHAR:
083 return CodeType.BACKSPACE;
084 case ConsoleReader.PREV_CHAR:
085 return CodeType.LEFT;
086 case ConsoleReader.NEXT_CHAR:
087 return CodeType.RIGHT;
088 case ConsoleReader.EXIT:
089 return CodeType.CLOSE;
090 case ConsoleReader.PREV_HISTORY:
091 return CodeType.UP;
092 case ConsoleReader.NEXT_HISTORY:
093 return CodeType.DOWN;
094 default:
095 return CodeType.CHAR;
096 }
097 */
098 throw new UnsupportedOperationException();
099 }
100
101 public void close() {
102 //
103 }
104
105 public void flush() throws IOException {
106 System.out.print(buffer);
107 buffer.setLength(0);
108 }
109
110 public void write(String s) throws IOException {
111 buffer.append(s);
112 }
113
114 public void write(char c) throws IOException {
115 buffer.append(c);
116 }
117
118 public void writeDel() throws IOException {
119 buffer.append("\b \b");
120 }
121
122 public void writeCRLF() throws IOException {
123 buffer.append("\n");
124 }
125
126 public boolean moveRight(char c) throws IOException {
127 buffer.append(c);
128 return true;
129 }
130
131 public boolean moveLeft() throws IOException {
132 buffer.append("\b");
133 return true;
134 }
135 }