Class: Rake::TUI
- Inherits:
-
Object
- Object
- Rake::TUI
- Defined in:
- lib/rake-tui/ext/rake/tui.rb
Constant Summary collapse
- VERSION =
File.read(File.('../../../../../VERSION', __FILE__)).strip
- BRANDING_HEADER_DEFAULT =
"== rake-tui version #{VERSION} =="- PROMPT_QUESTION_DEFAULT =
"Choose a Rake task: "- TASK_FORMATTER_DEFAULT =
lambda do |task, tasks| max_task_size = tasks.map(&:name_with_args).map(&:size).max + 1 line = "rake #{task.name_with_args.ljust(max_task_size)} # #{task.comment}" bound = TTY::Screen.width - 6 line.size <= bound ? line : "#{line[0..(bound - 3)]}..." end
Class Method Summary collapse
-
.instance(tui = nil) ⇒ Object
Singleton instance (if argument is specified, it is used to set instance).
-
.run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) ⇒ Object
Runs TUI (recommended for general use).
Instance Method Summary collapse
-
#initialize(tasks = Rake.application.tasks) ⇒ TUI
constructor
Initializes a new TUI object.
-
#run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) ⇒ Object
Runs TUI.
Constructor Details
#initialize(tasks = Rake.application.tasks) ⇒ TUI
Initializes a new TUI object
44 45 46 47 48 49 50 51 52 |
# File 'lib/rake-tui/ext/rake/tui.rb', line 44 def initialize(tasks=Rake.application.tasks) if tasks.empty? Rake.application.init Rake.application.load_rakefile tasks = Rake.application.tasks end @tasks = tasks.reject {|task| task.comment.nil?} end |
Class Method Details
.instance(tui = nil) ⇒ Object
Singleton instance (if argument is specified, it is used to set instance)
20 21 22 23 24 25 26 |
# File 'lib/rake-tui/ext/rake/tui.rb', line 20 def instance(tui=nil) if tui.nil? @instance ||= new else @instance = tui end end |
.run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) ⇒ Object
Runs TUI (recommended for general use)
Optionally takes a branding_header to customize the branding header or take it off when set to nil.
Optionally takes a prompt_question to customize the question for selecting a rake task.
Optionally pass a task_formatter mapping block to transform every rake task line into a different format e.g. Rake::TUI.new.run { |task, tasks| task.name_with_args } This makes tasks show up without rake prefix or ‘# description` suffix
38 39 40 |
# File 'lib/rake-tui/ext/rake/tui.rb', line 38 def run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) instance.run(branding_header: branding_header, prompt_question: prompt_question, &task_formatter) end |
Instance Method Details
#run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) ⇒ Object
Runs TUI
Optionally takes a branding_header to customize the branding header or take it off when set to nil.
Optionally takes a prompt_question to customize the question for selecting a rake task.
Optionally takes a task_formatter mapping block to transform every rake task line into a different format e.g. Rake::TUI.new.run { |task, tasks| task.name_with_args } This makes tasks show up without rake prefix or ‘# description` suffix
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rake-tui/ext/rake/tui.rb', line 64 def run(branding_header: BRANDING_HEADER_DEFAULT, prompt_question: PROMPT_QUESTION_DEFAULT, &task_formatter) puts branding_header unless branding_header.nil? if @tasks.empty? puts "No Rake tasks found!" puts "Exiting..." exit(0) end require 'tty-prompt' prompt = TTY::Prompt.new prompt.on(:keyescape) do |event| puts # a new line is needed puts "Exiting..." exit(0) end begin task_formatter ||= TASK_FORMATTER_DEFAULT rake_task_lines = @tasks.map {|task, tasks| task_formatter.call(task, @tasks)} rake_task_line = prompt.select(prompt_question, rake_task_lines, cycle: true, per_page: [TTY::Screen.height - 5, 1].max, filter: true, show_help: :always) do |list| list.choices rake_task_lines list.singleton_class.define_method(:keyspace) do |event| return unless filterable? if event.value = " " @filter << event.value @active = 1 end end end selected_task = @tasks[rake_task_lines.index(rake_task_line)] rake_task = selected_task.name rake_task_arg_names = selected_task.arg_names rake_task_arg_values = rake_task_arg_names.map do |rake_task_arg_name| prompt.ask("Enter [#{rake_task_arg_name}] (default=nil):") end Rake::Task[rake_task].invoke(*rake_task_arg_values) # TODO support args rescue TTY::Reader::InputInterrupt => e # No Op puts # a new line is needed puts "Exiting..." exit(0) end end |