IPython prompt

This commit is contained in:
Kujiu 2023-09-30 01:02:41 +02:00
parent 1da080f18d
commit 62f260def1
Signed by: kujiu
GPG key ID: ABBB2CAC6855599F
4 changed files with 164 additions and 61 deletions

View file

@ -13,62 +13,7 @@ IPython configuration:
.. code:: python .. code:: python
from IPython.terminal.prompts import Prompts, RichPromptDisplayHook from pygments_nightfox_style.ipython_prompt import NightfoxPrompts
from pygments.token import Token
from prompt_toolkit.enums import EditingMode
from platform import python_version
import os
import socket
import subprocess
class NightfoxPrompts(Prompts): c = get_config()
def vi_mode(self): c.TerminalInteractiveShell.prompts_class = NightfoxPrompts
if (getattr(self.shell.pt_app, 'editing_mode', None) == EditingMode.VI
and self.shell.prompt_includes_vi_mode):
mode = str(self.shell.pt_app.app.vi_state.input_mode)
if "INSERT" in mode:
return 'I'
if "NAV" in mode:
return 'N'
if "REPLACE_SINGLE" in mode:
return 'r'
if "REPLACE" in mode:
return 'R'
return 'E' # Emacs mode
def in_prompt_tokens(self):
git_branch = ""
try:
git_branch = subprocess.check_output(
["git", "branch", "--show-current"], stderr=subprocess.DEVNULL
)
if git_branch:
git_branch = git_branch.decode("utf-8").replace("\n", "")
except subprocess.CalledProcessError:
pass
git_branch = str(git_branch)
venv = (os.environ.get("VIRTUAL_ENV") or "").split("/")[-1]
return [
(Token.IPython.Prompt.Login, os.getlogin()),
(Token.IPython.Prompt.Login.Sep, ''),
(Token.IPython.Prompt.Host, socket.gethostname()),
(Token.IPython.Prompt.Host.Sep, ''),
(Token.IPython.Prompt.Venv, venv),
(Token.IPython.Prompt.Venv.Sep, ''),
(Token.IPython.Prompt.Version, ' ' + python_version()),
(Token.IPython.Prompt.Version.Sep, ''),
(Token, '\n'),
(Token.IPython.Prompt.Cwd, os.getcwd()),
(Token.IPython.Prompt.Cwd.Sep, ''),
(Token.IPython.Prompt.Vcs, str(' ') + git_branch if git_branch else ''),
(Token.IPython.Prompt.Vcs.Sep, ''),
(Token, '\n'),
(Token.IPython.Prompt.Mode, self.vi_mode()),
(Token.IPython.Prompt.Mode.Sep, ' '),
]
def out_prompt_tokens(self):
return []

View file

