001 package org.crsh.processor.jline;
002
003 import org.crsh.shell.ShellProcessContext;
004 import org.crsh.shell.ShellResponse;
005
006 import java.io.IOException;
007 import java.util.concurrent.CountDownLatch;
008 import java.util.concurrent.atomic.AtomicReference;
009
010 /** @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> */
011 class JLineProcessContext implements ShellProcessContext {
012
013 /** . */
014 private static final Character NO_ECHO = (char)0;
015
016 /** . */
017 final JLineProcessor processor;
018
019 /** . */
020 final CountDownLatch latch = new CountDownLatch(1);
021
022 /** . */
023 final AtomicReference<ShellResponse> resp = new AtomicReference<ShellResponse>();
024
025 public JLineProcessContext(JLineProcessor processor) {
026 this.processor = processor;
027 }
028
029 public int getWidth() {
030 return processor.reader.getTerminal().getWidth();
031 }
032
033 public String getProperty(String name) {
034 return null;
035 }
036
037 public String readLine(String msg, boolean echo) {
038 try {
039 if (echo) {
040 return processor.reader.readLine(msg);
041 } else {
042 return processor.reader.readLine(msg, NO_ECHO);
043 }
044 }
045 catch (IOException e) {
046 return null;
047 }
048 }
049
050 public void end(ShellResponse response) {
051 resp.set(response);
052 latch.countDown();
053 }
054 }