Module: SimplyGenius::Atmos::UI

Extended by:
ActiveSupport::Concern
Includes:
GemLogger::LoggerSupport
Included in:
Commands::BaseCommand, Commands::Container, IpcActions::Notify, Plugins::OutputFilter, Providers::Aws::AuthManager, TerraformExecutor
Defined in:
lib/simplygenius/atmos/ui.rb

Defined Under Namespace

Classes: Markup

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color_enabledObject



30
31
32
# File 'lib/simplygenius/atmos/ui.rb', line 30

def self.color_enabled
  Rainbow.enabled
end

.color_enabled=(val) ⇒ Object



26
27
28
# File 'lib/simplygenius/atmos/ui.rb', line 26

def self.color_enabled=(val)
  Rainbow.enabled = val
end

Instance Method Details

#agree(question, character = nil, &details) ⇒ Object



79
80
81
# 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) ⇒ Object



75
76
77
# 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) ⇒ Object



83
84
85
# File 'lib/simplygenius/atmos/ui.rb', line 83

def choose(*items, &details)
  return Markup.new().choose(*items, &details)
end

#display(data) ⇒ Object

Pretty display of hashes



88
89
90
91
# 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

#errorObject



67
68
69
# File 'lib/simplygenius/atmos/ui.rb', line 67

def error
  return Markup.new(:red)
end

#notify(message: nil, title: nil, modal: false, **opts) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



71
72
73
# File 'lib/simplygenius/atmos/ui.rb', line 71

def say(statement)
  return Markup.new().say(statement)
end

#warnObject



63
64
65
# File 'lib/simplygenius/atmos/ui.rb', line 63

def warn
  return Markup.new(:yellow)
end