@ -180,14 +180,29 @@ def get_styles(background_color: str) -> Dict[Any, str]:
Token.IPython.Prompt.Host.Sep: NFOX_BLUE + " bg:" + NFOX_FG3, Token.IPython.Prompt.Host.Sep: NFOX_BLUE + " bg:" + NFOX_FG3,
Token.IPython.Prompt.Venv: NFOX_BG0 + " bg:" + NFOX_FG3, Token.IPython.Prompt.Venv: NFOX_BG0 + " bg:" + NFOX_FG3,
Token.IPython.Prompt.Venv.Sep: NFOX_FG3 + " bg:" + NFOX_GREEN, Token.IPython.Prompt.Venv.Sep: NFOX_FG3 + " bg:" + NFOX_GREEN,
Token.IPython.Prompt.Version: NFOX_BG0 + " bg:" + NFOX_GREEN, Token.IPython.Prompt.Venv.Error.Sep: NFOX_FG3 + " bg:" + NFOX_RED,
Token.IPython.Prompt.Version.Sep: "bg: " + NFOX_GREEN, Token.IPython.Prompt.Status: NFOX_BG0 + " bg:" + NFOX_GREEN,
Token.IPython.Prompt.Status.Sep: "bg: " + NFOX_GREEN,
Token.IPython.Prompt.Status.Error: NFOX_BG0 + " bg:" + NFOX_RED,
Token.IPython.Prompt.Status.Error.Sep: "bg: " + NFOX_RED,
Token.IPython.Prompt.Cwd: NFOX_BG0 + " bg:" + NFOX_GREEN, Token.IPython.Prompt.Cwd: NFOX_BG0 + " bg:" + NFOX_GREEN,
Token.IPython.Prompt.Cwd.Sep: NFOX_GREEN + " bg:" + NFOX_BG2, Token.IPython.Prompt.Cwd.Sep: NFOX_GREEN + " bg:" + NFOX_BG2,
Token.IPython.Prompt.Cwd.Root: NFOX_BG0 + " bg:" + NFOX_RED,
Token.IPython.Prompt.Cwd.Root.Sep: NFOX_RED + " bg:" + NFOX_BG2,
Token.IPython.Prompt.Vcs: NFOX_ORANGE + " bg:" + NFOX_BG2, Token.IPython.Prompt.Vcs: NFOX_ORANGE + " bg:" + NFOX_BG2,
Token.IPython.Prompt.Vcs.Sep: "bg: " + NFOX_BG2, Token.IPython.Prompt.Vcs.Sep: "bg: " + NFOX_BG2,
Token.IPython.Prompt.Mode: NFOX_BG0 + " bg:" + NFOX_GREEN, Token.IPython.Prompt.Mode: NFOX_BG0 + " bg:" + NFOX_GREEN,
Token.IPython.Prompt.Mode.Sep: "bg: " + NFOX_GREEN, Token.IPython.Prompt.Mode.Sep: "bg: " + NFOX_GREEN,
Token.IPython.Prompt.Mode.Nav: NFOX_BG0 + " bg:" + NFOX_RED,
Token.IPython.Prompt.Mode.Nav.Sep: "bg: " + NFOX_RED,
Token.IPython.Prompt.Mode.Insert: NFOX_BG0 + " bg:" + NFOX_YELLOW,
Token.IPython.Prompt.Mode.Insert.Sep: "bg: " + NFOX_YELLOW,
Token.IPython.Prompt.Mode.Replace: NFOX_BG0 + " bg:" + NFOX_BLUE,
Token.IPython.Prompt.Mode.Replace.Sep: "bg: " + NFOX_BLUE,
Token.IPython.Prompt.Mode.ReplaceOne: NFOX_BG0 + " bg:" + NFOX_CYAN,
Token.IPython.Prompt.Mode.ReplaceOne.Sep: "bg: " + NFOX_CYAN,
Token.IPython.Prompt.Mode.Visual: NFOX_BG0 + " bg:" + NFOX_MAGENTA,
Token.IPython.Prompt.Mode.Visual.Sep: "bg: " + NFOX_MAGENTA,
} }
class NightfoxStyle(Style): class NightfoxStyle(Style):

View file

