Class: Trlo::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/trlo/ui.rb

Constant Summary collapse

GLOBAL_CONFIG_PATH =
"#{ENV['HOME']}/.trlo"
LOCAL_CONFIG_PATH =
"#{Dir.pwd}/.trlo"

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ UI

Returns a new instance of UI.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/trlo/ui.rb', line 10

def initialize(args)
  # require 'trlo/debugger' if ARGV.delete('--debug')
  @io = HighLine.new
  @global_config = load_global_config
  @client = Trlo::Client.new(@global_config)
  @local_config = load_local_config
  @board = @client.get_board(@local_config[:board_id])
  # command = args[0].to_sym rescue :my_work
  # @params = args[1..-1]
  # commands.include?(command.to_sym) ? send(command.to_sym) : help
end

Instance Method Details

#ask(msg) ⇒ Object



111
112
113
# File 'lib/trlo/ui.rb', line 111

def ask(msg)
  @io.ask("#{msg.bold}")
end

#check_local_config_pathObject



61
62
63
64
65
66
# File 'lib/trlo/ui.rb', line 61

def check_local_config_path
  if GLOBAL_CONFIG_PATH == LOCAL_CONFIG_PATH
    error("Please execute .trlo inside your project directory and not in your home.")
    exit
  end
end

#congrats(*msg) ⇒ Object



82
83
84
# File 'lib/trlo/ui.rb', line 82

def congrats(*msg)
  puts "\n#{split_lines(msg).green.bold}"
end

#error(*msg) ⇒ Object



90
91
92
# File 'lib/trlo/ui.rb', line 90

def error(*msg)
  puts "\n#{split_lines(msg).red.bold}"
end

#load_global_configObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trlo/ui.rb', line 22

def load_global_config
  config = YAML.load(File.read(GLOBAL_CONFIG_PATH)) rescue {}
  # if config.empty?
  #   message "I can't find info about your Trello account in #{GLOBAL_CONFIG_PATH}."
  #   # while !config[:api_key] do
  #   #   config[:email] = ask "What is your email?"
  #   #   password = ask_secret "And your password? (won't be displayed on screen)"
  #   #   begin
  #   #     # config[:api_number] = Trlo::Client.get_api_token(config[:email], password)
  #   #   rescue PT::InputError => e
  #   #     error e.message + " Please try again."
  #   #   end
  #   # end
  #   congrats "Thanks!",
  #     "Your API key is " + config[:api_key],
  #     "I'm saving it in #{GLOBAL_CONFIG_PATH} so you don't have to log in again."
  #   save_config(config, GLOBAL_CONFIG_PATH)
  # end
  config
end

#load_local_configObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/trlo/ui.rb', line 43

def load_local_config
  check_local_config_path
  config = YAML.load(File.read(LOCAL_CONFIG_PATH)) rescue {}
  if config.empty?
    message "I can't find info about this project in #{LOCAL_CONFIG_PATH}"
    boards = Trlo::BoardTable.new(@client.get_boards)
    board = select("Please select the board for the current directory", boards)
    config[:board_id], config[:board_name] = board.id, board.name
    project = @client.get_board(board.id)
    # membership = @client.get_membership(project, @global_config[:email])
    # config[:user_name], config[:user_id], config[:user_initials] = membership.name, membership.id, membership.initials
    congrats "Thanks! I'm saving this project's info",
      "in #{LOCAL_CONFIG_PATH}: remember to .gitignore it!"
    save_config(config, LOCAL_CONFIG_PATH)
  end
  config
end

#message(*msg) ⇒ Object



86
87
88
# File 'lib/trlo/ui.rb', line 86

def message(*msg)
  puts "\n#{split_lines(msg)}"
end

#save_config(config, path) ⇒ Object



68
69
70
71
# File 'lib/trlo/ui.rb', line 68

def save_config(config, path)
  File.new(path, 'w') unless File.exists?(path)
  File.open(path, 'w') {|f| f.write(config.to_yaml) }
end

#select(msg, table) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/trlo/ui.rb', line 94

def select(msg, table)
  if table.length > 0
    begin
      table.print @global_config
      row = ask "#{msg} (1-#{table.length}, 'q' to exit)"
      quit if row == 'q'
      selected = table[row]
      error "Invalid selection, try again:" unless selected
    end until selected
    selected
  else
    table.print @global_config
    message "Sorry, there are no options to select."
    quit
  end
end

#split_lines(text) ⇒ Object

I/O



74
75
76
# File 'lib/trlo/ui.rb', line 74

def split_lines(text)
  text.respond_to?(:join) ? text.join("\n") : text
end

#title(*msg) ⇒ Object



78
79
80
# File 'lib/trlo/ui.rb', line 78

def title(*msg)
  puts "\n#{split_lines(msg)}".bold
end