#!/usr/bin/env ruby require 'pastel' require 'tty-prompt' require 'yaml' paths = [ File.expand_path("#{Dir.home}/.umenurc.yml", __dir__), File.expand_path("#{Dir.home}/.umenu/umenurc.yml", __dir__), ] config_path = paths.first { |path| File.exist? path } config = YAML.load_file(config_path, symbolize_names: true) pastel = Pastel.new prompt = TTY::Prompt.new(prefix: pastel.magenta("\uea85 \u00b5menu "), interrupt: :signal) icons = { 'run' => "\ueb9e", 'terminal' => "\uea85", 'github' => "\uea84", 'chart' => "\ue760", 'jira' => "\ue75c", 'settings' => "\ue615", 'link' => "\ueb15" } thanks = [ pastel.green('Great job! o/'), pastel.green('See you later! =)'), pastel.green('Baby bye bye bye...'), "Bring me #{pastel.green('a cookie')} when you come back!?", "Have a nice #{pastel.cyan('day')}! =)", "See you later #{pastel.green('olligator')}! =)" ] trap('INT') do puts "\n\n#{thanks.sample}" exit(0) end action = nil title, options = config.values_at(:title, :options) 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 = Regexp.last_match('icon_name') option[:name].sub!("{{#{icon_name}}}", icons[icon_name]) else option[:name] = "#{icons['terminal']} #{option[:name]}" if option[:type] == 'command' option[:name] = "#{icons['link']} #{option[:name]}" if option[:type] == 'link' end end 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 "#{pastel.bold('Sure... opening link')} '#{pastel.cyan(action[:execute])}'" system "open #{action[:execute]} &" when 'edit' puts "#{pastel.bold('Sure... opening file')} '#{pastel.cyan(action[:execute])}'" system "#{config[:settings][:editor]} #{action[:execute]}" end exit(0) rescue StandardError => e puts "#{pastel.red('[error]')} #{e}" exit(1) end