Skip to content

Commit

Permalink
implement pygitstatus, add names
Browse files Browse the repository at this point in the history
  • Loading branch information
Spitfire1900 committed Mar 28, 2024
1 parent 792d7d3 commit e5d493a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'gitstatus.lines_added': <Prompt: gitstatus.lines_added>, # DONE
'gitstatus.lines_removed': <Prompt: gitstatus.lines_removed>, # DONE
'gitstatus.clean': <Prompt: gitstatus.clean>, # DONE
'gitstatus': <Prompt: gitstatus>, # TODO
'gitstatus': <Prompt: gitstatus>, # DONE
```

- currbranch <!--
Expand Down
6 changes: 4 additions & 2 deletions xontrib/pygitstatus/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from xonsh.built_ins import XonshSession

from .prompts import (ahead, behind, branch, changed, clean, conflicts, curr_branch,
deleted, lines_added, lines_deleted, numstat, repo_path,
short_head, staged, stash_count, tag, tag_or_hash, untracked)
deleted, gitstatus, lines_added, lines_deleted, numstat,
repo_path, short_head, staged, stash_count, tag, tag_or_hash,
untracked)


def _load_xontrib_(xsh: XonshSession, **_) -> dict:
Expand All @@ -27,6 +28,7 @@ def _load_xontrib_(xsh: XonshSession, **_) -> dict:

prompt_fields: MutableMapping
prompt_fields = xsh.env.get('PROMPT_FIELDS') # type: ignore
prompt_fields['pygitstatus'] = gitstatus
prompt_fields['pygitstatus_curr_branch'] = curr_branch
prompt_fields['pygitstatus.ahead'] = ahead
prompt_fields['pygitstatus.behind'] = behind
Expand Down
76 changes: 59 additions & 17 deletions xontrib/pygitstatus/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
### .venv/Lib/site-packages/xonsh/prompt/gitstatus.py


@PromptField.wrap(prefix='↑·', info='ahead')
@PromptField.wrap(prefix='↑·', info='ahead', name='pygitstatus.ahead')
def ahead(fld: PromptField, ctx: PromptFields):
fld.value = ''
_ahead, _behind = (0, 0)
Expand All @@ -29,7 +29,7 @@ def ahead(fld: PromptField, ctx: PromptFields):
fld.value = str(_ahead) if _ahead else ''


@PromptField.wrap(prefix='↓·', info='behind')
@PromptField.wrap(prefix='↓·', info='behind', name='pygitstatus.behind')
def behind(fld: PromptField, ctx: PromptFields):
fld.value = ''
_ahead, _behind = (0, 0)
Expand All @@ -45,15 +45,16 @@ def behind(fld: PromptField, ctx: PromptFields):
fld.value = str(_behind) if _behind else ''


@PromptField.wrap(prefix='{CYAN}', info='branch')
@PromptField.wrap(prefix='{CYAN}', info='branch', name='pygitstatus.branch')
def branch(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
repo = Repo('.')
fld.value = repo.head.shorthand


@PromptField.wrap(prefix="{BLUE}+", suffix="{RESET}", info="changed")
@PromptField.wrap(prefix="{BLUE}+", suffix="{RESET}", info="changed",
name='pygitstatus.changed')
def changed(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -64,7 +65,8 @@ def changed(fld: PromptField, ctx: PromptFields):
fld.value = str(untracked_count)


@PromptField.wrap(prefix="{RED}×", suffix="{RESET}", info="conflicts")
@PromptField.wrap(prefix="{RED}×", suffix="{RESET}", info="conflicts",
name='pygitstatus.conflicts')
def conflicts(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -75,7 +77,8 @@ def conflicts(fld: PromptField, ctx: PromptFields):
fld.value = str(conflicted_count)


@PromptField.wrap(prefix='{BOLD_GREEN}', suffix='{RESET}', symbol='✓')
@PromptField.wrap(prefix='{BOLD_GREEN}', suffix='{RESET}', symbol='✓',
name='pygitstatus.clean')
def clean(fld: PromptField, ctx: PromptFields):

# symbol attribute is auto-populated by wrap function
Expand All @@ -96,7 +99,8 @@ def curr_branch() -> Optional[str]:
return repo.head.shorthand


@PromptField.wrap(prefix="{RED}-", suffix="{RESET}", info="deleted")
@PromptField.wrap(prefix="{RED}-", suffix="{RESET}", info="deleted",
name='pygitstatus.deleted')
def deleted(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -107,7 +111,7 @@ def deleted(fld: PromptField, ctx: PromptFields):
fld.value = str(untracked_count)


@PromptField.wrap(prefix="{CYAN}+", suffix="{RESET}")
@PromptField.wrap(prefix="{CYAN}+", suffix="{RESET}", name='pygitstatus.lines_added')
def lines_added(fld: PromptField, ctx: PromptFields):
fld.value = ''

Expand All @@ -118,7 +122,8 @@ def lines_added(fld: PromptField, ctx: PromptFields):
fld.value = str(inserts)


@PromptField.wrap(prefix="{INTENSE_RED}-", suffix="{RESET}")
@PromptField.wrap(prefix="{INTENSE_RED}-", suffix="{RESET}",
name='pygitstatus.lines_deleted')
def lines_deleted(fld: PromptField, ctx: PromptFields):
fld.value = ''

Expand All @@ -129,7 +134,7 @@ def lines_deleted(fld: PromptField, ctx: PromptFields):
fld.value = str(deletes)


@PromptField.wrap()
@PromptField.wrap(name='pygitstatus.numstat')
def numstat(fld: PromptField, ctx: PromptFields):
fld.value = str((0, 0))
insert = 0
Expand All @@ -144,7 +149,7 @@ def numstat(fld: PromptField, ctx: PromptFields):
fld.value = str((insert, delete))


@PromptField.wrap()
@PromptField.wrap(name='pygitstatus.repo_path')
def repo_path(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -155,7 +160,7 @@ def repo_path(fld: PromptField, ctx: PromptFields):
fld.value = os.path.relpath(repo.path)


@PromptField.wrap(prefix=':')
@PromptField.wrap(prefix=':', name='pygitstatus.short_head')
def short_head(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -165,7 +170,8 @@ def short_head(fld: PromptField, ctx: PromptFields):
fld.value = local_commit.short_id


@PromptField.wrap(prefix="{RED}●", suffix="{RESET}", info="staged")
@PromptField.wrap(prefix="{RED}●", suffix="{RESET}", info="staged",
name='pygitstatus.staged')
def staged(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -182,7 +188,7 @@ def staged(fld: PromptField, ctx: PromptFields):
fld.value = str(untracked_count)


@PromptField.wrap(prefix="⚑")
@PromptField.wrap(prefix="⚑", name='pygitstatus.stash_count')
def stash_count(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -192,15 +198,15 @@ def stash_count(fld: PromptField, ctx: PromptFields):
fld.value = str(_stash_count)


@PromptField.wrap()
@PromptField.wrap(name='pygitstatus.tag')
def tag(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
repo = Repo('.')
fld.value = repo.describe()


@PromptField.wrap()
@PromptField.wrap(name='pygitstatus.tag_or_hash')
def tag_or_hash(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -213,7 +219,7 @@ def tag_or_hash(fld: PromptField, ctx: PromptFields):
fld.value = repo.lookup_reference(repo.head.name).peel(Commit).short_id


@PromptField.wrap(prefix="…", info="untracked")
@PromptField.wrap(prefix="…", info="untracked", name='pygitstatus.untracked')
def untracked(fld: PromptField, ctx: PromptFields):
fld.value = ''
with contextlib.suppress(GitError):
Expand All @@ -222,3 +228,39 @@ def untracked(fld: PromptField, ctx: PromptFields):
[v for k, v in repo.status().items() if v == GIT_STATUS_WT_NEW])
if untracked_count > 0:
fld.value = str(untracked_count)


class GitStatus(MultiPromptField):
"""Return str `BRANCH|OPERATOR|numbers`"""

_name = 'pygitstatus'
fragments = (
".branch",
".ahead",
".behind",
"gitstatus.operations", # does not use subprocess calls
"{RESET}|",
".staged",
".conflicts",
".changed",
".deleted",
".untracked",
".stash_count",
".lines_added",
".lines_removed",
".clean",
)
hidden = (
".lines_added",
".lines_removed",
)
"""These fields will not be processed for the result"""

def get_frags(self, env):
for frag in self.fragments:
if frag in self.hidden:
continue
yield frag


gitstatus = GitStatus()

0 comments on commit e5d493a

Please sign in to comment.