Module: PT::IO

Included in:
CLI, Configuration
Defined in:
lib/pt/io.rb

Constant Summary collapse

ACTION =
%w[show open start finish deliver accept reject done assign estimate tasks comment label ]

Instance Method Summary collapse

Instance Method Details

#ask(msg) ⇒ Object



37
38
39
# File 'lib/pt/io.rb', line 37

def ask(msg)
  HighLine.new.ask(msg)
end

#ask_secret(msg) ⇒ Object



41
42
43
# File 'lib/pt/io.rb', line 41

def ask_secret(msg)
  HighLine.new.ask("#{msg.bold}"){ |q| q.echo = '*' }
end

#choose_action(story) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pt/io.rb', line 146

def choose_action(story)
  HighLine.new.choose do |menu|
    menu.prompt = "Please choose action ( [number/name]:select action | [Enter]:show story )".magenta
    menu.choice(:view, 'view details of story','view') { show_story(story) }
    menu.choice(:edit, nil,'edit story') { edit_story(story) }
    menu.choice(:start, nil,'start'.white) { start_story(story) }
    menu.choice(:finish, nil,'finish'.blue) { finish_story(story) }
    menu.choice(:deliver, nil,'deliver'.yellow) { deliver_story(story) }
    menu.choice(:accept, nil,'accept'.green) { accept_story(story) }
    menu.choice(:reject, nil,'reject'.red) { reject_story(story) }
    %w[unstart assign estimate tasks comment add_label open].each do |action|
      menu.choice(action.to_sym) { send("#{action}_story", story) }
    end
    menu.choice('id (copy story id)') { copy_story_id(story)}
    menu.choice('url (copy story url)') { copy_story_url(story) }
    menu.choice(:back) { say('back to table ....'); return :no_request }
    menu.choice(:quit) { quit }
    menu.default = :view
  end
end

#choose_filterObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pt/io.rb', line 167

def choose_filter
  HighLine.new.choose do |menu|
    menu.prompt = "Please choose filter ( [number/name]:select filter | [Enter]:back to table)".magenta
    %w[current backlog mywork label bug feature unstarted started finished delivered accepted rejected].each do |f|
      menu.choice(f.to_sym) do
        say 'filtering ' + f
        send(f.to_sym)
      end
    end
    menu.choice(:search) { find }
    menu.choice(:cancel) { say('back to table ....'); return :no_request }
    menu.choice(:quit) { quit }
    menu.default = :back
  end
end

#clearObject



66
67
68
# File 'lib/pt/io.rb', line 66

def clear
  puts "\e[H\e[2J"
end

#compact_message(*msg) ⇒ Object



24
25
26
# File 'lib/pt/io.rb', line 24

def compact_message(*msg)
  puts "#{split_lines(msg)}"
end

#congrats(*msg) ⇒ Object



16
17
18
# File 'lib/pt/io.rb', line 16

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

#error(*msg) ⇒ Object



28
29
30
# File 'lib/pt/io.rb', line 28

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

#get_open_story_task_from_params(task) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pt/io.rb', line 53

def get_open_story_task_from_params(task)
  title "Pending tasks for '#{task.name}'"
  task_struct = Struct.new(:description, :position)

  pending_tasks = [
    task_struct.new('<< Add new task >>', -1)
  ]

  task.tasks.each{ |t| pending_tasks << t unless t.complete }
  table = TodoTaskTable.new(pending_tasks)
  select("Pick task to edit, 1 to add new task", table)
end

#message(*msg) ⇒ Object



20
21
22
# File 'lib/pt/io.rb', line 20

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

#project_to_sObject



49
50
51
# File 'lib/pt/io.rb', line 49

def project_to_s
  "Project #{Settings[:project_name].upcase}"
end

#quitObject



32
33
34
35
# File 'lib/pt/io.rb', line 32

def quit
  message "bye!"
  exit
end

#select(msg, table) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pt/io.rb', line 70

def select(msg, table)
  if table.length > 0
    begin
      table.print
      row = ask "#{msg} (1-#{table.length}, 'q' to exit)".magenta
      if row == 'q'
        quit
      elsif row.to_i > 0
        selected = table[row]
        error "Invalid selection, try again:" unless selected
      elsif %w[n p q c r f].include?(row)
        return row
      end
    end until selected
    selected
  else
    table.print
    error "Sorry, there are no options to select."
    return 'EOF'
  end
end

#select_story_from_paginated_table(options = {}, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pt/io.rb', line 92

def select_story_from_paginated_table(options={}, &block)
  prompt = "Please select a story"
  page = 0
  no_request = false
  begin
    say('Please wait...')
    stories = block.call(page) unless no_request
    current_page = @client.current_page
    total_page = @client.total_page
    title = (options[:title] || 'Stories').to_s + " [#{current_page}/#{total_page}]"
    table = TasksTable.new(stories, title)
    clear
    say "Pivotal Tracker Command Line v#{PT::VERSION}".magenta
    say '========================================================================================='.green
    help = "[num]: select | [f]ilter | #{'[n]ext |' if current_page < total_page} #{'[p]revious |' if current_page > 1} [c]reate | [r]efresh | [q]uit"
    say help.green
    say '========================================================================================='.green
    case story = select(prompt, table)
    when TrackerApi::Resources::Story
      say "Action for >> '#{story.name.green}'[#{story.story_type}]"
      action = (options[:default_action] ? send("#{options[:default_action]}_story", story) : choose_action(story))
      no_request = action == :no_request
    when String
      if story == 'n'
        old_page = page
        page+=1
      elsif story == 'p'
        old_page = page
        page-=1
      elsif story == 'q'
        quit
      elsif story == 'c'
        create_interactive_story
      elsif story == 'f'
        choose_filter
      elsif story == 'EOF'
        HighLine.new.choose do |menu|
          menu.prompt = "Please choose action ( [number/name]:select action)".magenta
          menu.choice(:filter) { choose_filter }
          menu.choice(:back) { say('back to table ....'); page-=1 }
          menu.choice(:quit) { quit }
          menu.default = :quit
        end
      elsif story == 'r'
        page = old_page
      end
      no_request = false
    else
      error "Invalid selection, try again:"
      no_request = true
    end
  end while true
end

#split_lines(text) ⇒ Object

I/O



8
9
10
# File 'lib/pt/io.rb', line 8

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

#title(*msg) ⇒ Object



12
13
14
# File 'lib/pt/io.rb', line 12

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

#user_sObject



45
46
47
# File 'lib/pt/io.rb', line 45

def user_s
  "#{Settings[:user_name]} (#{Settings[:user_initials]})"
end