001/*
002 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
003 *
004 * This software is distributable under the BSD license. See the terms of the
005 * BSD license in the documentation provided with this software.
006 */
007package jline;
008
009import java.io.IOException;
010
011/**
012 *  A no-op unsupported terminal.
013 *
014 *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
015 */
016public class UnsupportedTerminal extends Terminal {
017    private Thread maskThread = null;
018
019    public void initializeTerminal() {
020        // nothing we need to do (or can do) for windows.
021    }
022
023    public boolean getEcho() {
024        return true;
025    }
026
027
028    public boolean isEchoEnabled() {
029        return true;
030    }
031
032
033    public void enableEcho() {
034    }
035
036
037    public void disableEcho() {
038    }
039
040
041    /**
042     *  Always returng 80, since we can't access this info on Windows.
043     */
044    public int getTerminalWidth() {
045        return 80;
046    }
047
048    /**
049     *  Always returng 24, since we can't access this info on Windows.
050     */
051    public int getTerminalHeight() {
052        return 80;
053    }
054
055    public boolean isSupported() {
056        return false;
057    }
058
059    public void beforeReadLine(final ConsoleReader reader, final String prompt,
060       final Character mask) {
061        if ((mask != null) && (maskThread == null)) {
062            final String fullPrompt = "\r" + prompt
063                + "                 "
064                + "                 "
065                + "                 "
066                + "\r" + prompt;
067
068            maskThread = new Thread("JLine Mask Thread") {
069                public void run() {
070                    while (!interrupted()) {
071                        try {
072                            reader.out.write(fullPrompt);
073                            reader.out.flush();
074                            sleep(3);
075                        } catch (IOException ioe) {
076                            return;
077                        } catch (InterruptedException ie) {
078                            return;
079                        }
080                    }
081                }
082            };
083
084            maskThread.setPriority(Thread.MAX_PRIORITY);
085            maskThread.setDaemon(true);
086            maskThread.start();
087        }
088    }
089
090    public void afterReadLine(final ConsoleReader reader, final String prompt,
091        final Character mask) {
092        if ((maskThread != null) && maskThread.isAlive()) {
093            maskThread.interrupt();
094        }
095
096        maskThread = null;
097    }
098}