2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
.Date
;
35 import java
.util
.StringTokenizer
;
38 * TTYSessionInfo queries environment variables and the tty window size for
39 * the session information. The username is taken from user.name, language
40 * is taken from user.language, and text window size from 'stty size'.
42 public final class TTYSessionInfo
implements SessionInfo
{
47 private String username
= "";
52 private String language
= "";
55 * Text window width. Default is 80x24 (same as VT100-ish terminals).
57 private int windowWidth
= 80;
60 * Text window height. Default is 80x24 (same as VT100-ish terminals).
62 private int windowHeight
= 24;
65 * Time at which the window size was refreshed.
67 private Date lastQueryWindowTime
;
72 * @return the username
74 public String
getUsername() {
81 * @param username the value
83 public void setUsername(final String username
) {
84 this.username
= username
;
90 * @return the language
92 public String
getLanguage() {
99 * @param language the value
101 public void setLanguage(final String language
) {
102 this.language
= language
;
106 * Call 'stty size' to obtain the tty window size. windowWidth and
107 * windowHeight are set automatically.
109 private void sttyWindowSize() {
111 "/bin/sh", "-c", "stty size < /dev/tty"
114 Process process
= Runtime
.getRuntime().exec(cmd
);
115 BufferedReader in
= new BufferedReader(
116 new InputStreamReader(process
.getInputStream(), "UTF-8"));
117 String line
= in
.readLine();
118 if ((line
!= null) && (line
.length() > 0)) {
119 StringTokenizer tokenizer
= new StringTokenizer(line
);
120 int rc
= Integer
.parseInt(tokenizer
.nextToken());
124 rc
= Integer
.parseInt(tokenizer
.nextToken());
130 BufferedReader err
= new BufferedReader(
131 new InputStreamReader(process
.getErrorStream(),
133 line
= err
.readLine();
134 if ((line
!= null) && (line
.length() > 0)) {
135 System
.err
.println("Error output from stty: " + line
);
140 } catch (InterruptedException e
) {
144 int rc
= process
.exitValue();
146 System
.err
.println("stty returned error code: " + rc
);
148 } catch (IOException e
) {
154 * Text window width getter.
156 * @return the window width
158 public int getWindowWidth() {
159 if (System
.getProperty("os.name").startsWith("Windows")) {
160 // Always use 80x25 for Windows (same as DOS)
167 * Text window height getter.
169 * @return the window height
171 public int getWindowHeight() {
172 if (System
.getProperty("os.name").startsWith("Windows")) {
173 // Always use 80x25 for Windows (same as DOS)
180 * Re-query the text window size.
182 public void queryWindowSize() {
183 if (lastQueryWindowTime
== null) {
184 lastQueryWindowTime
= new Date();
186 Date now
= new Date();
187 if (now
.getTime() - lastQueryWindowTime
.getTime() < 3000) {
188 // Don't re-spawn stty, it's been too soon.
192 if (System
.getProperty("os.name").startsWith("Linux")
193 || System
.getProperty("os.name").startsWith("Mac OS X")
194 || System
.getProperty("os.name").startsWith("SunOS")
195 || System
.getProperty("os.name").startsWith("FreeBSD")
197 // Use stty to get the window size
203 * Public constructor.
205 public TTYSessionInfo() {
206 // Populate lang and user from the environment
207 username
= System
.getProperty("user.name");
208 language
= System
.getProperty("user.language");