Skip to content

Instantly share code, notes, and snippets.

@yyk
Created August 4, 2022 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyk/9735d4df2b3c7dd0ea0d35affbb8c77e to your computer and use it in GitHub Desktop.
Save yyk/9735d4df2b3c7dd0ea0d35affbb8c77e to your computer and use it in GitHub Desktop.
checkout with ncurses
from curses import wrapper
import curses
from git import Repo
def main(stdscr, path):
repo = Repo(path)
branches = list(repo.branches)
branches.sort(key=lambda x: x.commit.committed_date, reverse=True)
branches = branches[:min(len(branches), curses.LINES - 1)]
current = repo.active_branch
curses.noecho()
stdscr.scrollok(True)
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_GREEN)
selection = 0
def refresh():
stdscr.clear()
for i, branch in enumerate(branches):
if i == selection:
color = 1
else:
color = 0
if branch == current:
stdscr.addstr('* ')
else:
stdscr.addstr(' ')
stdscr.addstr(branch.name + '\n', curses.color_pair(color))
stdscr.refresh()
while True:
refresh()
a = stdscr.getch()
print(a)
if a == ord('q'):
print("Quitting")
exit(0)
elif a == curses.KEY_DOWN:
print("DOWN")
selection += 1
selection = selection % len(branches)
elif a == curses.KEY_UP:
print("UP")
selection -= 1
if selection < 0:
selection = len(branches) - 1
elif a == curses.KEY_ENTER or a == 10:
branches[selection].checkout()
return branches[selection].name
if __name__ == '__main__':
co = wrapper(main, path=".")
print("Checked out " + co)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment