Skip to content

Commit 1753dd5

Browse files
committed
Guard BarplotGraphics#preprocess against empty bars to prevent max_by crash
1 parent 3953275 commit 1753dd5

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/unicode_plot/graphics/bar_graphics.cr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,19 @@ module UnicodePlot
7575
end
7676

7777
def preprocess(io : IO) : GraphicsArea ->
78+
if @bars.empty?
79+
@max_val = Math.max(0.0, @maximum)
80+
@max_len = 0
81+
return ->(grph : GraphicsArea) {
82+
bg = grph.as(BarplotGraphics)
83+
bg.max_val = -Float64::INFINITY
84+
bg.max_len = 0
85+
}
86+
end
7887
scaled = @bars.map { |bar| @xscale.call(bar) }
7988
max_val, max_idx = scaled.each_with_index.max_by { |v, _| v }
8089
@max_val = Math.max(max_val || -Float64::INFINITY, @maximum)
81-
@max_len = @bars.empty? ? 0 : @formatter.call(@bars[max_idx || 0]).size
90+
@max_len = @formatter.call(@bars[max_idx || 0]).size
8291
->(grph : GraphicsArea) {
8392
bg = grph.as(BarplotGraphics)
8493
bg.max_val = -Float64::INFINITY

0 commit comments

Comments
 (0)