Coverage for src/extratools_gittools/prompt.py: 0%

38 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2025-03-27 18:10 -0700

1from typing import Any 

2 

3from colors import color 

4 

5from .status import get_status 

6 

7 

8def get_prompt() -> str: 

9 status: dict[str, Any] | None = get_status() 

10 if status is None: 

11 return "" 

12 

13 branch: str | None = status["branch"]["head"] 

14 has_remote: bool = status["branch"]["upstream"] is not None 

15 ahead: int = status["commits"]["ahead"] 

16 behind: int = status["commits"]["behind"] 

17 staged = bool(status["files"]["staged"]) 

18 unstaged = bool(status["files"]["unstaged"]) 

19 untracked = bool(status["files"]["untracked"]) 

20 

21 prompt: str = "" 

22 

23 if branch is None: 

24 prompt += color("(detached)", fg="red") 

25 else: 

26 prompt += color(branch, fg=( 

27 "green" if has_remote else "cyan" 

28 )) 

29 

30 if has_remote: 

31 remote_flags: str = "" 

32 

33 if ahead > 0: 

34 remote_flags += color('↑', fg="blue", style="bold") 

35 if behind > 0: 

36 remote_flags += color('↓', fg="yellow", style="bold") 

37 

38 if remote_flags: 

39 prompt += remote_flags 

40 

41 local_flags: str = "" 

42 

43 if staged: 

44 local_flags += color('+', fg="green", style="bold") 

45 if unstaged: 

46 local_flags += color('*', fg="red", style="bold") 

47 if untracked: 

48 local_flags += color('?', fg="cyan", style="bold") 

49 

50 if local_flags: 

51 prompt += ':' + local_flags 

52 

53 return prompt 

54 

55 

56def print_prompt() -> None: 

57 print(get_prompt(), end="")