Class: Uniword::ReviewCLI

Inherits:
Thor
  • Object
show all
Includes:
CLIHelpers
Defined in:
lib/uniword/cli/review_cli.rb

Overview

Review subcommands for Uniword CLI.

Provides commands for reviewing comments and tracked changes:

  • comments: list comments in a document

  • changes: list tracked changes (revisions) in a document

  • accept: accept a single revision by ID

  • reject: reject a single revision by ID

  • accept-all: accept all tracked changes

  • reject-all: reject all tracked changes

  • interactive: step through changes one-by-one

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#accept(path, revision_id) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/uniword/cli/review_cli.rb', line 116

def accept(path, revision_id)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  unless manager.accept(revision_id)
    say "Revision '#{revision_id}' not found.", :red
    exit 1
  end

  save_output(doc, path)
  say "Accepted revision '#{revision_id}'.", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#accept_all(path) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/uniword/cli/review_cli.rb', line 170

def accept_all(path)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  count = manager.accept_all
  save_output(doc, path)

  if count.positive?
    say "Accepted #{count} revision(s).", :green
  else
    say "No revisions to accept.", :yellow
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#changes(path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/uniword/cli/review_cli.rb', line 74

def changes(path)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  revisions = filtered_revisions(manager)

  if revisions.empty?
    say "No tracked changes found.", :yellow
    return
  end

  say "Tracked changes (#{revisions.count}):", :green
  revisions.each_with_index do |rev, idx|
    say "  #{idx + 1}. ID: #{rev.revision_id}"
    say "     Type:   #{format_type(rev.type)}"
    say "     Author: #{rev.author || '(unknown)'}"
    say "     Date:   #{rev.date || '(unknown)'}"

    if options[:verbose]
      say "     Text:   #{rev.text}"
    else
      preview = rev.text&.[](0..40) || "(empty)"
      preview += "..." if rev.text.to_s.length > 40
      say "     Text:   #{preview}"
    end
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#comments(path) ⇒ Object



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
56
57
58
# File 'lib/uniword/cli/review_cli.rb', line 30

def comments(path)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)
  comment_list = manager.list_comments

  if comment_list.empty?
    say "No comments found.", :yellow
    return
  end

  say "Comments (#{comment_list.count}):", :green
  comment_list.each_with_index do |comment, idx|
    say "  #{idx + 1}. ID: #{comment.comment_id}"
    say "     Author: #{comment.author || '(unknown)'}"
    say "     Date:   #{comment.date || '(unknown)'}"

    if options[:verbose]
      say "     Text:   #{comment.text}"
    else
      preview = comment.text&.[](0..60) || "(empty)"
      preview += "..." if comment.text.to_s.length > 60
      say "     Text:   #{preview}"
    end
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#interactive(path) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/uniword/cli/review_cli.rb', line 224

def interactive(path)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  session = Review::InteractiveReview.new(
    manager,
    output: $stdout,
    input: $stdin,
  )
  result = session.run

  if result[:accepted].positive? || result[:rejected].positive?
    save_output(doc, path)
    say "Document updated.", :green
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#reject(path, revision_id) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/uniword/cli/review_cli.rb', line 143

def reject(path, revision_id)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  unless manager.reject(revision_id)
    say "Revision '#{revision_id}' not found.", :red
    exit 1
  end

  save_output(doc, path)
  say "Rejected revision '#{revision_id}'.", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#reject_all(path) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/uniword/cli/review_cli.rb', line 198

def reject_all(path)
  doc = load_document(path)
  manager = Review::ReviewManager.new(doc)

  count = manager.reject_all
  save_output(doc, path)

  if count.positive?
    say "Rejected #{count} revision(s).", :green
  else
    say "No revisions to reject.", :yellow
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end