Sha256: af7c435ac1f765db327fe8e260f3e74e2b482020655f70b036e6aacf6efc8bea

Contents?: true

Size: 1.53 KB

Versions: 16

Compression:

Stored size: 1.53 KB

Contents

class KuberKit::UI::Simple
  class Task
    def initialize(title, &callback)
      @title = title
      @callback = callback
    end

    def execute
      if @thread
        raise "Already started execution of task '#{title}'"
      end

      @thread = Thread.new do
        puts "Start task: #{@title.green}"
        @callback.call(self)
        puts "Finish task: #{@title.green}"
      end
    end

    def wait
      if !@thread
        raise "Task '#{title}' not started"
      end
      @thread.join
    end
    
    def update_title(title)
      @title = title
    end
  end

  class TaskGroup
    def add(task_title, &task_block)
      task = Task.new(task_title, &task_block)
      task.execute
      add_task(task)
    end

    def add_task(task)
      @tasks ||= []
      @tasks << task
    end

    def wait
      @tasks.each(&:wait)
    end
  end

  def create_task_group
    TaskGroup.new
  end

  def create_task(title, &block)
    task = Task.new(title, &block)
    task.execute
    task.wait
  end

  def print_info(title, text)
    print_text(title, text, color: String::Colors::BLUE)
  end

  def print_error(title, text)
    print_text(title, text, color: String::Colors::RED)
  end

  def print_warning(title, text)
    print_text(title, text, color: String::Colors::YELLOW)
  end

  def prompt(text, options, &callback)
    print_info("Select", text)
    result = $stdin.gets.chomp
    callback.call(result)
  end

  private
    def print_text(title, text, color:)
      puts "#{title.colorize(color)}\r\n #{text.colorize(color)}"
    end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
kuber_kit-0.3.5 lib/kuber_kit/ui/simple.rb
kuber_kit-0.3.4 lib/kuber_kit/ui/simple.rb
kuber_kit-0.3.3 lib/kuber_kit/ui/simple.rb
kuber_kit-0.3.2 lib/kuber_kit/ui/simple.rb
kuber_kit-0.3.1 lib/kuber_kit/ui/simple.rb
kuber_kit-0.3.0 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.9 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.8 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.7 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.6 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.5 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.4 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.3 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.2 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.1 lib/kuber_kit/ui/simple.rb
kuber_kit-0.2.0 lib/kuber_kit/ui/simple.rb