Module: EasyCreds::Views::TemplatesMenu
- Defined in:
- lib/easy_creds/views/templates_menu.rb
Class Method Summary collapse
- .build_choices(registry, default) ⇒ Object
- .create_template(prompt, _registry) ⇒ Object
- .delete_template(prompt, registry, name) ⇒ Object
- .duplicate_template(prompt, registry, name) ⇒ Object
- .edit_in_editor(registry, name) ⇒ Object
- .prepare_user_copy(registry, name) ⇒ Object
- .run(prompt) ⇒ Object
- .set_default(name) ⇒ Object
- .template_actions(prompt, registry, name, default) ⇒ Object
Class Method Details
.build_choices(registry, default) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/easy_creds/views/templates_menu.rb', line 27 def self.build_choices(registry, default) p = Theme.pastel choices = [{ name: p.dim(' ── Bundled (read-only) ────────────────────────'), value: nil, disabled: '' }] registry.send(:bundled_names).sort.each do |name| star = name == default ? " #{p.green('[default]')}" : '' choices << { name: " #{p.bold(name.to_s.ljust(28))} #{p.dim('bundled')}#{star}", value: name } end user_names = registry.send(:user_names).sort if user_names.any? choices << { name: p.dim(' ── User templates ─────────────────────────────'), value: nil, disabled: '' } user_names.each do |name| star = name == default ? " #{p.green('[default]')}" : '' choices << { name: " #{p.bold(name.to_s.ljust(28))} #{p.dim('user')}#{star}", value: name } end end choices << { name: p.dim(' ─────────────────────────────────────────────────'), value: nil, disabled: '' } choices << { name: " #{p.bold('create new')}", value: :create } choices << { name: " #{p.dim('back')}", value: :back } choices end |
.create_template(prompt, _registry) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/easy_creds/views/templates_menu.rb', line 122 def self.create_template(prompt, _registry) name = prompt.ask('New template name:') do |q| q.validate(/\A[a-z0-9_-]+\z/, 'Use lowercase letters, digits, hyphens, or underscores.') end return unless name dest = File.join(EasyCreds.config.global_dir, 'templates', "#{name}.yml") if File.exist?(dest) Theme.failure("#{dest} already exists.") return end File.write(dest, "# #{name}\n# Add your credential keys below:\n") editor = EasyCreds.config.editor || ENV['VISUAL'] || ENV.fetch('EDITOR', 'vi') system("#{editor} #{Shellwords.escape(dest)}") Theme.success("Created #{dest}") end |
.delete_template(prompt, registry, name) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/easy_creds/views/templates_menu.rb', line 115 def self.delete_template(prompt, registry, name) return unless prompt.yes?("Delete template '#{name}'?", default: false) registry.delete(name) Theme.success("Deleted #{name}") end |
.duplicate_template(prompt, registry, name) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/easy_creds/views/templates_menu.rb', line 99 def self.duplicate_template(prompt, registry, name) new_name = prompt.ask('New template name (used as filename):') do |q| q.validate(/\A[a-z0-9_-]+\z/, 'Use lowercase letters, digits, hyphens, or underscores.') end return unless new_name src = registry.path_for(name) dest = File.join(EasyCreds.config.global_dir, 'templates', "#{new_name}.yml") if File.exist?(dest) Theme.failure("#{dest} already exists.") return end FileUtils.cp(src, dest) Theme.success("Created #{dest}") end |
.edit_in_editor(registry, name) ⇒ Object
83 84 85 86 87 |
# File 'lib/easy_creds/views/templates_menu.rb', line 83 def self.edit_in_editor(registry, name) path = prepare_user_copy(registry, name) editor = EasyCreds.config.editor || ENV['VISUAL'] || ENV.fetch('EDITOR', 'vi') system("#{editor} #{Shellwords.escape(path)}") end |
.prepare_user_copy(registry, name) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/easy_creds/views/templates_menu.rb', line 89 def self.prepare_user_copy(registry, name) user_path = File.join(EasyCreds.config.global_dir, 'templates', "#{name}.yml") return user_path if File.exist?(user_path) bundled = registry.send(:bundled_path, name) FileUtils.cp(bundled, user_path) Theme.notice("Copied bundled template to #{user_path} — editing your copy.") user_path end |
.run(prompt) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/easy_creds/views/templates_menu.rb', line 8 def self.run(prompt) loop do registry = Templates::Registry.new(global_dir: EasyCreds.config.global_dir) default = EasyCreds.config.default_template choices = build_choices(registry, default) choice = prompt.select("\nTemplate library:", choices, cycle: true, filter: false, per_page: [choices.size, 20].min) case choice when nil then next when :create then create_template(prompt, registry) when :back then return else template_actions(prompt, registry, choice, default) end end end |
.set_default(name) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/easy_creds/views/templates_menu.rb', line 76 def self.set_default(name) EasyCreds.configure { |c| c.default_template = name } path = File.join(EasyCreds.config.global_dir, 'config.yml') Configuration.write_file!(EasyCreds.config, path) Theme.success("Default template set to #{name}") end |
.template_actions(prompt, registry, name, default) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/easy_creds/views/templates_menu.rb', line 54 def self.template_actions(prompt, registry, name, default) p = Theme.pastel is_user = registry.user_defined?(name) actions = [ { name: " #{p.bold('set as default')} #{p.dim('─')} mark this as the default template", value: :set_default } ] actions << { name: " #{p.bold('edit ($EDITOR)')} #{p.dim('─')} open YAML in $EDITOR", value: :edit_editor } actions << { name: " #{p.bold('duplicate')} #{p.dim('─')} copy to user templates dir", value: :duplicate } actions << { name: " #{p.bold('delete')} #{p.dim('─')} remove (user templates only)", value: :delete } if is_user actions << { name: " #{p.dim('back')}", value: :back } action = prompt.select("Template: #{p.bold(name.to_s)}", actions, cycle: true) case action when :set_default then set_default(name) when :edit_editor then edit_in_editor(registry, name) when :duplicate then duplicate_template(prompt, registry, name) when :delete then delete_template(prompt, registry, name) end end |