Skip to content

Commit

Permalink
[canvas-] change mock window size from 80x25 to 1x1 saulpw#2171
Browse files Browse the repository at this point in the history
Plotter/Canvas/GraphSheet do not have a screen when __init__() is called.
Until they have a screen, DrawablePane.windowWidth and .windowHeight
will give 25 and 80. If the actual terminal is smaller than 80x25.
errors will be caused by drawing past the edges of the terminal.

This commit uses a temporary size of 1x1 in __init__().
True canvas dimensions will be calculated later, with the real
curses screen, by first calling Canvas.refresh(), then waiting
for the next screen draw. During that draw, the sheet's screen
will be provided by drawSheet():
drawSheet(scr, sheet) -> draw(scr) -> render(h, w) -> resetCanvasDimensions(w, h)
  • Loading branch information
midichef committed Aug 6, 2024
1 parent 8f823f5 commit f31f1cb
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions visidata/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(self, *names, **kwargs):
self.labels = [] # (x, y, text, attr, row)
self.hiddenAttrs = set()
self.needsRefresh = False
self.resetCanvasDimensions(self.windowHeight, self.windowWidth)
self.resetCanvasDimensions(1, 1) #2171

@property
def nRows(self):
Expand Down Expand Up @@ -393,6 +393,12 @@ def resetCanvasDimensions(self, windowHeight, windowWidth):
if self.cursorBox.xmin == self.visibleBox.xmin and self.cursorBox.ymin == self.calcBottomCursorY():
realign_cursor = True
super().resetCanvasDimensions(windowHeight, windowWidth)
# if window is not big enough to contain a particular margin, pretend that margin is 0
pvbox_x = pvbox_y = 0
if self.plotwidth > self.left_margin:
pvbox_x = self.left_margin
if self.plotheight > self.topMarginPixels:
pvbox_y = self.topMarginPixels
if hasattr(self, 'legendwidth'):
# +4 = 1 empty space after the graph + 2 characters for the legend prefixes of "1:", "2:", etc +
# 1 character for the empty rightmost column
Expand All @@ -403,8 +409,10 @@ def resetCanvasDimensions(self, windowHeight, windowWidth):
else:
pvbox_xmax = self.plotwidth-self.rightMarginPixels-1
self.left_margin = min(self.left_margin, math.ceil(self.plotwidth * 1/3)//2*2)
self.plotviewBox = BoundingBox(self.left_margin, self.topMarginPixels,
pvbox_xmax, self.plotheight-self.bottomMarginPixels-1)
# enforce a minimum plotview box size of 1x1
pvbox_xmax = max(pvbox_xmax, 1)
pvbox_ymax = max(self.plotheight-self.bottomMarginPixels-1, 1)
self.plotviewBox = BoundingBox(pvbox_x, pvbox_y, pvbox_xmax, pvbox_ymax)
if [self.plotheight, self.plotwidth] != old_plotsize:
if hasattr(self, 'cursorBox') and self.cursorBox:
self.setCursorSizeInPlotterPixels(2, 4)
Expand Down

0 comments on commit f31f1cb

Please sign in to comment.