From 6a1d3a8921388aa371732b6750e9ddfd1f2318d7 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sat, 7 May 2016 10:04:07 +0200 Subject: [PATCH] add choice wallet --- bin/mpw | 3 ++- lib/mpw/ui/cli.rb | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/bin/mpw b/bin/mpw index e0f5f16..088f173 100755 --- a/bin/mpw +++ b/bin/mpw @@ -113,7 +113,7 @@ end.parse! config = MPW::Config.new(options[:config]) check_error = config.checkconfig -cli = Cli.new(config, options[:wallet]) +cli = Cli.new(config) # Setup a new config if not check_error or not options[:setup].nil? @@ -122,6 +122,7 @@ elsif not config.check_gpg_key? cli.setup_gpg_key end +cli.get_wallet(options[:wallet]) cli.decrypt # Display the item's informations diff --git a/lib/mpw/ui/cli.rb b/lib/mpw/ui/cli.rb index 868ccfe..57063de 100644 --- a/lib/mpw/ui/cli.rb +++ b/lib/mpw/ui/cli.rb @@ -14,12 +14,9 @@ require "#{APP_ROOT}/../lib/mpw/mpw.rb" class Cli # Constructor - # @args: lang -> the operating system language - # config_file -> a specify config file - # TODO - def initialize(config, wallet) + # @args: config_file -> a specify config file + def initialize(config) @config = config - @wallet_file = "#{@config.wallet_dir}/#{wallet}.mpw" end # Create a new config file @@ -155,6 +152,40 @@ class Cli puts item.comment end + # Display the wallet + # @args: wallet -> the wallet name + def get_wallet(wallet=nil) + if wallet.to_s.empty? + wallets = Dir.glob("#{@config.wallet_dir}/*.mpw") + puts wallets + + case wallets.length + when 0 + puts I18n.t('display.nothing') + when 1 + @wallet_file = wallets[0] + else + i = 1 + wallets.each do |wallet| + print "#{i}: ".cyan + puts File.basename(wallet, '.mpw') + + i += 1 + end + + choice = ask(I18n.t('form.select')).to_i + + if choice >= 1 and choice < i + @wallet_file = wallets[choice-1] + else + puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow + end + end + else + @wallet_file = "#{@config.wallet_dir}/#{wallet}.mpw" + end + end + # Form to add a new item def add options = {}