LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / session / TTYSessionInfo.java
index 2236ac3e0bf904f7f474269fc46babe096f4ea19..91a83674384a64a52256a0cfd30cdb65eb074274 100644 (file)
@@ -1,34 +1,30 @@
-/**
+/*
  * Jexer - Java Text User Interface
  *
- * Version: $Id$
- *
- * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a>
- *
- * License: LGPLv3 or later
+ * The MIT License (MIT)
  *
- * Copyright: This module is licensed under the GNU Lesser General
- * Public License Version 3.  Please see the file "COPYING" in this
- * directory for more information about the GNU Lesser General Public
- * License Version 3.
+ * Copyright (C) 2016 Kevin Lamonte
  *
- *     Copyright (C) 2015  Kevin Lamonte
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 3 of
- * the License, or (at your option) any later version.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, see
- * http://www.gnu.org/licenses/, or write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
+ * @author Kevin Lamonte [kevin.lamonte@gmail.com]
+ * @version 1
  */
 package jexer.session;
 
@@ -40,19 +36,18 @@ import java.util.StringTokenizer;
 
 /**
  * TTYSessionInfo queries environment variables and the tty window size for
- * the session information.  The username is taken from
- * getpwuid(geteuid()).pw_name, language is taken from LANG, and text window
- * size from ioctl(TIOCGWINSIZ).
+ * the session information.  The username is taken from user.name, language
+ * is taken from user.language, and text window size from 'stty size'.
  */
