Class: GithubDash::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/github_dash/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
16
# File 'lib/github_dash/cli.rb', line 12

def initialize(*args)
  super
  @prompt = TTY::Prompt.new
  @pastel = Pastel.new
end

Instance Method Details

#add_repo(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/github_dash/cli.rb', line 19

def add_repo(name)
  # Keep looping while the user is choosing different tokens
  token = nil
  loop do
    begin
      # Try to successfully add repository
      GithubDash::add_repo_to_following(name, token)
      @prompt.say "Added #{name} to followed repositories."
      break
    rescue ArgumentError
      # If repo is already follewed, just break
      @prompt.say "Repository is already followed!"
      break
    rescue Octokit::NotFound, Octokit::Unauthorized
      # Prompt user to enter another token
      @prompt.say "Could not find #{name} on github using #{@pastel.bright_blue(token)}."
      ans = @prompt.yes? "Do you want to try with a different token?"

      # break if they said no
      break unless ans

      # Make menu for them to choose a toekn
      token = @prompt.select "Chose a token" do |menu|
        tokens = GithubDash::DataDepository.get_all_tokens
        # There must be at least 1 token to choose from
        if tokens.size < 1
          @prompt.say @pastel.red("No tokens added. Add a token with add_token.")
          return
        end
        # Add a choice per token
        tokens.map do |t|
          menu.choice(t[:name], t[:token])
        end
      end
    end
  end
end

#add_token(token) ⇒ Object



85
86
87
88
# File 'lib/github_dash/cli.rb', line 85

def add_token(token)
  GithubDash::add_token(token, options[:token_name])
  @prompt.say "Added #{@pastel.bright_blue(options[:token_name])}"
end

#compare_reviewObject



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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/github_dash/cli.rb', line 119

def compare_review
  # First, create headers, which is just
  #   the user's option aligned center
  headers = options[:users].map {|u| {value: u, alignment: :center}}

  # Create a new table
  table = TTY::Table.new :header => headers do |t|

    # Create empty 2D array
    rows = Array.new(options[:users].count) { [] }

    # Fetch the repo
    repo = GithubDash::fetch_repository options[:repo_name]

    # Get the commit messages for each user
    options[:users].each_with_index do |val, i|
      commits = repo.get_commits(10, val)

      # Add their messages to the rwos array
      commits.each_with_index do |c, c_i|
        rows[i].push c.commit.message.split("\n").first
      end
    end
    # Pads the rows until they are all equal size
    mx_size = rows.map(&:size).max
    rows.each {|r| r.fill(nil, r.count, mx_size - r.count) }

    # Since we have one user per row, but the table will have one user per column,
    #   transpose the rows before adding them
    rows.transpose.each do |r|
      t << r
    end
  end

  # Tell user what repos we are comparing
  @prompt.say "\n"
  @prompt.say "Comparing commits from #{@pastel.bright_green(options[:repo_name])}" \
          " in the last #{@pastel.bright_green(options[:days])}".center(table.width)
  @prompt.say "\n"

  # Pastel colors only show up when the table is saved as a string first
  table_str = table.render(:unicode) do |r|
    r.filter = Proc.new do |val, row_index, col_index|
      if row_index == 0
        @pastel.yellow(val)
      else
        @pastel.bright_blue(val)
      end
    end
    r.padding = [0, 1]
  end

  # Print the table
  @prompt.say table_str
end

#followingObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/github_dash/cli.rb', line 177

