module SimplyGenius::Atmos::UI

Public Class Methods

color_enabled() click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 30
def self.color_enabled
  Rainbow.enabled
end
color_enabled=(val) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 26
def self.color_enabled=(val)
  Rainbow.enabled = val
end

Public Instance Methods

agree(question, character=nil, &details) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 79
def agree(question, character=nil, &details)
  return Markup.new().agree(question, character, &details)
end
ask(question, answer_type=nil, &details) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 75
def ask(question, answer_type=nil, &details)
  return Markup.new().ask(question, answer_type, &details)
end
choose(*items, &details) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 83
def choose(*items, &details)
  return Markup.new().choose(*items, &details)
end
display(data) click to toggle source

Pretty display of hashes

# File lib/simplygenius/atmos/ui.rb, line 88
def display(data)
  data = Hashie.stringify_keys(data)
  display = YAML.dump(data).sub(/\A---\n/, "").gsub(/^/, "  ")
end
error() click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 67
def error
  return Markup.new(:red)
end
notify(message:nil, title: nil, modal: false, **opts) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 93
      def notify(message:nil, title: nil, modal: false, **opts)

        result = {
            'stdout' => '',
            'success' => ''
        }

        message = message.to_s
        title = title.present? ? title.to_s : "Atmos Notification"
        modal = ["true", "1"].include?(modal.to_s)
        modal = false if Atmos.config["atmos.ui.notify.disable_modal"]

        return result if Atmos.config["atmos.ui.notify.disable"].to_s == "true"

        force_inline = opts[:inline].to_s == "true" || Atmos.config["atmos.ui.notify.force_inline"].to_s == "true"

        command = Atmos.config["atmos.ui.notify.command"]

        if command.present? && ! force_inline

          raise ArgumentError.new("notify command must be a list") if ! command.is_a?(Array)

          command = command.collect do |c|
            c = c.gsub("{{title}}", title)
            c = c.gsub("{{message}}", message)
            c = c.gsub("{{modal}}", modal.to_s)
          end
          result.merge! run_ui_process(*command)

        elsif OS.mac? && ! force_inline
          display_method = modal ? "displayDialog" : "displayNotification"

          # Usse to_json as JSON.generate doesn't work for strings on ruby 2.3
          dialogScript = <<~EOF
            var app = Application.currentApplication();
            app.includeStandardAdditions = true;
            app.#{display_method}(
              #{message.to_json}, {
                withTitle: #{title.to_json},
                buttons: ['OK'],
                defaultButton: 1
            })
          EOF

          result.merge! run_ui_process("osascript", "-l", "JavaScript", "-e", dialogScript)

        elsif OS.linux? && ! OS.docker? && ! force_inline
          # TODO: add a modal option
          result.merge! run_ui_process("notify-send", title, message)

        # TODO windows notifications?
        # elseif OS.windows? && ! force_inline

        else

          logger.debug("Notifications are unsupported on this OS") unless force_inline
          logger.info(Rainbow("\n***** #{title} *****\n#{message}\n").orange)
          if modal
            logger.info(Rainbow("Hit enter to continue\n").orange)
            $stdin.gets
          end

        end

        return result
      end
say(statement) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 71
def say(statement)
  return Markup.new().say(statement)
end
warn() click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 63
def warn
  return Markup.new(:yellow)
end

Private Instance Methods

run_ui_process(*args) click to toggle source
# File lib/simplygenius/atmos/ui.rb, line 162
def run_ui_process(*args)
  stdout, status = Open3.capture2e(*args)
  result = {'stdout' => stdout, 'success' => status.success?.to_s}
  if ! status.success?
    result['error'] = "Notification process failed"
    logger.debug("Failed to run notification utility: #{stdout}")
  end
  return result
end