View Javadoc
1   package com.guinetik.hexafun.examples;
2   
3   import com.guinetik.hexafun.examples.counter.CounterApp;
4   import com.guinetik.hexafun.examples.sysmon.SysmonTUI;
5   import com.guinetik.hexafun.examples.tasks.tui.TasksTUI;
6   
7   import java.io.BufferedReader;
8   import java.io.InputStreamReader;
9   
10  import static com.guinetik.hexafun.examples.tui.Ansi.*;
11  
12  /**
13   * Interactive launcher for HexaFun examples.
14   *
15   * <p>Run with: {@code mvn exec:java -pl hexafun-examples}</p>
16   */
17  public class ExamplesLauncher {
18  
19      private static final String LOGO = """
20  
21                ╦ ╦┌─┐─┐ ┬┌─┐╔═╗┬ ┬┌┐┌
22                ╠═╣├┤ ┌┴┬┘├─┤╠╣ │ ││││
23                ╩ ╩└─┘┴ └─┴ ┴╚  └─┘┘└┘
24              """;
25  
26      private static final String MENU = """
27  
28                %s Examples %s
29  
30                %s1%s  Counter    Simple increment/add with validation
31                %s2%s  Tasks      Kanban board TUI (TODO → DOING → DONE)
32                %s3%s  Sysmon     System monitor with multiple output formats
33  
34                %sq%s  Quit
35  
36              """.formatted(
37              color("─────", DIM), color("─────", DIM),
38              color("[", DIM) + color("1", CYAN) + color("]", DIM), "",
39              color("[", DIM) + color("2", CYAN) + color("]", DIM), "",
40              color("[", DIM) + color("3", CYAN) + color("]", DIM), "",
41              color("[", DIM) + color("q", YELLOW) + color("]", DIM), ""
42      );
43  
44      public static void main(String[] args) {
45          // If an argument is passed, launch that example directly
46          if (args.length > 0) {
47              launchByName(args[0]);
48              return;
49          }
50  
51          // Interactive menu
52          var reader = new BufferedReader(new InputStreamReader(System.in));
53  
54          while (true) {
55              System.out.print(CLEAR_SCREEN + CURSOR_HOME);
56              System.out.println(color(LOGO, CYAN));
57              System.out.println(MENU);
58              System.out.print(color("  Select example: ", BOLD));
59  
60              try {
61                  String input = reader.readLine();
62                  if (input == null || input.equalsIgnoreCase("q")) {
63                      System.out.println(color("\n  Goodbye!\n", DIM));
64                      break;
65                  }
66  
67                  switch (input.trim()) {
68                      case "1", "counter" -> {
69                          System.out.println(color("\n  Launching Counter...\n", GREEN));
70                          CounterApp.main(new String[]{});
71                          pause(reader);
72                      }
73                      case "2", "tasks" -> {
74                          System.out.println(color("\n  Launching Tasks TUI...\n", GREEN));
75                          TasksTUI.main(new String[]{});
76                      }
77                      case "3", "sysmon" -> {
78                          System.out.println(color("\n  Launching System Monitor...\n", GREEN));
79                          new SysmonTUI().run();
80                      }
81                      default -> {
82                          System.out.println(color("\n  Unknown option: " + input, RED));
83                          pause(reader);
84                      }
85                  }
86              } catch (Exception e) {
87                  System.out.println(color("\n  Error: " + e.getMessage(), RED));
88              }
89          }
90      }
91  
92      private static void launchByName(String name) {
93          switch (name.toLowerCase()) {
94              case "counter", "1" -> CounterApp.main(new String[]{});
95              case "tasks", "2" -> TasksTUI.main(new String[]{});
96              case "sysmon", "3" -> new SysmonTUI().run();
97              default -> {
98                  System.out.println("Unknown example: " + name);
99                  System.out.println("Available: counter, tasks, sysmon");
100             }
101         }
102     }
103 
104     private static void pause(BufferedReader reader) {
105         System.out.print(color("\n  Press Enter to continue...", DIM));
106         try {
107             reader.readLine();
108         } catch (Exception ignored) {}
109     }
110 }