@ -0,0 +1,142 @@
from IPython.terminal.prompts import Prompts
from IPython.core.getipython import get_ipython
from pygments.token import Token
from prompt_toolkit.enums import EditingMode
from platform import python_version
import os
import socket
import subprocess
import sys
from typing_extensions import override
from typing import List, Any, Tuple
TOKENS_MODE = {
'V': (
Token.IPython.Prompt.Mode.Visual,
Token.IPython.Prompt.Mode.Visual.Sep,
Token.IPython.Prompt.Mode.Visual.Sep.Root,
),
'I': (
Token.IPython.Prompt.Mode.Insert,
Token.IPython.Prompt.Mode.Insert.Sep,
Token.IPython.Prompt.Mode.Insert.Sep.Root,
),
'N': (
Token.IPython.Prompt.Mode.Nav,
Token.IPython.Prompt.Mode.Nav.Sep,
Token.IPython.Prompt.Mode.Nav.Sep.Root,
),
'R': (
Token.IPython.Prompt.Mode.Replace,
Token.IPython.Prompt.Mode.Replace.Sep,
Token.IPython.Prompt.Mode.Replace.Sep.Root,
),
'r': (
Token.IPython.Prompt.Mode.ReplaceOne,
Token.IPython.Prompt.Mode.ReplaceOne.Sep,
Token.IPython.Prompt.Mode.ReplaceOne.Sep.Root,
),
'E': (
Token.IPython.Prompt.Mode,
Token.IPython.Prompt.Mode.Sep,
Token.IPython.Prompt.Mode.Sep.Root,
)
}
class NightfoxPrompts(Prompts):
@override
def vi_mode(self) -> str:
if (getattr(self.shell.pt_app, 'editing_mode', None) == EditingMode.VI
and self.shell.prompt_includes_vi_mode):
mode = str(self.shell.pt_app.app.vi_state.input_mode)
if "VISUAL" in mode:
return 'V'
if "INSERT" in mode:
return 'I'
if "NAV" in mode:
return 'N'
if "REPLACE_SINGLE" in mode:
return 'r'
if "REPLACE" in mode:
return 'R'
return 'E' # Emacs mode
@override
def in_prompt_tokens(self) -> List[Tuple[Any, Any]]:
git_branch = ""
try:
git_branch = subprocess.check_output(
["git", "branch", "--show-current"], stderr=subprocess.DEVNULL
)
if git_branch:
git_branch = git_branch.decode("utf-8").replace("\n", "")
except subprocess.CalledProcessError:
pass
git_branch = str(git_branch)
cwd = str(os.getcwd())
term_width = int(os.get_terminal_size().columns)
diff_width = term_width - len(cwd) - len(git_branch) - 4
if diff_width < 0 and diff_width > -1 - len(cwd):
cwd = '' + cwd[1-diff_width:]
venv = (os.environ.get("VIRTUAL_ENV") or "").split("/")[-1]
if venv:
venv += '|'
vi_mode = self.vi_mode()
login = os.getlogin()
token_cwd = Token.IPython.Prompt.Cwd
token_cwd_sep = Token.IPython.Prompt.Cwd.Sep
token_mode = TOKENS_MODE[vi_mode][0]
token_mode_sep = TOKENS_MODE[vi_mode][1]
token_status = Token.IPython.Prompt.Status
token_status_sep = Token.IPython.Prompt.Status.Sep
token_venv_sep = Token.IPython.Prompt.Venv.Sep
ip = get_ipython()
last_error = 'NoneType'
if ip and ip.last_execution_result:
last_error = type(
ip.last_execution_result.error_in_exec
).__name__
if last_error == 'NoneType':
last_error = ''
else:
last_error = '' + last_error
token_status = Token.IPython.Prompt.Status.Error
token_status_sep = Token.IPython.Prompt.Status.Error.Sep
token_venv_sep = Token.IPython.Prompt.Venv.Error.Sep
if login == 'root':
token_cwd = Token.IPython.Prompt.Cwd.Root
token_cwd_sep = Token.IPython.Prompt.Cwd.Root.Sep
token_mode_sep = TOKENS_MODE[vi_mode][2]
return [
(Token.IPython.Prompt.Login, login),
(Token.IPython.Prompt.Login.Sep, ''),
(Token.IPython.Prompt.Host, socket.gethostname()),
(Token.IPython.Prompt.Host.Sep, ''),
(Token.IPython.Prompt.Venv, venv + '' + python_version()),
(token_venv_sep, ''),
(token_status, last_error),
(token_status_sep, ''),
(Token, '\n'),
(token_cwd, cwd),
(token_cwd_sep if login=='root' else Token.IPython.Prompt.Cwd.Sep, ''),
(Token.IPython.Prompt.Vcs, str('') + git_branch if git_branch else ''),
(Token.IPython.Prompt.Vcs.Sep, ''),
(Token, '\n'),
(token_mode, vi_mode),
(token_mode_sep, ''),
]
@override
def out_prompt_tokens(self) -> List[Tuple[Any, Any]]:
return []

View file

@ -7,7 +7,8 @@ name = "pygments_nightfox_style"
version = "1.0.0" version = "1.0.0"
requires-python = ">=3.8" requires-python = ">=3.8"
dependencies = [ dependencies = [
"pygments" "pygments",
"typing_extensions"
] ]
license = {text = "MIT"} license = {text = "MIT"}
authors = [ authors = [