Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #2991 by adjusting the y scroll after max_lines exceeded #3005

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/textual/widgets/_text_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,20 @@ def write(
strip.adjust_cell_length(render_width)
self.lines.extend(strips)

y_adjust: int = 0
if self.max_lines is not None and len(self.lines) > self.max_lines:
self._start_line += len(self.lines) - self.max_lines
self.refresh()
y_adjust = len(self.lines) - self.max_lines
self._start_line += y_adjust
self.lines = self.lines[-self.max_lines :]
self.refresh()

self.virtual_size = Size(self.max_width, len(self.lines))

if auto_scroll:
self.scroll_end(animate=False)
elif y_adjust > 0:
scroll_x, scroll_y = self.scroll_offset
self.scroll_to(y=max(0, scroll_y - y_adjust), animate=False)

return self

Expand Down
156 changes: 156 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -26214,6 +26214,162 @@

'''
# ---
# name: test_textlog_max_lines_noautoscroll
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io -->
<style>

@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}

.terminal-817664824-matrix {
font-family: Fira Code, monospace;
font-size: 20px;
line-height: 24.4px;
font-variant-east-asian: full-width;
}

.terminal-817664824-title {
font-size: 18px;
font-weight: bold;
font-family: arial;
}

.terminal-817664824-r1 { fill: #e1e1e1 }
.terminal-817664824-r2 { fill: #c5c8c6 }
.terminal-817664824-r3 { fill: #14191f }
</style>

<defs>
<clipPath id="terminal-817664824-clip-terminal">
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
</clipPath>
<clipPath id="terminal-817664824-line-0">
<rect x="0" y="1.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-1">
<rect x="0" y="25.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-2">
<rect x="0" y="50.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-3">
<rect x="0" y="74.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-4">
<rect x="0" y="99.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-5">
<rect x="0" y="123.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-6">
<rect x="0" y="147.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-7">
<rect x="0" y="172.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-8">
<rect x="0" y="196.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-9">
<rect x="0" y="221.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-10">
<rect x="0" y="245.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-11">
<rect x="0" y="269.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-12">
<rect x="0" y="294.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-13">
<rect x="0" y="318.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-14">
<rect x="0" y="343.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-15">
<rect x="0" y="367.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-16">
<rect x="0" y="391.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-17">
<rect x="0" y="416.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-18">
<rect x="0" y="440.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-19">
<rect x="0" y="465.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-20">
<rect x="0" y="489.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-21">
<rect x="0" y="513.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-817664824-line-22">
<rect x="0" y="538.3" width="976" height="24.65"/>
</clipPath>
</defs>

<rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-817664824-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">TextLogLines</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>

<g transform="translate(9, 41)" clip-path="url(#terminal-817664824-clip-terminal)">
<rect fill="#1e1e1e" x="0" y="1.5" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="1.5" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="1.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="25.9" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="25.9" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="50.3" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="50.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="74.7" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="99.1" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="99.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="123.5" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="123.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="147.9" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="147.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="172.3" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="172.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="196.7" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="196.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="221.1" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="221.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="245.5" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="245.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="269.9" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="269.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="294.3" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="294.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="318.7" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="318.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="343.1" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="343.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="367.5" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="367.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="391.9" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="391.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="416.3" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="416.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="440.7" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="440.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="465.1" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#23568b" x="951.6" y="465.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="489.5" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="489.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="513.9" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="513.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="538.3" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="538.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="158.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="158.6" y="562.7" width="793" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="562.7" width="24.4" height="24.65" shape-rendering="crispEdges"/>
<g class="terminal-817664824-matrix">
<text class="terminal-817664824-r1" x="0" y="20" textLength="158.6" clip-path="url(#terminal-817664824-line-0)">Key&#160;press&#160;#21</text><text class="terminal-817664824-r2" x="976" y="20" textLength="12.2" clip-path="url(#terminal-817664824-line-0)">
</text><text class="terminal-817664824-r1" x="0" y="44.4" textLength="158.6" clip-path="url(#terminal-817664824-line-1)">Key&#160;press&#160;#22</text><text class="terminal-817664824-r2" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-817664824-line-1)">
</text><text class="terminal-817664824-r1" x="0" y="68.8" textLength="158.6" clip-path="url(#terminal-817664824-line-2)">Key&#160;press&#160;#23</text><text class="terminal-817664824-r2" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-817664824-line-2)">
</text><text class="terminal-817664824-r1" x="0" y="93.2" textLength="158.6" clip-path="url(#terminal-817664824-line-3)">Key&#160;press&#160;#24</text><text class="terminal-817664824-r2" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-817664824-line-3)">
</text><text class="terminal-817664824-r1" x="0" y="117.6" textLength="158.6" clip-path="url(#terminal-817664824-line-4)">Key&#160;press&#160;#25</text><text class="terminal-817664824-r2" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-817664824-line-4)">
</text><text class="terminal-817664824-r1" x="0" y="142" textLength="158.6" clip-path="url(#terminal-817664824-line-5)">Key&#160;press&#160;#26</text><text class="terminal-817664824-r2" x="976" y="142" textLength="12.2" clip-path="url(#terminal-817664824-line-5)">
</text><text class="terminal-817664824-r1" x="0" y="166.4" textLength="158.6" clip-path="url(#terminal-817664824-line-6)">Key&#160;press&#160;#27</text><text class="terminal-817664824-r2" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-817664824-line-6)">
</text><text class="terminal-817664824-r1" x="0" y="190.8" textLength="158.6" clip-path="url(#terminal-817664824-line-7)">Key&#160;press&#160;#28</text><text class="terminal-817664824-r2" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-817664824-line-7)">
</text><text class="terminal-817664824-r1" x="0" y="215.2" textLength="158.6" clip-path="url(#terminal-817664824-line-8)">Key&#160;press&#160;#29</text><text class="terminal-817664824-r2" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-817664824-line-8)">
</text><text class="terminal-817664824-r1" x="0" y="239.6" textLength="158.6" clip-path="url(#terminal-817664824-line-9)">Key&#160;press&#160;#30</text><text class="terminal-817664824-r2" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-817664824-line-9)">
</text><text class="terminal-817664824-r1" x="0" y="264" textLength="158.6" clip-path="url(#terminal-817664824-line-10)">Key&#160;press&#160;#31</text><text class="terminal-817664824-r2" x="976" y="264" textLength="12.2" clip-path="url(#terminal-817664824-line-10)">
</text><text class="terminal-817664824-r1" x="0" y="288.4" textLength="158.6" clip-path="url(#terminal-817664824-line-11)">Key&#160;press&#160;#32</text><text class="terminal-817664824-r2" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-817664824-line-11)">
</text><text class="terminal-817664824-r1" x="0" y="312.8" textLength="158.6" clip-path="url(#terminal-817664824-line-12)">Key&#160;press&#160;#33</text><text class="terminal-817664824-r2" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-817664824-line-12)">
</text><text class="terminal-817664824-r1" x="0" y="337.2" textLength="158.6" clip-path="url(#terminal-817664824-line-13)">Key&#160;press&#160;#34</text><text class="terminal-817664824-r2" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-817664824-line-13)">
</text><text class="terminal-817664824-r1" x="0" y="361.6" textLength="158.6" clip-path="url(#terminal-817664824-line-14)">Key&#160;press&#160;#35</text><text class="terminal-817664824-r2" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-817664824-line-14)">
</text><text class="terminal-817664824-r1" x="0" y="386" textLength="158.6" clip-path="url(#terminal-817664824-line-15)">Key&#160;press&#160;#36</text><text class="terminal-817664824-r2" x="976" y="386" textLength="12.2" clip-path="url(#terminal-817664824-line-15)">
</text><text class="terminal-817664824-r1" x="0" y="410.4" textLength="158.6" clip-path="url(#terminal-817664824-line-16)">Key&#160;press&#160;#37</text><text class="terminal-817664824-r2" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-817664824-line-16)">
</text><text class="terminal-817664824-r1" x="0" y="434.8" textLength="158.6" clip-path="url(#terminal-817664824-line-17)">Key&#160;press&#160;#38</text><text class="terminal-817664824-r2" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-817664824-line-17)">
</text><text class="terminal-817664824-r1" x="0" y="459.2" textLength="158.6" clip-path="url(#terminal-817664824-line-18)">Key&#160;press&#160;#39</text><text class="terminal-817664824-r2" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-817664824-line-18)">
</text><text class="terminal-817664824-r1" x="0" y="483.6" textLength="158.6" clip-path="url(#terminal-817664824-line-19)">Key&#160;press&#160;#40</text><text class="terminal-817664824-r3" x="951.6" y="483.6" textLength="24.4" clip-path="url(#terminal-817664824-line-19)">▆▆</text><text class="terminal-817664824-r2" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-817664824-line-19)">
</text><text class="terminal-817664824-r1" x="0" y="508" textLength="158.6" clip-path="url(#terminal-817664824-line-20)">Key&#160;press&#160;#41</text><text class="terminal-817664824-r2" x="976" y="508" textLength="12.2" clip-path="url(#terminal-817664824-line-20)">
</text><text class="terminal-817664824-r1" x="0" y="532.4" textLength="158.6" clip-path="url(#terminal-817664824-line-21)">Key&#160;press&#160;#42</text><text class="terminal-817664824-r2" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-817664824-line-21)">
</text><text class="terminal-817664824-r1" x="0" y="556.8" textLength="158.6" clip-path="url(#terminal-817664824-line-22)">Key&#160;press&#160;#43</text><text class="terminal-817664824-r2" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-817664824-line-22)">
</text><text class="terminal-817664824-r1" x="0" y="581.2" textLength="158.6" clip-path="url(#terminal-817664824-line-23)">Key&#160;press&#160;#44</text>
</g>
</g>
</svg>

'''
# ---
# name: test_textlog_scroll
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from textual.app import App
from textual.widgets import TextLog


class TextLogLines(App):
count = 0

def compose(self):
yield TextLog(max_lines=30, auto_scroll=False)

async def on_key(self):
self.count += 1
log_widget = self.query_one(TextLog)
log_widget.write(f"Key press #{self.count}")


app = TextLogLines()

if __name__ == "__main__":
app.run()
Loading