Add vertical separator + basic scrolling
This commit is contained in:
parent
58c7866468
commit
c2ef9f47e8
|
|
@ -1,6 +1,4 @@
|
||||||
# by ChatGPT
|
# by ChatGPT
|
||||||
|
|
||||||
|
|
||||||
import curses
|
import curses
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -17,46 +15,75 @@ def get_file_at_commit(commit_hash, filename):
|
||||||
|
|
||||||
def main(stdscr, filename):
|
def main(stdscr, filename):
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
|
curses.mousemask(curses.ALL_MOUSE_EVENTS)
|
||||||
stdscr.keypad(True)
|
stdscr.keypad(True)
|
||||||
|
|
||||||
commits = get_commits(filename)
|
commits = get_commits(filename)
|
||||||
selected = 0
|
selected_commit = 0
|
||||||
|
divider_col = 40
|
||||||
|
focus = "left"
|
||||||
|
scroll_offset = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
stdscr.clear()
|
stdscr.clear()
|
||||||
height, width = stdscr.getmaxyx()
|
height, width = stdscr.getmaxyx()
|
||||||
mid = width // 2
|
|
||||||
|
|
||||||
# Draw commit list on the left
|
# Draw commit list on the left
|
||||||
for i, line in enumerate(commits):
|
for i in range(min(len(commits), height)):
|
||||||
if i >= height:
|
line = commits[i]
|
||||||
break
|
if i == selected_commit and focus == "left":
|
||||||
if i == selected:
|
|
||||||
stdscr.attron(curses.A_REVERSE)
|
stdscr.attron(curses.A_REVERSE)
|
||||||
stdscr.addnstr(i, 0, line, mid - 1)
|
stdscr.addnstr(i, 0, line, divider_col - 1)
|
||||||
if i == selected:
|
if i == selected_commit and focus == "left":
|
||||||
stdscr.attroff(curses.A_REVERSE)
|
stdscr.attroff(curses.A_REVERSE)
|
||||||
|
|
||||||
# Show file content on the right
|
# Draw vertical divider with box-drawing character
|
||||||
|
for y in range(height):
|
||||||
|
stdscr.addch(y, divider_col, '│')
|
||||||
|
|
||||||
|
# Draw file content on the right
|
||||||
if commits:
|
if commits:
|
||||||
commit_hash = commits[selected].split()[0]
|
commit_hash = commits[selected_commit].split()[0]
|
||||||
file_lines = get_file_at_commit(commit_hash, filename)
|
file_lines = get_file_at_commit(commit_hash, filename)
|
||||||
for i, line in enumerate(file_lines):
|
visible_lines = file_lines[scroll_offset:scroll_offset + height]
|
||||||
if i >= height:
|
for i, line in enumerate(visible_lines):
|
||||||
break
|
stdscr.addnstr(i, divider_col + 2, line, width - divider_col - 3)
|
||||||
stdscr.addnstr(i, mid + 1, line, width - mid - 2)
|
|
||||||
|
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
|
|
||||||
key = stdscr.getch()
|
key = stdscr.getch()
|
||||||
if key in [ord('q'), 27]: # q or ESC to quit
|
|
||||||
|
if key == curses.KEY_MOUSE:
|
||||||
|
try:
|
||||||
|
_, mx, my, _, bstate = curses.getmouse()
|
||||||
|
if bstate & curses.BUTTON1_CLICKED:
|
||||||
|
focus = "left" if mx <= divider_col else "right"
|
||||||
|
except curses.error:
|
||||||
|
pass
|
||||||
|
|
||||||
|
elif key in [ord('q'), 27]:
|
||||||
break
|
break
|
||||||
elif key in [curses.KEY_DOWN, ord('j')]:
|
|
||||||
if selected < len(commits) - 1:
|
elif focus == "left":
|
||||||
selected += 1
|
if key in [curses.KEY_DOWN, ord('j')]:
|
||||||
|
if selected_commit < len(commits) - 1:
|
||||||
|
selected_commit += 1
|
||||||
|
scroll_offset = 0 # reset right pane scroll
|
||||||
elif key in [curses.KEY_UP, ord('k')]:
|
elif key in [curses.KEY_UP, ord('k')]:
|
||||||
if selected > 0:
|
if selected_commit > 0:
|
||||||
selected -= 1
|
selected_commit -= 1
|
||||||
|
scroll_offset = 0
|
||||||
|
|
||||||
|
elif focus == "right":
|
||||||
|
if key == curses.KEY_DOWN or key == ord('j'):
|
||||||
|
scroll_offset += 1
|
||||||
|
elif key == curses.KEY_UP or key == ord('k'):
|
||||||
|
if scroll_offset > 0:
|
||||||
|
scroll_offset -= 1
|
||||||
|
elif key == curses.KEY_NPAGE: # Page Down
|
||||||
|
scroll_offset += height
|
||||||
|
elif key == curses.KEY_PPAGE: # Page Up
|
||||||
|
scroll_offset = max(0, scroll_offset - height)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue