11package se .mwthinker ;
22
3+ import org .jline .consoleui .elements .ConfirmChoice ;
4+ import org .jline .consoleui .prompt .ConsolePrompt ;
5+ import org .jline .consoleui .prompt .PromptResultItemIF ;
6+ import org .jline .consoleui .prompt .builder .PromptBuilder ;
37import org .jline .reader .EndOfFileException ;
48import org .jline .reader .UserInterruptException ;
9+ import org .jline .terminal .Terminal ;
10+ import org .jline .terminal .TerminalBuilder ;
511import picocli .CommandLine ;
612import picocli .CommandLine .Command ;
713import picocli .CommandLine .Option ;
814import picocli .CommandLine .Parameters ;
915
1016import java .io .*;
17+ import java .util .Map ;
1118import java .util .Properties ;
1219
13- @ SuppressWarnings ("SpellCheckingInspection" )
20+ @ SuppressWarnings ({ "SpellCheckingInspection" , "java:S106" } )
1421@ Command (name = "cppgen" ,
1522 mixinStandardHelpOptions = true ,
1623 description = "C++ generator using CMake"
@@ -38,15 +45,15 @@ public class GeneratorCli implements Closeable, Flushable {
3845 private boolean test = false ;
3946
4047 @ Option (names = { "-l" , "--license" }, paramLabel = "LICENSE" , description = "Add MIT license with author." )
41- private String author = "" ;
48+ private String licenseAuthor = "" ;
4249
4350 @ Option (names = { "-k" , "--keepFiles" }, paramLabel = "KEEPFILES" , description = "Keep generated files on error." )
4451 private boolean keepFiles = false ;
4552
4653 @ Option (names = { "-v" , "--version" }, versionHelp = true , description = "Display version info." )
4754 private boolean versionRequested = false ;
4855
49- private final ConsoleIO consoleIO ;
56+ private Terminal terminal ;
5057
5158 static void main (String [] args ) {
5259 try (GeneratorCli generatorCli = new GeneratorCli ()) {
@@ -59,15 +66,28 @@ static void main(String[] args) {
5966 }
6067
6168 public GeneratorCli () throws IOException {
62- consoleIO = new ConsoleIO ();
69+ terminal = null ;
70+ try {
71+ terminal = TerminalBuilder .builder ()
72+ .system (true )
73+ .dumb (true )
74+ .build ();
75+ } catch (IOException e ) {
76+ throw new RuntimeException (e );
77+ }
6378 }
6479
6580 public void run (String [] args ) {
81+ ConsolePrompt prompt = new ConsolePrompt (terminal );
82+ PromptBuilder builder = prompt .getPromptBuilder ();
83+
6684 var commandLine = new CommandLine (this );
6785 try {
6886 commandLine .parseArgs (args );
6987 } catch (CommandLine .ParameterException e ) {
70- consoleIO .printError ("Argument error: " + e .getMessage ());
88+ builder .createText ()
89+ .addLine ("Argument error: " + e .getMessage ())
90+ .addPrompt ();
7191 System .exit (2 );
7292 }
7393
@@ -80,7 +100,7 @@ public void run(String[] args) {
80100 }
81101
82102 if (projectDir == null ) {
83- interactivePrompt (consoleIO );
103+ interactivePrompt (prompt , builder );
84104 }
85105 int exitCode = executeGeneratorLogic ();
86106 if (exitCode == 2 ) {
@@ -89,55 +109,82 @@ public void run(String[] args) {
89109 System .exit (exitCode );
90110 }
91111
92- private void interactivePrompt (ConsoleIO consoleIO ) {
93- String projectName = "" ;
94- while (projectName .trim ().isEmpty ()) {
95- projectName = consoleIO .askQuestion ("Enter project name: " );
96- if (projectName .trim ().isEmpty ()) {
97- consoleIO .printError ("Project name cannot be empty." );
98- }
99- }
100- projectDir = new File (projectName .trim ());
101-
102- String desc = consoleIO .askQuestion ( "Enter project description " , "(or press Enter for default 'Description')" );
103- if (desc != null && !desc .trim ().isEmpty ()) {
104- description = desc .trim ();
105- }
106-
107- gui = consoleIO .askYesNoQuestion ("Add GUI library?" );
108- test = consoleIO .askYesNoQuestion ("Add tests?" );
109-
110- String authorInput = consoleIO .askQuestion ("Enter author name for MIT license " , "(or press Enter to skip)" );
111- if (authorInput != null && !authorInput .trim ().isEmpty ()) {
112- author = authorInput .trim ();
113- }
114-
115- cmake = consoleIO .askYesNoQuestion ("Run cmake after generation?" );
116- if (cmake ) {
117- open = consoleIO .askYesNoQuestion ("Open Visual Studio solution?" );
112+ private void interactivePrompt (ConsolePrompt prompt , PromptBuilder builder ) {
113+ builder .createInputPrompt ()
114+ .name ("projectName" )
115+ .message ("Enter project name: " )
116+ .addPrompt ()
117+ .createInputPrompt ()
118+ .name ("description" )
119+ .message ("Enter project description (or press Enter for default 'Description'): " )
120+ .defaultValue ("Description" )
121+ .addPrompt ()
122+ .createConfirmPromp ()
123+ .name ("gui" )
124+ .message ("Add GUI library?" )
125+ .defaultValue (ConfirmChoice .ConfirmationValue .NO )
126+ .addPrompt ()
127+ .createConfirmPromp ()
128+ .name ("test" )
129+ .message ("Add tests?" )
130+ .defaultValue (ConfirmChoice .ConfirmationValue .NO )
131+ .addPrompt ()
132+ .createInputPrompt ()
133+ .name ("licenseAuthor" )
134+ .defaultValue ("" )
135+ .message ("Enter author name for MIT license (or press Enter to skip): " )
136+ .addPrompt ()
137+ .createConfirmPromp ()
138+ .name ("cmake" )
139+ .message ("Run cmake after generation?" )
140+ .defaultValue (ConfirmChoice .ConfirmationValue .NO )
141+ .addPrompt ()
142+ .createConfirmPromp ()
143+ .name ("open" )
144+ .message ("Open Visual Studio solution?" )
145+ .defaultValue (ConfirmChoice .ConfirmationValue .NO )
146+ .addPrompt ()
147+ .createConfirmPromp ()
148+ .name ("verbose" )
149+ .message ("Show verbose output?" )
150+ .defaultValue (ConfirmChoice .ConfirmationValue .NO )
151+ .addPrompt ();
152+
153+ Map <String , PromptResultItemIF > result ;
154+ try {
155+ result = prompt .prompt (builder .build ());
156+ } catch (IOException e ) {
157+ throw new RuntimeException (e );
118158 }
119159
120- verbose = consoleIO .askYesNoQuestion ("Show verbose output?" );
160+ projectDir = new File (result .get ("projectName" ).toString ());
161+ description = result .get ("description" ).toString ();
162+ gui = result .get ("gui" ).toString ().equalsIgnoreCase ("yes" );
163+ test = result .get ("test" ).toString ().equalsIgnoreCase ("yes" );
164+ licenseAuthor = result .get ("licenseAuthor" ).toString ();
165+ cmake = result .get ("cmake" ).toString ().equalsIgnoreCase ("yes" );
166+ open = result .get ("open" ).toString ().equalsIgnoreCase ("yes" );
167+ verbose = result .get ("verbose" ).toString ().equalsIgnoreCase ("yes" );
121168 }
122169
123170 private int executeGeneratorLogic () {
124171 if (projectDir .exists () || !projectDir .mkdir ()) {
125- consoleIO . printError ("Failed to create project folder: " + projectDir .getAbsolutePath ());
172+ System . out . println ("Failed to create project folder: " + projectDir .getAbsolutePath ());
126173 return 1 ;
127174 }
128175 if (!new File (projectDir , "data" ).mkdir ()) {
129- consoleIO . printError ("Failed to create data folder" );
176+ System . out . println ("Failed to create data folder" );
130177 return 1 ;
131178 }
132179
133- consoleIO . printError ("Generating project in: " + projectDir .getName ());
180+ System . out . println ("Generating project in: " + projectDir .getName ());
134181 FileSystem fileSystem = new FileSystem (projectDir , createResourceHandler ());
135182 fileSystem .setVerbose (verbose );
136183
137184 CMakeBuilder cmakeBuilder = new CMakeBuilder (fileSystem , new Github ())
138185 .withDescription (description )
139186 .withTestProject (test )
140- .withLicense (LicenseType .MIT , author );
187+ .withLicense (LicenseType .MIT , licenseAuthor );
141188
142189 if (gui ) {
143190 cmakeBuilder
@@ -157,10 +204,10 @@ private int executeGeneratorLogic() {
157204 cmakeBuilder .buildFiles ();
158205 } catch (RuntimeException e ) {
159206 if (!keepFiles ) {
160- consoleIO . printError ("Cleaning up generated files due to error." );
207+ System . out . println ("Cleaning up generated files due to error." );
161208 fileSystem .deleteProjectDir ();
162209 }
163- consoleIO . printError ("Error during project generation: " + e .getMessage ());
210+ System . out . println ("Error during project generation: " + e .getMessage ());
164211 return 1 ;
165212 }
166213
@@ -174,7 +221,7 @@ private int executeGeneratorLogic() {
174221 }
175222 }
176223
177- consoleIO . printError ("Project generation complete." );
224+ System . out . println ("Project generation complete." );
178225 return 0 ;
179226 }
180227
@@ -192,17 +239,17 @@ public void printVersion() {
192239 } catch (IOException e ) {
193240 throw new RuntimeException (e );
194241 }
195- consoleIO . printInfo ("Version info: v" + properties .getProperty ("version" ));
242+ System . out . println ("Version info: v" + properties .getProperty ("version" ));
196243 }
197244
198245 @ Override
199246 public void close () throws IOException {
200- consoleIO .close ();
247+ terminal .close ();
201248 }
202249
203250 @ Override
204251 public void flush () throws IOException {
205- consoleIO .flush ();
252+ terminal .flush ();
206253 }
207254
208255}
0 commit comments