[LIB-9] Console output: stylistic changes
[chesshog.git] / chesshog-core / src / main / java / org / hedgecode / chess / ChessHogConsole.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess;
18
19 import java.time.LocalDateTime;
20 import java.util.Calendar;
21
22 /**
23  * ChessHog utility class for working with console.
24  *
25  * @author Dmitry Samoshin aka gotty
26  */
27 public class ChessHogConsole {
28
29     private static final String[] ASCII_LOGO = {
30             "  _______     _                   _                  ",
31             " |   |  _|_ _| |___ ___ ___ ___ _| |___              ",
32             " |     | -_| . | . | -_|  _| . | . | -_|             ",
33             " |     |___|___|_  |___|___|___|___|___|             ",
34             " |___|___|     |___|    _               _            ",
35             "                    ___| |_ ___ ___ ___| |_ ___ ___  ",
36             "                   |  _|   | -_|_ -|_ -|   | . | . | ",
37             "                   |___|_|_|___|___|___|_|_|___|_  | ",
38             "                                               |___| "
39     };
40
41     private static final String COPYRIGHT = "Copyright (c) Hedgecode";
42
43     private static boolean logMode = false;
44
45     private ChessHogConsole() {
46     }
47
48     public static void setLogMode(boolean isLogMode) {
49         logMode = isLogMode;
50     }
51
52     public static void console(String message) {
53         System.out.println(
54                 String.format(
55                         logMode ? "[%1$tT] %2$s" : "%2$s",
56                         LocalDateTime.now(),
57                         message == null ? "" : message
58                 )
59         );
60     }
61
62     public static void empty() {
63         console(null);
64     }
65
66     public static void init() {
67         asciiLogo();
68         copyright();
69         version();
70         empty();
71         os();
72         java();
73     }
74
75     private static void asciiLogo() {
76         for (String line : ASCII_LOGO) {
77             console(line);
78         }
79     }
80
81     private static void copyright() {
82         int inceptionYear = Integer.parseInt(
83                 ChessHogProperties.get("chesshog.inception.year")
84         );
85         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
86         String copyright = currentYear > inceptionYear
87                 ? String.format("%s-%s %s", inceptionYear, currentYear, COPYRIGHT)
88                 : String.format("%s %s", currentYear, COPYRIGHT);
89         console(
90                 alignCenter(copyright)
91         );
92     }
93
94     private static void version() {
95         console(
96                 alignCenter(
97                         String.format(
98                                 "v. %s", ChessHogProperties.get("chesshog.version")
99                         )
100                 )
101         );
102     }
103
104     private static void os() {
105         console(
106                 String.format(
107                         "OS: %s %s %s",
108                         System.getProperty("os.name"),
109                         System.getProperty("os.version"),
110                         System.getProperty("os.arch")
111                 )
112         );
113     }
114
115     private static void java() {
116         console(
117                 String.format(
118                         "JRE: %s %s",
119                         System.getProperty("java.runtime.name"),
120                         System.getProperty("java.runtime.version")
121                 )
122         );
123         console(
124                 String.format(
125                         "JVM: %s %s %s",
126                         System.getProperty("java.vm.vendor"),
127                         System.getProperty("java.vm.name"),
128                         System.getProperty("java.vm.version")
129                 )
130         );
131     }
132
133     private static String alignCenter(String message) {
134         int indent = (ASCII_LOGO[0].length() + message.length()) / 2;
135         return String.format(
136                 "%" + indent + "s",
137                 message
138         );
139     }
140
141 }