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

41 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2025-04-10 19:59 -0700

1from typing import Any 

2 

3from colors import color 

4 

5from .status import get_status 

6 

7 

8def __get_branch_prompt( 

9 branch: str | None, 

10 *, 

11 has_remote: bool, 

12 ahead: int, 

13 behind: int, 

14) -> str: 

15 if branch is None: 

16 return color("(detached)", fg="red") 

17 

18 prompt: str = color(branch, fg=( 

19 "green" if has_remote else "cyan" 

20 )) 

21 

22 if has_remote: 

23 remote_flags: str = "" 

24 

25 if ahead > 0: 

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

27 if behind > 0: 

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

29 

30 if remote_flags: 

31 prompt += remote_flags 

32 

33 return prompt 

34 

35 

36def __get_local_flags( 

37 *, 

38 staged: bool, 

39 unstaged: bool, 

40 untracked: bool, 

41) -> str: 

42 local_flags: str = "" 

43 

44 if staged: 

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

46 if unstaged: 

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

48 if untracked: 

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

50 

51 if local_flags: 

52 return ':' + local_flags 

53 

54 return "" 

55 

56 

57def get_prompt() -> str: 

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

59 if status is None: 

60 return "" 

61 

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

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

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

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

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

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

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

69 

70 return __get_branch_prompt( 

71 branch, 

72 has_remote=has_remote, 

73 ahead=ahead, 

74 behind=behind, 

75 ) + __get_local_flags( 

76 staged=staged, 

77 unstaged=unstaged, 

78 untracked=untracked, 

79 ) 

80 

81 

82def print_prompt() -> None: 

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