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



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

def ask(msg)
  prompt.ask(msg)
end

#ask_secret(msg) ⇒ Object



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

def ask_secret(msg)
  prompt.mask(msg)
end

#choose_action(story) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pt/io.rb', line 155

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pt/io.rb', line 176

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



75
76
77
# File 'lib/pt/io.rb', line 75

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

#compact_message(*msg) ⇒ Object



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

def compact_message(*msg)
  TTY::Markdown.parse "#{split_lines(msg)}"
end

#congrats(*msg) ⇒ Object



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

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

#error(*msg) ⇒ Object



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

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

#get_open_story_task_from_params(task) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pt/io.rb', line 62

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



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

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

#project_to_sObject



58
59
60
# File 'lib/pt/io.rb', line 58

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

#promptObject



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

def prompt
  @prompt ||= TTY::Prompt.new
end

#quitObject



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

def quit
  message "bye!"
  exit
end

#select(msg, table) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pt/io.rb', line 79

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



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
145
146
147
148
149
150
151
152
153
# File 'lib/pt/io.rb', line 101

def select_story_from_paginated_table(options={}, &block)
  prompt_message = "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
    prompt.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_message, 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



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

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

#title(*msg) ⇒ Object



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

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

#user_sObject



54
55
56
# File 'lib/pt/io.rb', line 54

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