Skip to content

Commit

Permalink
Updated Auto-Refresh exit code
Browse files Browse the repository at this point in the history
The original approach was unreliable. Enter was picked up just fine but
Main() getting the results and existing didn't always work.  Had to do
it a bunch of time for it to exit.

This approach simply has the EnterPressed thread exit the program. Seems
to work much better but it's not very elegant.
  • Loading branch information
frossm committed Jul 27, 2023
1 parent 48008c0 commit b02cdea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>quoter</artifactId>
<version>5.0.15</version>
<version>5.0.16</version>
<packaging>jar</packaging>

<name>quoter</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: quoter
version: '5.0.15'
version: '5.0.16'
summary: Command line utility to pull stock and index quotes
description: |
Quoter fetches online stock quotes and index data for easy display on
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/org/fross/quoter/EnterPressed.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
***************************************************************************************************************/
package org.fross.quoter;

import java.io.Console;

import org.fross.library.Output;
import org.fusesource.jansi.Ansi;

public class EnterPressed extends Thread {
boolean enterPressed;

Expand All @@ -39,16 +44,20 @@ public EnterPressed() {
*/
@Override
public void run() {
// Check for a key press
// Check for entered text
try {
if (System.in.read() != -1) {
Console c = System.console();

if (c.readLine() != null) {
this.enterPressed = true;
}
Output.printColorln(Ansi.Color.CYAN, "Exiting...");

System.in.reset();
// This seems sloppy, but having Main check the status wasn't working reliably
System.exit(0);
}

} catch (Exception ex) {
// Just move on...
// Just keeping 'er movin'
}
}

Expand Down
32 changes: 11 additions & 21 deletions src/main/java/org/fross/quoter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,20 @@ public static void main(String[] args) {

// Auto-refresh is enabled. Re-display the data every cli.clAutoRefresh seconds
if (cli.clAutoRefresh > 0) {
boolean continueCountdown = true;

while (continueCountdown == true) {
// Continuous loop displaying the output until user hits enter. The EnterPressed thread picks that up and exits the program
while (true) {
int countDown = cli.clAutoRefresh;

Output.println("");

// Start a thread and look for the 'ENTER' key to be hit
// Start a thread and look for the 'ENTER' key to be hit - then exit quoter
EnterPressed ep = new EnterPressed();
ep.start();

while (countDown > 0 && continueCountdown == true) {
while (countDown > 0) {
// Erase the line. Use a large number so it hits the front of the line
System.out.print(ansi().cursorLeft(5000));

Output.printColor(Ansi.Color.RED,
Format.CenterText(88, String.format("----- Quoter auto-refreshing in %02d seconds. Press 'ENTER' to exit -----", countDown)));

Expand All @@ -233,28 +235,16 @@ public static void main(String[] args) {
Output.fatalError("Error during auto-refresh count down", 0);
}

// Erase the line. Use a large number so it hits the front of the line
System.out.print(ansi().cursorLeft(5000));

// Decrement the count down timer by 1 second
countDown--;

// Check the EnterPressed thread and see if, well, enter was pressed
if (ep.queryEnterPressed() == true) {
continueCountdown = false;
ep.interrupt();
}

}

// Clear the screen before we display the next iteration
if (continueCountdown == true) {
Output.clearScreen();
System.out.flush();
Output.clearScreen();
System.out.flush();

// Display another set of output
quoteConsoleOutput.displayOutput(exporter);
}
// Display another set of output
quoteConsoleOutput.displayOutput(exporter);

}

Expand Down

0 comments on commit b02cdea

Please sign in to comment.