Class: Dsu::Subcommands::Theme

Inherits:
BaseSubcommand show all
Defined in:
lib/dsu/subcommands/theme.rb

Instance Method Summary collapse

Methods included from Dsu::Support::Ask

#ask_while, #yes?

Methods inherited from BaseCLI

date_option_description, exit_on_failure?, #initialize, mnemonic_option_description

Methods included from Dsu::Support::TimesSortable

#sorted_dsu_times_for, #times_for, #times_sort

Methods included from Dsu::Support::CommandHookable

included

Methods included from Dsu::Support::CommandHelpColorizable

included

Methods included from Dsu::Support::ColorThemable

apply_theme, #prompt_with_options

Constructor Details

This class inherits a constructor from Dsu::BaseCLI

Instance Method Details

#create(theme_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dsu/subcommands/theme.rb', line 26

def create(theme_name)
  if Models::ColorTheme.exist?(theme_name: theme_name)
    message = I18n.t('subcommands.theme.create.errors.already_exists', theme_name: theme_name)
    Views::Shared::Error.new(messages: message).render
    return false
  end
  prompt_string = I18n.t('subcommands.theme.create.prompts.create_theme', theme_name: theme_name)
  prompt = color_theme.prompt_with_options(prompt: prompt_string, options: %w[y N])
  if yes?(prompt, options: options)
    theme_hash = Models::ColorTheme::DEFAULT_THEME.dup
    theme_hash[:description] = options[:description] || I18n.t('subcommands.theme.generic.color_theme',
      theme_name: theme_name.capitalize)
    Models::ColorTheme.new(theme_name: theme_name, theme_hash: theme_hash).save!
    message = I18n.t('subcommands.theme.create.messages.created', theme_name: theme_name)
    Views::Shared::Info.new(messages: "\n#{message}").render
    true
  else
    message = I18n.t('subcommands.theme.create.messages.cancelled')
    Views::Shared::Info.new(messages: "\n#{message}").render
    false
  end
end

#delete(theme_name) ⇒ Object

rubocop:disable Metrics/MethodLength



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dsu/subcommands/theme.rb', line 52

def delete(theme_name) # rubocop:disable Metrics/MethodLength
  display_dsu_header

  if theme_name == Models::ColorTheme::DEFAULT_THEME_NAME
    message = I18n.t('subcommands.theme.delete.errors.cannot_delete', theme_name: theme_name)
    Views::Shared::Error.new(messages: message).render
    return
  end

  unless Models::ColorTheme.exist?(theme_name: theme_name)
    message = I18n.t('subcommands.theme.generic.errors.does_not_exist', theme_name: theme_name)
    Views::Shared::Error.new(messages: message).render
    return
  end

  prompt_string = I18n.t('subcommands.theme.delete.prompts.delete_theme', theme_name: theme_name)
  prompt = color_theme.prompt_with_options(prompt: prompt_string, options: %w[y N])
  message = if yes?(prompt, options: options)
    Models::ColorTheme.delete!(theme_name: theme_name)
    change_theme
    I18n.t('subcommands.theme.delete.messages.deleted', theme_name: theme_name)
  else
    I18n.t('subcommands.theme.delete.messages.cancelled')
  end
  Views::Shared::Info.new(messages: "\n#{message}").render
end

#listObject



82
83
84
# File 'lib/dsu/subcommands/theme.rb', line 82

def list
  Views::ColorTheme::Index.new.render
end

#show(theme_name = configuration.theme_name) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/dsu/subcommands/theme.rb', line 109

def show(theme_name = configuration.theme_name)
  if Dsu::Models::ColorTheme.exist?(theme_name: theme_name)
    Views::ColorTheme::Show.new(theme_name: theme_name).render
    return
  end

  message = I18n.t('subcommands.theme.generic.errors.does_not_exist', theme_name: theme_name)
  Views::Shared::Error.new(messages: message).render
end

#use(theme_name = Models::ColorTheme::DEFAULT_THEME_NAME) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dsu/subcommands/theme.rb', line 89

def use(theme_name = Models::ColorTheme::DEFAULT_THEME_NAME)
  display_dsu_header

  return if Dsu.env.local? && !Models::ColorTheme.exist?(theme_name: theme_name) && !create(theme_name)

  unless Models::ColorTheme.exist?(theme_name: theme_name)
    message = I18n.t('subcommands.theme.generic.errors.does_not_exist', theme_name: theme_name)
    Views::Shared::Error.new(messages: message).render
    return
  end

  change_theme theme_name: theme_name
  # We need to display the header after the theme is updated so that it is displayed in the
  # correct theme color.
  message = I18n.t('subcommands.theme.use.messages.using_color_theme', theme_name: theme_name)
  Views::Shared::Info.new(messages: message).render
end