lib/kuber_kit/ui/simple.rb in kuber_kit-0.3.11 vs lib/kuber_kit/ui/simple.rb in kuber_kit-0.4.0

- old
+ new

@@ -1,21 +1,35 @@ class KuberKit::UI::Simple + include KuberKit::Import[ + "tools.logger", + ] + class Task def initialize(title, &callback) @title = title @callback = callback end + def print_started + puts "- #{@title}" + end + + def print_finished + puts "- #{@title.grey}" + end + def execute if @thread raise "Already started execution of task '#{title}'" end @thread = Thread.new do - puts "Start task: #{@title.green}" + Thread.current.abort_on_exception = true + Thread.current.report_on_exception = false + print_started @callback.call(self) - puts "Finish task: #{@title.green}" + print_finished end end def wait if !@thread @@ -28,12 +42,16 @@ @title = title end end class TaskGroup + def initialize(task_class) + @task_class = task_class + end + def add(task_title, &task_block) - task = Task.new(task_title, &task_block) + task = @task_class.new(task_title, &task_block) task.execute add_task(task) end def add_task(task) @@ -45,11 +63,11 @@ @tasks.each(&:wait) end end def create_task_group - TaskGroup.new + TaskGroup.new(KuberKit::UI::Simple::Task) end def create_task(title, &block) task = Task.new(title, &block) task.execute @@ -64,19 +82,30 @@ print_text(title, text, color: String::Colors::RED) end def print_warning(title, text) print_text(title, text, color: String::Colors::YELLOW) + logger.debug(text) end + def print_debug(title, text) + logger.debug(text) + end + + def print_result(message, data = {}) + print_debug("Result", "---------------------------") + print_debug("Result", message) + print_debug("Result", "---------------------------") + end + def prompt(text, options, &callback) print_info("Select", text) result = $stdin.gets.chomp callback.call(result) if callback result end - private + protected def print_text(title, text, color:) puts "#{title.colorize(color)}\r\n #{text.colorize(color)}" end end \ No newline at end of file