Last active
July 4, 2025 14:01
-
-
Save rfletchr/3cb7987b4f636b6eb226c14bc141587e to your computer and use it in GitHub Desktop.
Python Console Prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
wget https://gist.githubusercontent.com/rfletchr/3cb7987b4f636b6eb226c14bc141587e/raw/13d126a43f449204aa5d50635d6a517efa3f1620/prompt.py -O ~/.local/bin/prompt | |
chmod ug+x ~/.local/bin/prompt | |
echo 'export PROMPT_COMMAND=prompt' >> ~/.bashrc | |
""" | |
import sys | |
import os | |
import datetime | |
import subprocess | |
class Builder: | |
colors = [ | |
(30, 46), | |
(30, 44), | |
] | |
arrow_colors = [ | |
(36, 44), | |
(34, 46), | |
] | |
end_colors = [ | |
(36, 40), | |
(34, 40), | |
] | |
PLAY = "\ue0b0" | |
def __init__(self): | |
self.elements = [] | |
def time(self): | |
time = datetime.datetime.now() | |
self.elements.append(time.strftime("\uf017 %H:%M")) | |
return self | |
def basename(self): | |
self.elements.append(" " + os.path.basename(os.getcwd())) | |
return self | |
def ticket(self): | |
name = os.environ.get("TICKET") | |
if name: | |
self.elements.append(f" {name}") | |
return self | |
def branch(self): | |
if os.path.exists(".git"): | |
branch = ( | |
subprocess.check_output(["git", "branch", "--show-current"]) | |
.decode() | |
.strip() | |
) | |
self.elements.append(f" {branch}") | |
return self | |
def iter_blocks(self): | |
yield "┌" | |
for index, element in enumerate(self.elements): | |
fg, bg = self.colors[index % len(self.colors)] | |
format = f"\x1b[0;{fg};{bg}m" | |
result = f"{format} {element} \x1b[0m" | |
yield result | |
if index == len(self.elements) - 1: | |
colors = self.end_colors | |
else: | |
colors = self.arrow_colors | |
fg, bg = colors[index % len(colors)] | |
format = f"\x1b[0;{fg};{bg}m" | |
result = f"{format}{self.PLAY}\x1b[0m" | |
yield result | |
yield "\n└" | |
def format(self): | |
for element in self.iter_blocks(): | |
sys.stdout.write(f"{element}") | |
def venv(self): | |
if os.environ.get("VIRTUAL_ENV"): | |
name = os.path.basename(os.environ["VIRTUAL_ENV"]) | |
self.elements.append(f"{name}") | |
return self | |
if __name__ == "__main__": | |
try: | |
b = Builder() | |
b.basename().ticket().branch().venv().format() | |
except Exception: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment