Skip to content

Commit

Permalink
Clean up some cyclomatic complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyhaines committed Sep 29, 2023
1 parent 727af14 commit 61449c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/topos-playground/break_text.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class ToposPlayground
# ameba:disable Metrics/CyclomaticComplexity

# This is a helper function for nicely formatting the text output from topos-playground.
# It takes a string and breaks it into lines of a maximum length, inserting newlines
# where necessary. It also attempts to break lines at word boundaries, and to avoid
Expand All @@ -17,6 +15,8 @@ class ToposPlayground
# Finally, the algorithm maintains the indetation at the start of a line, when a line is
# broken into multiple lines. This maintains text formatting, such as when examples are
# being provided in an indented section.)
#
# ameba:disable Metrics/CyclomaticComplexity
def self.break_text(str : String, max_line_length : Int32 = 80) : String
return str if max_line_length <= 0

Expand Down
61 changes: 37 additions & 24 deletions src/topos-playground/command/log.cr
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,45 @@ class ToposPlayground::Command::Logs < ToposPlayground::Command
# or in a comma separated list.
def do_prune_range(sorted_logs, selector)
range_parts = /^\s*(\d*)\s*\.\.\s*(\d*)\s*$/.match(selector.to_s)
show_deletion_count(
if range_parts
do_prune_range_range_parts(sorted_logs, range_parts)
else
do_prune_range_selector_split(sorted_logs, selector)
end
)
end

private def do_prune_range_range_parts(sorted_logs, range_parts)
start = range_parts[1].to_i? ? range_parts[1].to_i - 1 : 0
finish = range_parts[2].to_i? ? range_parts[2].to_i - 1 : sorted_logs.size - 1
start = 0 if start < 0
finish = sorted_logs.size - 1 if finish >= sorted_logs.size

if start > finish
start, finish = finish, start
end

count = 0
if range_parts
start = range_parts[1].to_i? ? range_parts[1].to_i - 1 : 0
finish = range_parts[2].to_i? ? range_parts[2].to_i - 1 : sorted_logs.size - 1
start = 0 if start < 0
finish = sorted_logs.size - 1 if finish >= sorted_logs.size

if start > finish
start, finish = finish, start
sorted_logs.reverse[start..finish].each do |log|
count += 1
if config.dry_run?
Log.for("stdout").info { "Would delete #{log[1]}" }
else
Log.for("stdout").info { "Deleting #{log[1]}" } if config.verbose?
File.delete(log[1])
end
end

count
end

sorted_logs.reverse[start..finish].each do |log|
private def do_prune_range_selector_split(sorted_logs, selector)
count = 0
selector.split(",").each do |index|
index = index.to_i?
if index && index > 0 && index <= sorted_logs.size
log = sorted_logs[-index]
count += 1
if config.dry_run?
Log.for("stdout").info { "Would delete #{log[1]}" }
Expand All @@ -295,23 +322,9 @@ class ToposPlayground::Command::Logs < ToposPlayground::Command
File.delete(log[1])
end
end
else
selector.split(",").each do |index|
index = index.to_i?
if index && index > 0 && index <= sorted_logs.size
log = sorted_logs[-index]
count += 1
if config.dry_run?
Log.for("stdout").info { "Would delete #{log[1]}" }
else
Log.for("stdout").info { "Deleting #{log[1]}" } if config.verbose?
File.delete(log[1])
end
end
end
end

show_deletion_count(count)
count
end

# -----
Expand Down

0 comments on commit 61449c3

Please sign in to comment.