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 System
.out
.println("spawn stty");
121 Process process
= Runtime
.getRuntime().exec(cmd
);
122 BufferedReader in
= new BufferedReader(new InputStreamReader(process
.getInputStream(), "UTF-8"));
123 String line
= in
.readLine();
124 if ((line
!= null) && (line
.length() > 0)) {
125 StringTokenizer tokenizer
= new StringTokenizer(line
);
126 windowHeight
= Integer
.parseInt(tokenizer
.nextToken());
127 windowWidth
= Integer
.parseInt(tokenizer
.nextToken());
130 BufferedReader err
= new BufferedReader(new InputStreamReader(process
.getErrorStream(), "UTF-8"));
131 line
= err
.readLine();
132 if ((line
!= null) && (line
.length() > 0)) {
133 System
.err
.println("Error output from stty: " + line
);
138 } catch (InterruptedException e
) {
142 int rc
= process
.exitValue();
144 System
.err
.println("stty returned error code: " + rc
);
146 } catch (IOException e
) {
152 * Text window width getter
154 * @return the window width
156 public int getWindowWidth() {
157 if (System
.getProperty("os.name").startsWith("Windows")) {
158 // Always use 80x25 for Windows (same as DOS)
165 * Text window height getter
167 * @return the window height
169 public int getWindowHeight() {
170 if (System
.getProperty("os.name").startsWith("Windows")) {
171 // Always use 80x25 for Windows (same as DOS)
178 * Re-query the text window size
180 public void queryWindowSize() {
181 if (lastQueryWindowTime
== null) {
182 lastQueryWindowTime
= new Date();
184 Date now
= new Date();
185 if (now
.getTime() - lastQueryWindowTime
.getTime() < 3000) {
186 // Don't re-spawn stty, it's been too soon.
190 if (System
.getProperty("os.name").startsWith("Linux") ||
191 System
.getProperty("os.name").startsWith("Mac OS X") ||
192 System
.getProperty("os.name").startsWith("SunOS") ||
193 System
.getProperty("os.name").startsWith("FreeBSD")
195 // Use stty to get the window size
203 public TTYSessionInfo() {
204 // Populate lang and user from the environment
205 username
= System
.getProperty("user.name");
206 language
= System
.getProperty("user.language");