001package com.pi4j.util;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  Console.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2021 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033
034public class Console {
035
036    private static final int LINE_WIDTH = 60;
037    public static final String CLEAR_SCREEN_ESCAPE_SEQUENCE = "\033[2J\033[1;1H";
038    public static final String ERASE_LINE_ESCAPE_SEQUENCE = "\033[K";
039
040    public static final char LINE_SEPARATOR_CHAR = '*';
041    public static final String LINE_SEPARATOR = StringUtil.repeat(LINE_SEPARATOR_CHAR, LINE_WIDTH);
042
043    protected boolean exiting = false;
044
045    public synchronized Console println(String format, Object ... args){
046        return println(String.format(format, args));
047    }
048
049    public synchronized Console print(String format, Object ... args){
050        return print(String.format(format, args));
051    }
052
053    public synchronized Console println(String line){
054        System.out.println(line);
055        return this;
056    }
057
058    public synchronized Console println(Object line){
059        System.out.println(line);
060        return this;
061    }
062
063    public synchronized Console println(){
064        return println("");
065    }
066
067    public synchronized Console print(Object data){
068        System.out.print(data);
069        return this;
070    }
071
072    public synchronized Console print(String data){
073        System.out.print(data);
074        return this;
075    }
076
077    public synchronized Console println(char character, int repeat){
078        return println(StringUtil.repeat(character, repeat));
079    }
080
081    public synchronized Console emptyLine(){
082        return emptyLine(1);
083    }
084
085    public synchronized Console emptyLine(int number){
086        for(int index = 0; index < number; index++){
087            println();
088        }
089        return this;
090    }
091
092    public synchronized Console separatorLine(){
093        return println(LINE_SEPARATOR);
094    }
095
096    public synchronized Console separatorLine(char character){
097        return separatorLine(character, LINE_WIDTH);
098    }
099
100    public synchronized Console separatorLine(char character, int length){
101        return println(StringUtil.repeat(character, length));
102    }
103
104    public synchronized Console title(String ... title){
105        clearScreen().separatorLine().separatorLine().emptyLine();
106        for(String s : title) {
107            println(StringUtil.center(s, LINE_WIDTH));
108        }
109        emptyLine().separatorLine().separatorLine().emptyLine();
110        return this;
111    }
112
113    public synchronized Console box(String ... lines) {
114        return box(2, lines);
115    }
116
117    public synchronized Console box(int padding, String ... lines) {
118        int max_length = 0;
119        for(String l : lines) {
120            if (l.length() > max_length) {
121                max_length = l.length();
122            }
123        }
124        separatorLine('-', max_length + padding * 2 + 2);
125        String left  = StringUtil.padRight("|", padding);
126        String right = StringUtil.padLeft("|", padding);
127        for(String l : lines){
128            println(StringUtil.concat(left, StringUtil.padRight(l, max_length - l.length()), right));
129        }
130        separatorLine('-', max_length + padding * 2 + 2);
131        return this;
132    }
133
134    public synchronized Console goodbye() {
135        emptyLine();
136        separatorLine();
137        println(StringUtil.center("GOODBYE", LINE_WIDTH));
138        separatorLine();
139        emptyLine();
140        return this;
141    }
142
143    public synchronized Console clearScreen(){
144        return print(CLEAR_SCREEN_ESCAPE_SEQUENCE);
145    }
146
147    public synchronized Console eraseLine(){
148        return print(ERASE_LINE_ESCAPE_SEQUENCE);
149    }
150
151    public synchronized Console promptForExit(){
152        box(4, "PRESS CTRL-C TO EXIT");
153        emptyLine();
154        exiting = false;
155        Runtime.getRuntime().addShutdownHook(new Thread() {
156            @Override
157            public void run() {
158                exiting = true;
159                goodbye();
160            }
161        });
162        return this;
163    }
164
165    public void waitForExit() throws InterruptedException {
166        while(!exiting){
167            Thread.sleep(50);
168        }
169    }
170
171    public synchronized boolean exiting(){
172        return exiting;
173    }
174    public synchronized boolean isRunning(){
175        return !exiting;
176    }
177
178    public synchronized void exit(){
179        exiting = true;
180        goodbye();
181    }
182}