-public class TTYSessionInfo implements SessionInfo {
+public final class TTYSessionInfo implements SessionInfo {
 
     /**
-     * User name
+     * User name.
      */
     private String username = "";
 
     /**
-     * Language
+     * Language.
      */
     private String language = "";
 
@@ -67,44 +62,44 @@ public class TTYSessionInfo implements SessionInfo {
     private int windowHeight = 24;
 
     /**
-     * Time at which the window size was refreshed
+     * Time at which the window size was refreshed.
      */
     private Date lastQueryWindowTime;
-    
+
     /**
-     * Username getter
+     * Username getter.
      *
      * @return the username
      */
     public String getUsername() {
-       return this.username;
+        return this.username;
     }
 
     /**
-     * Username setter
+     * Username setter.
      *
      * @param username the value
      */
-    public void setUsername(String username) {
-       this.username = username;
+    public void setUsername(final String username) {
+        this.username = username;
     }
 
     /**
-     * Language getter
+     * Language getter.
      *
      * @return the language
      */
     public String getLanguage() {
-       return this.language;
+        return this.language;
     }
 
     /**
-     * Language setter
+     * Language setter.
      *
      * @param language the value
      */
-    public void setLanguage(String language) {
-       this.language = language;
+    public void setLanguage(final String language) {
+        this.language = language;
     }
 
     /**
@@ -112,96 +107,105 @@ public class TTYSessionInfo implements SessionInfo {
      * windowHeight are set automatically.
      */
     private void sttyWindowSize() {
-       String [] cmd = {
-           "/bin/sh", "-c", "stty size < /dev/tty"
-       };
-       try {
-           Process process = Runtime.getRuntime().exec(cmd);
-           BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
-           String line = in.readLine();
-           if ((line != null) && (line.length() > 0)) {
-               StringTokenizer tokenizer = new StringTokenizer(line);
-               windowHeight = Integer.parseInt(tokenizer.nextToken());
-               windowWidth = Integer.parseInt(tokenizer.nextToken());
-           }
-           while (true) {
-               BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
-               line = err.readLine();
-               if ((line != null) && (line.length() > 0)) {
-                   System.err.println("Error output from stty: " + line);
-               }
-               try{
-                   process.waitFor();
-                   break;
-               } catch (InterruptedException e) {
-                   e.printStackTrace();
-               }
-           }
-           int rc = process.exitValue();
-           if (rc != 0) {
-               System.err.println("stty returned error code: " + rc);
-           }
-       } catch (IOException e) {
-           e.printStackTrace();
-       }
+        String [] cmd = {
+            "/bin/sh", "-c", "stty size < /dev/tty"
+        };
+        try {
+            Process process = Runtime.getRuntime().exec(cmd);
+            BufferedReader in = new BufferedReader(
+                new InputStreamReader(process.getInputStream(), "UTF-8"));
+            String line = in.readLine();
+            if ((line != null) && (line.length() > 0)) {
+                StringTokenizer tokenizer = new StringTokenizer(line);
+                int rc = Integer.parseInt(tokenizer.nextToken());
+                if (rc > 0) {
+                    windowHeight = rc;
+                }
+                rc = Integer.parseInt(tokenizer.nextToken());
+                if (rc > 0) {
+                    windowWidth = rc;
+                }
+            }
+            while (true) {
+                BufferedReader err = new BufferedReader(
+                        new InputStreamReader(process.getErrorStream(),
+                            "UTF-8"));
+                line = err.readLine();
+                if ((line != null) && (line.length() > 0)) {
+                    System.err.println("Error output from stty: " + line);
+                }
+                try {
+                    process.waitFor();
+                    break;
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            int rc = process.exitValue();
+            if (rc != 0) {
+                System.err.println("stty returned error code: " + rc);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
     }
-    
+
     /**
-     * Text window width getter
+     * Text window width getter.
      *
      * @return the window width
      */
     public int getWindowWidth() {
-       if (System.getProperty("os.name").startsWith("Windows")) {
-           // Always use 80x25 for Windows (same as DOS)
-           return 80;
-       }
-       return windowWidth;
+        if (System.getProperty("os.name").startsWith("Windows")) {
+            // Always use 80x25 for Windows (same as DOS)
+            return 80;
+        }
+        return windowWidth;
     }
 
     /**
-     * Text window height getter
+     * Text window height getter.
      *
      * @return the window height
      */
     public int getWindowHeight() {
-       if (System.getProperty("os.name").startsWith("Windows")) {
-           // Always use 80x25 for Windows (same as DOS)
-           return 25;
-       }
-       return windowHeight;
+        if (System.getProperty("os.name").startsWith("Windows")) {
+            // Always use 80x25 for Windows (same as DOS)
+            return 25;
+        }
+        return windowHeight;
     }
 
     /**
-     * Re-query the text window size
+     * Re-query the text window size.
      */
     public void queryWindowSize() {
-       if (lastQueryWindowTime == null) {
-           lastQueryWindowTime = new Date();
-       } else {
-           Date now = new Date();
-           if (now.getTime() - lastQueryWindowTime.getTime() < 3000) {
-               // Don't re-spawn stty, it's been too soon.
-               return;
-           }
-       }
-       if (System.getProperty("os.name").startsWith("Linux") ||
-           System.getProperty("os.name").startsWith("Mac OS X") ||
-           System.getProperty("os.name").startsWith("SunOS") ||
-           System.getProperty("os.name").startsWith("FreeBSD")
-       ) {
-           // Use stty to get the window size
-           sttyWindowSize();
-       }
+        if (lastQueryWindowTime == null) {
+            lastQueryWindowTime = new Date();
+        } else {
+            Date now = new Date();
+            if (now.getTime() - lastQueryWindowTime.getTime() < 3000) {
+                // Don't re-spawn stty, it's been too soon.
+                return;
+            }
+        }
+        if (System.getProperty("os.name").startsWith("Linux")
+            || System.getProperty("os.name").startsWith("Mac OS X")
+            || System.getProperty("os.name").startsWith("SunOS")
+            || System.getProperty("os.name").startsWith("FreeBSD")
+        ) {
+            // Use stty to get the window size
+            sttyWindowSize();
+        }
     }
 
     /**
-     * Public constructor
+     * Public constructor.
      */
     public TTYSessionInfo() {
-       // Populate lang and user from the environment
-       username = System.getProperty("user.name");
-       language = System.getProperty("user.language");
-       queryWindowSize();
+        // Populate lang and user from the environment
+        username = System.getProperty("user.name");
+        language = System.getProperty("user.language");
+        queryWindowSize();
     }
 }