1 package ch
.cyberduck
.cli
;
4 * Copyright (c) 2002-2014 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch
.cyberduck
.core
.Factory
;
22 import ch
.cyberduck
.core
.exception
.ConnectionCanceledException
;
23 import ch
.cyberduck
.core
.exception
.LoginCanceledException
;
25 import org
.apache
.commons
.lang3
.StringUtils
;
26 import org
.fusesource
.jansi
.AnsiConsole
;
28 import java
.io
.BufferedReader
;
29 import java
.io
.IOException
;
30 import java
.io
.InputStreamReader
;
31 import java
.io
.PrintStream
;
32 import java
.io
.PrintWriter
;
33 import java
.util
.Arrays
;
34 import java
.util
.concurrent
.Semaphore
;
39 public class Console
{
41 private final java
.io
.Console console
44 private final PrintStream out
47 private static final Semaphore lock
50 public String
readLine(String format
, Object
... args
) throws ConnectionCanceledException
{
52 return this.wrap(console
.readLine(format
, args
));
54 this.printf(format
, args
);
55 final BufferedReader reader
= new BufferedReader(new InputStreamReader(System
.in
));
57 return this.wrap(reader
.readLine());
59 catch(IOException e
) {
60 throw new ConnectionCanceledException(e
);
64 public char[] readPassword(String format
, Object
... args
) throws ConnectionCanceledException
{
66 return this.wrap(console
.readPassword(format
, args
));
68 final String line
= this.readLine(format
, args
);
69 if(StringUtils
.isBlank(line
)) {
70 throw new LoginCanceledException();
72 return line
.toCharArray();
75 public void printf(final String format
, Object
... args
) {
76 if(StringUtils
.isEmpty(format
)) {
82 switch(Factory
.Platform
.getDefault()) {
86 final PrintWriter writer
= console
.writer();
87 if(Arrays
.asList(args
).isEmpty()) {
91 writer
.printf(format
, args
);
97 if(Arrays
.asList(args
).isEmpty()) {
101 out
.printf(format
, args
);
105 catch(InterruptedException e
) {
113 private String
wrap(final String input
) throws ConnectionCanceledException
{
115 throw new ConnectionCanceledException();
120 private char[] wrap(final char[] input
) throws ConnectionCanceledException
{
122 throw new ConnectionCanceledException();