X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FTTYSessionInfo.java;h=d7f5bc8e246d32da560da62c7e02886e7ddf1971;hb=505be508ae7d3fb48122be548b310a238cfb91eb;hp=e69fc7018664b4c25515040c55aedc46d2501c04;hpb=42873e30bf487bc0b695d60652dba44f82185dbb;p=fanfix.git diff --git a/src/jexer/backend/TTYSessionInfo.java b/src/jexer/backend/TTYSessionInfo.java index e69fc70..d7f5bc8 100644 --- a/src/jexer/backend/TTYSessionInfo.java +++ b/src/jexer/backend/TTYSessionInfo.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -31,7 +31,6 @@ package jexer.backend; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; -import java.util.Date; import java.util.StringTokenizer; /** @@ -39,7 +38,11 @@ import java.util.StringTokenizer; * the session information. The username is taken from user.name, language * is taken from user.language, and text window size from 'stty size'. */ -public final class TTYSessionInfo implements SessionInfo { +public class TTYSessionInfo implements SessionInfo { + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * User name. @@ -64,7 +67,25 @@ public final class TTYSessionInfo implements SessionInfo { /** * Time at which the window size was refreshed. */ - private Date lastQueryWindowTime; + private long lastQueryWindowTime; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Public constructor. + */ + public TTYSessionInfo() { + // Populate lang and user from the environment + username = System.getProperty("user.name"); + language = System.getProperty("user.language"); + queryWindowSize(); + } + + // ------------------------------------------------------------------------ + // SessionInfo ------------------------------------------------------------ + // ------------------------------------------------------------------------ /** * Username getter. @@ -102,54 +123,6 @@ public final class TTYSessionInfo implements SessionInfo { this.language = language; } - /** - * Call 'stty size' to obtain the tty window size. windowWidth and - * 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); - 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. * @@ -180,12 +153,13 @@ public final class TTYSessionInfo implements SessionInfo { * Re-query the text window size. */ public void queryWindowSize() { - if (lastQueryWindowTime == null) { - lastQueryWindowTime = new Date(); + if (lastQueryWindowTime == 0) { + lastQueryWindowTime = System.currentTimeMillis(); } else { - Date now = new Date(); - if (now.getTime() - lastQueryWindowTime.getTime() < 3000) { - // Don't re-spawn stty, it's been too soon. + long nowTime = System.currentTimeMillis(); + if (nowTime - lastQueryWindowTime < 1000) { + // Don't re-spawn stty if it hasn't been a full second since + // the last time. return; } } @@ -199,13 +173,56 @@ public final class TTYSessionInfo implements SessionInfo { } } + // ------------------------------------------------------------------------ + // TTYSessionInfo --------------------------------------------------------- + // ------------------------------------------------------------------------ + /** - * Public constructor. + * Call 'stty size' to obtain the tty window size. windowWidth and + * windowHeight are set automatically. */ - public TTYSessionInfo() { - // Populate lang and user from the environment - username = System.getProperty("user.name"); - language = System.getProperty("user.language"); - queryWindowSize(); + 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); + 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) { + // SQUASH + } + } + int rc = process.exitValue(); + if (rc != 0) { + System.err.println("stty returned error code: " + rc); + } + } catch (IOException e) { + e.printStackTrace(); + } } + }