From 1f0b9981d719153298ed5a434b64ab35f8d6e9ee Mon Sep 17 00:00:00 2001 From: zhandao Date: Tue, 12 Mar 2024 15:40:48 +0800 Subject: [PATCH 1/2] Underline the keywords in the questions Co-authored-by: zhandao --- .gitignore | 1 + lib/nextgen/commands/create.rb | 56 ++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 4e0f123..ccdde23 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /spec/reports/ /tmp/ /Gemfile.lock +/.idea/ diff --git a/lib/nextgen/commands/create.rb b/lib/nextgen/commands/create.rb index 2fce5d9..665d454 100644 --- a/lib/nextgen/commands/create.rb +++ b/lib/nextgen/commands/create.rb @@ -5,6 +5,7 @@ require "open3" require "tmpdir" require "tty-prompt" +require "rainbow" require "nextgen/ext/prompt/list" require "nextgen/ext/prompt/multilist" @@ -26,9 +27,9 @@ def run # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity say <<~BANNER Welcome to nextgen, the interactive Rails app generator! - You are about to create a Rails app named "#{app_name}" in the following directory: + You are about to create a Rails app named "#{cyan(app_name)}" in the following directory: - #{app_path} + #{cyan(app_path)} You'll be asked ~10 questions about database, test framework, and other options. The standard Rails "omakase" experience will be selected by default. @@ -83,9 +84,9 @@ def run # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity say <<~DONE.gsub(/^/, " ") - #{set_color("Done!", :green)} + #{green("Done!")} - A Rails #{rails_version} app was generated in #{set_color(app_path, :cyan)}. + A Rails #{rails_version} app was generated in #{cyan(app_path)}. Run #{set_color("bin/setup", :yellow)} in that directory to get started. @@ -99,7 +100,7 @@ def run # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity def_delegators :shell, :say, :set_color def continue_if(question) - if prompt.yes?(question) + if prompt.yes?("#{question} ↵") say else say "Canceled", :red @@ -121,7 +122,7 @@ def rails_version def ask_rails_version selected = prompt.select( - "What version of Rails will you use?", + "What #{underline("version")} of Rails will you use?", Rails.version => :current, "edge (#{Rails.edge_branch} branch)" => :edge ) @@ -129,24 +130,21 @@ def ask_rails_version end def ask_database - common_databases = { + databases = { "SQLite3 (default)" => "sqlite3", "PostgreSQL (recommended)" => "postgresql", - "MySQL" => "mysql" - } - all_databases = common_databases.merge( - %w[MySQL Trilogy Oracle SQLServer JDBCMySQL JDBCSQLite3 JDBCPostgreSQL JDBC].to_h do |name| + **%w[MySQL Trilogy Oracle SQLServer JDBCMySQL JDBCSQLite3 JDBCPostgreSQL JDBC].to_h do |name| [name, name.downcase] end, "None (disable Active Record)" => nil + } + rails_opts.database = prompt_select( + "Which #{underline("database")}?", databases ) - rails_opts.database = - prompt.select("Which database?", common_databases.merge("More options..." => false)) || - prompt.select("Which database?", all_databases) end def ask_full_stack_or_api - api = prompt.select( + api = prompt_select( "What style of Rails app do you need?", "Standard, full-stack Rails (default)" => false, "API only" => true @@ -155,12 +153,13 @@ def ask_full_stack_or_api end def ask_frontend_management - frontend = prompt.select( - "How will you manage frontend assets?", + frontend = prompt_select( + "How will you manage frontend #{underline("assets")}?", "Sprockets (default)" => "sprockets", "Propshaft" => "propshaft", "Vite" => :vite ) + if frontend == :vite rails_opts.asset_pipeline = nil rails_opts.javascript = "vite" @@ -170,8 +169,8 @@ def ask_frontend_management end def ask_css - rails_opts.css = prompt.select( - "Which CSS framework will you use with the asset pipeline?", + rails_opts.css = prompt_select( + "Which #{underline("CSS")} framework will you use with the asset pipeline?", "None (default)" => nil, "Bootstrap" => "bootstrap", "Bulma" => "bulma", @@ -182,8 +181,8 @@ def ask_css end def ask_javascript - rails_opts.javascript = prompt.select( - "Which JavaScript bundler will you use with the asset pipeline?", + rails_opts.javascript = prompt_select( + "Which #{underline("JavaScript")} bundler will you use with the asset pipeline?", "Importmap (default)" => "importmap", "Bun" => "bun", "ESBuild" => "esbuild", @@ -214,7 +213,7 @@ def ask_rails_frameworks end answers = prompt.multi_select( - "Which optional Rails frameworks do you need?", + "Which optional Rails #{underline("frameworks")} do you need?", frameworks, default: frameworks.keys.reverse ) @@ -223,8 +222,8 @@ def ask_rails_frameworks end def ask_test_framework - rails_opts.test_framework = prompt.select( - "Which test framework will you use?", + rails_opts.test_framework = prompt_select( + "Which #{underline("test")} framework will you use?", "Minitest (default)" => "minitest", "RSpec" => "rspec", "None" => nil @@ -232,8 +231,8 @@ def ask_test_framework end def ask_system_testing - system_testing = prompt.select( - "Include system testing (capybara)?", + system_testing = prompt_select( + "Include #{underline("system testing")} (capybara)?", "Yes (default)" => true, "No" => false ) @@ -309,5 +308,10 @@ def prompt def shell @shell ||= Thor::Base.shell.new end + + def green(string) = set_color(string, :green) + def cyan(string) = set_color(string, :cyan) + def underline(string) = Rainbow(string).underline + def prompt_select(question, choices) = prompt.select(question, choices, enum: ".", cycle: true) end end From b60c657fc4af8e4669994d75080e8c0bb76bdd99 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Thu, 11 Apr 2024 17:26:09 -0700 Subject: [PATCH 2/2] Reduce proposed changes to just colors and select :cycle option --- .gitignore | 1 - lib/nextgen/commands/create.rb | 42 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index ccdde23..4e0f123 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,3 @@ /spec/reports/ /tmp/ /Gemfile.lock -/.idea/ diff --git a/lib/nextgen/commands/create.rb b/lib/nextgen/commands/create.rb index 665d454..33d5f3b 100644 --- a/lib/nextgen/commands/create.rb +++ b/lib/nextgen/commands/create.rb @@ -5,7 +5,6 @@ require "open3" require "tmpdir" require "tty-prompt" -require "rainbow" require "nextgen/ext/prompt/list" require "nextgen/ext/prompt/multilist" @@ -87,7 +86,7 @@ def run # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity #{green("Done!")} A Rails #{rails_version} app was generated in #{cyan(app_path)}. - Run #{set_color("bin/setup", :yellow)} in that directory to get started. + Run #{yellow("bin/setup")} in that directory to get started. DONE @@ -97,10 +96,10 @@ def run # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity attr_accessor :app_path, :app_name, :rails_opts, :generators - def_delegators :shell, :say, :set_color + def_delegators :shell, :say def continue_if(question) - if prompt.yes?("#{question} ↵") + if prompt.yes?(question) say else say "Canceled", :red @@ -122,7 +121,7 @@ def rails_version def ask_rails_version selected = prompt.select( - "What #{underline("version")} of Rails will you use?", + "What version of Rails will you use?", Rails.version => :current, "edge (#{Rails.edge_branch} branch)" => :edge ) @@ -130,17 +129,20 @@ def ask_rails_version end def ask_database - databases = { + common_databases = { "SQLite3 (default)" => "sqlite3", "PostgreSQL (recommended)" => "postgresql", - **%w[MySQL Trilogy Oracle SQLServer JDBCMySQL JDBCSQLite3 JDBCPostgreSQL JDBC].to_h do |name| + "MySQL" => "mysql" + } + all_databases = common_databases.merge( + %w[MySQL Trilogy Oracle SQLServer JDBCMySQL JDBCSQLite3 JDBCPostgreSQL JDBC].to_h do |name| [name, name.downcase] end, "None (disable Active Record)" => nil - } - rails_opts.database = prompt_select( - "Which #{underline("database")}?", databases ) + rails_opts.database = + prompt_select("Which database?", common_databases.merge("More options..." => false)) || + prompt_select("Which database?", all_databases) end def ask_full_stack_or_api @@ -154,7 +156,7 @@ def ask_full_stack_or_api def ask_frontend_management frontend = prompt_select( - "How will you manage frontend #{underline("assets")}?", + "How will you manage frontend assets?", "Sprockets (default)" => "sprockets", "Propshaft" => "propshaft", "Vite" => :vite @@ -170,7 +172,7 @@ def ask_frontend_management def ask_css rails_opts.css = prompt_select( - "Which #{underline("CSS")} framework will you use with the asset pipeline?", + "Which CSS framework will you use with the asset pipeline?", "None (default)" => nil, "Bootstrap" => "bootstrap", "Bulma" => "bulma", @@ -182,7 +184,7 @@ def ask_css def ask_javascript rails_opts.javascript = prompt_select( - "Which #{underline("JavaScript")} bundler will you use with the asset pipeline?", + "Which JavaScript bundler will you use with the asset pipeline?", "Importmap (default)" => "importmap", "Bun" => "bun", "ESBuild" => "esbuild", @@ -213,7 +215,7 @@ def ask_rails_frameworks end answers = prompt.multi_select( - "Which optional Rails #{underline("frameworks")} do you need?", + "Which optional Rails frameworks do you need?", frameworks, default: frameworks.keys.reverse ) @@ -223,7 +225,7 @@ def ask_rails_frameworks def ask_test_framework rails_opts.test_framework = prompt_select( - "Which #{underline("test")} framework will you use?", + "Which test framework will you use?", "Minitest (default)" => "minitest", "RSpec" => "rspec", "None" => nil @@ -232,7 +234,7 @@ def ask_test_framework def ask_system_testing system_testing = prompt_select( - "Include #{underline("system testing")} (capybara)?", + "Include system testing (capybara)?", "Yes (default)" => true, "No" => false ) @@ -309,9 +311,9 @@ def shell @shell ||= Thor::Base.shell.new end - def green(string) = set_color(string, :green) - def cyan(string) = set_color(string, :cyan) - def underline(string) = Rainbow(string).underline - def prompt_select(question, choices) = prompt.select(question, choices, enum: ".", cycle: true) + def cyan(string) = shell.set_color(string, :cyan) + def green(string) = shell.set_color(string, :green) + def yellow(string) = shell.set_color(string, :yellow) + def prompt_select(question, choices) = prompt.select(question, choices, cycle: true) end end