Sha256: 70f7d47b7403f868cce6f153785d95c3c99c2b43ddd913bdbd691548a928fa6f

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

#!/usr/bin/env ruby

require_relative '../lib/u-menu'

require 'tty-prompt'

command = ARGV.first

if command == 'init'
  puts "\n\nCreating a simple config file at ``"
  Micro::Menu.configurations.init
end

config = Micro::Menu.configurations.load
colors = Micro::Menu.colors
icons = Micro::Menu.icons.all
messages = Micro::Menu.messages

trap('INT') do
  thanks = messages.thanks

  puts "\n\n#{thanks.sample}"
  exit(0)
end

title, options, use_icons =
  config.values_at(:title, :options, :use_icons)

use_icons ||= false

prompt = TTY::Prompt.new(
  prefix: colors.magenta("#{use_icons ? "\uea85" : ''} u-menu "),
  interrupt: :signal
)

# create a default edit settings option
options << {
  name: '{{settings}} Edit Settings', type: 'edit', value: 'settings', execute: config[:_path]
}

options = options.sort { |a, b| a[:name] <=> b[:name] }

options.each do |option|
  if option[:name].match(/\{\{(?<icon_name>.*)\}\}/)
    icon_name = Regexp.last_match('icon_name')
    option[:name].sub!("{{#{icon_name}}}", use_icons ? icons[icon_name] : '')
  else
    option[:name] = "#{use_icons ? icons['terminal'] : ''} #{option[:name]}" if option[:type] == 'command'
    option[:name] = "#{use_icons ? icons['link'] : ''} #{option[:name]}" if option[:type] == 'link'
  end
end

action = nil
loop do
  choice = prompt.select(title, options, filter: true)
  action = config[:options].find { |item| item[:value] == choice }

  case action[:type]
  when 'command'
    system action[:execute]
  when 'link'
    puts "#{colors.bold('Sure... opening link')} '#{colors.cyan(action[:execute])}'"
    system "open #{action[:execute]} &"
  when 'edit'
    unless config.dig(:settings, :editor)
      warn '[error] key `settings > editor` not found into config file.'
      exit(1)
    end

    puts "#{colors.bold('Sure... opening file')} '#{colors.cyan(action[:execute])}'"
    system "#{config[:settings][:editor]} #{action[:execute]}"
  end

  exit(0)
rescue StandardError => e
  puts "#{colors.red('[error]')} #{e}"
  exit(1)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
u-menu-0.10.0 bin/u-menu