001 /*
002 * Copyright (C) 2012 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.processor.jline;
021
022 import org.crsh.shell.ShellProcessContext;
023 import org.crsh.shell.ShellResponse;
024 import org.crsh.text.Chunk;
025 import org.crsh.text.Style;
026 import org.crsh.text.Text;
027
028 import java.io.IOException;
029 import java.util.concurrent.CountDownLatch;
030 import java.util.concurrent.atomic.AtomicReference;
031
032 class JLineProcessContext implements ShellProcessContext {
033
034 /** . */
035 private static final Character NO_ECHO = (char)0;
036
037 /** . */
038 final JLineProcessor processor;
039
040 /** . */
041 final CountDownLatch latch = new CountDownLatch(1);
042
043 /** . */
044 final AtomicReference<ShellResponse> resp = new AtomicReference<ShellResponse>();
045
046 public JLineProcessContext(JLineProcessor processor) {
047 this.processor = processor;
048 }
049
050 public int getWidth() {
051 return processor.reader.getTerminal().getWidth();
052 }
053
054 public int getHeight() {
055 return processor.reader.getTerminal().getHeight();
056 }
057
058 public String getProperty(String name) {
059 return null;
060 }
061
062 public String readLine(String msg, boolean echo) {
063 try {
064 if (echo) {
065 return processor.reader.readLine(msg);
066 } else {
067 return processor.reader.readLine(msg, NO_ECHO);
068 }
069 }
070 catch (IOException e) {
071 return null;
072 }
073 }
074
075 public void provide(Chunk element) throws IOException {
076 if (element instanceof Text) {
077 Text textChunk = (Text)element;
078 processor.writer.append(textChunk.getText());
079 } else if (element instanceof Style) {
080 try {
081 ((Style)element).writeAnsiTo(processor.writer);
082 }
083 catch (IOException ignore) {
084 }
085 } else {
086
087
088 // Clear screen : this method has drawback to modifying history
089 // processor.writer.print("\033[");
090 // processor.writer.print("2J");
091 // processor.writer.flush();
092
093 // Clear all lines without touching the history
094 int height = processor.reader.getTerminal().getHeight();
095 for (int i = 1;i < height;i++) {
096 processor.writer.print("\033[");
097 processor.writer.print(i + ";1H");
098 processor.writer.print("\033[");
099 processor.writer.print("K");
100 }
101
102 // Move cursor to top
103 processor.writer.print("\033[");
104 processor.writer.print("1;1H");
105
106 // Flush all the changes
107 // processor.writer.flush();
108 }
109 }
110
111 public void flush() {
112 processor.writer.flush();
113 }
114
115 public void end(ShellResponse response) {
116 resp.set(response);
117 latch.countDown();
118 }
119 }