2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.io
.BufferedReader
;
32 import java
.io
.InputStreamReader
;
33 import java
.io
.IOException
;
34 import java
.util
.StringTokenizer
;
37 * TTYSessionInfo queries environment variables and the tty window size for
38 * the session information. The username is taken from user.name, language
39 * is taken from user.language, and text window size from 'stty size'.
41 public class TTYSessionInfo
implements SessionInfo
{
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
50 private String username
= "";
55 private String language
= "";
58 * Text window width. Default is 80x24 (same as VT100-ish terminals).
60 private int windowWidth
= 80;
63 * Text window height. Default is 80x24 (same as VT100-ish terminals).
65 private int windowHeight
= 24;
68 * Time at which the window size was refreshed.
70 private long lastQueryWindowTime
;
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
79 public TTYSessionInfo() {
80 // Populate lang and user from the environment
81 username
= System
.getProperty("user.name");
82 language
= System
.getProperty("user.language");
86 // ------------------------------------------------------------------------
87 // SessionInfo ------------------------------------------------------------
88 // ------------------------------------------------------------------------
93 * @return the username
95 public String
getUsername() {
102 * @param username the value
104 public void setUsername(final String username
) {
105 this.username
= username
;
111 * @return the language
113 public String
getLanguage() {
114 return this.language
;
120 * @param language the value
122 public void setLanguage(final String language
) {
123 this.language
= language
;
127 * Text window width getter.
129 * @return the window width
131 public int getWindowWidth() {
132 if (System
.getProperty("os.name").startsWith("Windows")) {
133 // Always use 80x25 for Windows (same as DOS)
140 * Text window height getter.
142 * @return the window height
144 public int getWindowHeight() {
145 if (System
.getProperty("os.name").startsWith("Windows")) {
146 // Always use 80x25 for Windows (same as DOS)
153 * Re-query the text window size.
155 public void queryWindowSize() {
156 if (lastQueryWindowTime
== 0) {
157 lastQueryWindowTime
= System
.currentTimeMillis();
159 long nowTime
= System
.currentTimeMillis();
160 if (nowTime
- lastQueryWindowTime
< 1000) {
161 // Don't re-spawn stty if it hasn't been a full second since
166 if (System
.getProperty("os.name").startsWith("Linux")
167 || System
.getProperty("os.name").startsWith("Mac OS X")
168 || System
.getProperty("os.name").startsWith("SunOS")
169 || System
.getProperty("os.name").startsWith("FreeBSD")
171 // Use stty to get the window size
176 // ------------------------------------------------------------------------
177 // TTYSessionInfo ---------------------------------------------------------
178 // ------------------------------------------------------------------------
181 * Call 'stty size' to obtain the tty window size. windowWidth and
182 * windowHeight are set automatically.
184 private void sttyWindowSize() {
186 "/bin/sh", "-c", "stty size < /dev/tty"
189 Process process
= Runtime
.getRuntime().exec(cmd
);
190 BufferedReader in
= new BufferedReader(
191 new InputStreamReader(process
.getInputStream(), "UTF-8"));
192 String line
= in
.readLine();
193 if ((line
!= null) && (line
.length() > 0)) {
194 StringTokenizer tokenizer
= new StringTokenizer(line
);
195 int rc
= Integer
.parseInt(tokenizer
.nextToken());
199 rc
= Integer
.parseInt(tokenizer
.nextToken());
205 BufferedReader err
= new BufferedReader(
206 new InputStreamReader(process
.getErrorStream(),
208 line
= err
.readLine();
209 if ((line
!= null) && (line
.length() > 0)) {
210 System
.err
.println("Error output from stty: " + line
);
215 } catch (InterruptedException e
) {
219 int rc
= process
.exitValue();
221 System
.err
.println("stty returned error code: " + rc
);
223 } catch (IOException e
) {