2 * Jexer - Java Text User Interface
6 * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a>
8 * License: LGPLv3 or later
10 * Copyright: This module is licensed under the GNU Lesser General
11 * Public License Version 3. Please see the file "COPYING" in this
12 * directory for more information about the GNU Lesser General Public
15 * Copyright (C) 2015 Kevin Lamonte
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this program; if not, see
29 * http://www.gnu.org/licenses/, or write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
33 package jexer
.session
;
35 import java
.io
.BufferedReader
;
36 import java
.io
.InputStreamReader
;
37 import java
.io
.IOException
;
38 import java
.util
.Date
;
39 import java
.util
.StringTokenizer
;
42 * TTYSessionInfo queries environment variables and the tty window size for
43 * the session information. The username is taken from
44 * getpwuid(geteuid()).pw_name, language is taken from LANG, and text window
45 * size from ioctl(TIOCGWINSIZ).
47 public class TTYSessionInfo
implements SessionInfo
{
52 private String username
= "";
57 private String language
= "";
60 * Text window width. Default is 80x24 (same as VT100-ish terminals).
62 private int windowWidth
= 80;
65 * Text window height. Default is 80x24 (same as VT100-ish terminals).
67 private int windowHeight
= 24;
70 * Time at which the window size was refreshed
72 private Date lastQueryWindowTime
;
77 * @return the username
79 public String
getUsername() {
86 * @param username the value
88 public void setUsername(String username
) {
89 this.username
= username
;
95 * @return the language
97 public String
getLanguage() {
104 * @param language the value
106 public void setLanguage(String language
) {
107 this.language
= language
;
111 * Call 'stty size' to obtain the tty window size. windowWidth and
112 * windowHeight are set automatically.
114 private void sttyWindowSize() {
116 "/bin/sh", "-c", "stty size < /dev/tty"
119 Process process
= Runtime
.getRuntime().exec(cmd
);
120 BufferedReader in
= new BufferedReader(new InputStreamReader(process
.getInputStream(), "UTF-8"));
121 String line
= in
.readLine();
122 if ((line
!= null) && (line
.length() > 0)) {
123 StringTokenizer tokenizer
= new StringTokenizer(line
);
124 windowHeight
= Integer
.parseInt(tokenizer
.nextToken());
125 windowWidth
= Integer
.parseInt(tokenizer
.nextToken());
128 BufferedReader err
= new BufferedReader(new InputStreamReader(process
.getErrorStream(), "UTF-8"));
129 line
= err
.readLine();
130 if ((line
!= null) && (line
.length() > 0)) {
131 System
.err
.println("Error output from stty: " + line
);
136 } catch (InterruptedException e
) {
140 int rc
= process
.exitValue();
142 System
.err
.println("stty returned error code: " + rc
);
144 } catch (IOException e
) {
150 * Text window width getter
152 * @return the window width
154 public int getWindowWidth() {
155 if (System
.getProperty("os.name").startsWith("Windows")) {
156 // Always use 80x25 for Windows (same as DOS)
163 * Text window height getter
165 * @return the window height
167 public int getWindowHeight() {
168 if (System
.getProperty("os.name").startsWith("Windows")) {
169 // Always use 80x25 for Windows (same as DOS)
176 * Re-query the text window size
178 public void queryWindowSize() {
179 if (lastQueryWindowTime
== null) {
180 lastQueryWindowTime
= new Date();
182 Date now
= new Date();
183 if (now
.getTime() - lastQueryWindowTime
.getTime() < 3000) {
184 // Don't re-spawn stty, it's been too soon.
188 if (System
.getProperty("os.name").startsWith("Linux") ||
189 System
.getProperty("os.name").startsWith("Mac OS X") ||
190 System
.getProperty("os.name").startsWith("SunOS") ||
191 System
.getProperty("os.name").startsWith("FreeBSD")
193 // Use stty to get the window size
201 public TTYSessionInfo() {
202 // Populate lang and user from the environment
203 username
= System
.getProperty("user.name");
204 language
= System
.getProperty("user.language");