def following

  # Gather all repos
  repos = {}
  GithubDash::get_following.each do |r|
    repos[r] = GithubDash::fetch_repository r
  end

  # Loop while liveupdating
  loop do
    begin
      # Create table
      table = TTY::Table.new(header: ["Repository", "PRs in the last week", "Commits in the last week"]) do |t|
        # Add each repo's data to the table
        repos.each do |r, val|
          repos.fetch(r).update_commits 100
          repos.fetch(r).update_pull_requests 100
          t << [val.data.full_name, val.get_pull_requests.size, val.get_commits.size]
        end
      end

      # Get the table's string
      table_str = table.render(:unicode) do |r|
        r.filter = Proc.new do |val, row, col|
          # The headers are yellow
          if row == 0
            @pastel.yellow(val)
          else
            # Set different colors for different columns
            case col
            when 0
              @pastel.bright_blue val
            when 1
              @pastel.bright_green val
            when 2
              @pastel.bright_red val
            else
              val
            end
          end
        end
        r.padding = [0, 1]
      end

      # Clear the screen and move the cursor to the too left
      #   Note: This must be done before printing the table
      if options[:liveupdate]
        print TTY::Cursor.clear_screen
        print TTY::Cursor.move_to
        print TTY::Cursor.hide
      end
      # Print the table
      @prompt.say table_str

      # If not live updating, break
      break unless options[:liveupdate]
      # Update every 10 seconds
      sleep 10
    ensure
      # If the user quits, show the cursor
      #   otherwise the cursor won't show in regular terminal
      print TTY::Cursor.show
    end
  end
end

#loginObject



73
74
75
76
77
78
79
80
81
# File 'lib/github_dash/cli.rb', line 73

def 

  # Prompt for username/password
  username = @prompt.ask("Enter username: ")
  password = @prompt.ask("Enter password: ", echo: false)

  GithubDash::add_user(username, password)
  @prompt.say "Added #{@pastel.bright_blue(username)}"
end

#remove_reposObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/github_dash/cli.rb', line 58

def remove_repos
  # Prompt user for repos
  repos = @prompt.multi_select "Select which repos to remove" do |menu|
    GithubDash::DataDepository.get_following.each do |r|
      menu.choice r
    end
  end
  # Delete each one
  repos.each do |name|
    GithubDash::remove_repo_from_following(name)
  end
  @prompt.say "Removed #{repos.size} repositories."
end

#remove_tokensObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/github_dash/cli.rb', line 91

def remove_tokens

  # Get the tokens to remove
  remove_tokens = @prompt.multi_select "Select which tokens to remove" do |menu|
    GithubDash::DataDepository.get_all_tokens.each do |t|
      menu.choice t[:name], t[:token]
    end
  end
  @prompt.say "You are removing #{@pastel.bright_blue(remove_tokens.size)} tokens"

  # Double check
  if @prompt.yes?("Proceed: ")

    # Remove the tokens
    remove_tokens.each do |t|
      GithubDash::DataDepository.delete_token(t)
    end

    @prompt.say "Removed #{@pastel.bright_blue(remove_tokens.size)} tokens."
  else
    @prompt.say "Cancelled"
  end
end

#repo(name) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/github_dash/cli.rb', line 245

def repo(name)
  # Fetch repo information
  repo = GithubDash::fetch_repository name

  # Create a table for this repo's informaion
  table = TTY::Table.new header: ["Commit Date", "Commit Title", "Commit Author"] do |t|
    repo.get_commits(options[:days]).each do |c|
      t << [c.commit.author.date, c.commit.message.split("\n").first, c.commit.author.name]
    end
  end

  # print title for the table
  @prompt.say "\n"
  @prompt.say @pastel.yellow(repo.data.full_name)
  @prompt.say "Commits from the last #{@pastel.bright_green(options[:days])} day#{"s" if options[:days] > 1}"
  table_str = table.render(:unicode) do |r|
    r.filter = Proc.new do |val, row_index, col_index|

      # Headers are yellow
      if row_index == 0
        @pastel.yellow(val)
      else
        # columns have different colors
        case col_index
        when 0
          @pastel.bright_blue(val)
        when 1
          @pastel.bright_red(val)
        when 2
          @pastel.green(val)
        else
          val
        end
      end
    end
    r.padding = [0, 1]
  end
  # Print the table
  @prompt.say table_str
end