Skip to content

Commit

Permalink
remove spaceship info item since all symbols are customizable (#257)
Browse files Browse the repository at this point in the history
* remove spaceship info item since all symbols are customizable

* fix unit tests
  • Loading branch information
nosarthur authored Jul 17, 2023
1 parent 091cd91 commit 25a2ea0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 53 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,16 @@ branch,commit_msg,commit_time
```

Here `branch` includes both branch name and status.

Another choice is to enable `spaceship_status`, which mimics
the symbols used in [spaceship-prompt](https://spaceship-prompt.sh/sections/git/#Git-status-git_status).
The status symbols are similar to the ones used in [spaceship-prompt](https://spaceship-prompt.sh/sections/git/#Git-status-git_status).

To customize these symbols, add a file in `$XDG_CONFIG_HOME/gita/symbols.csv`.
The default `spaceship_status` settings corresponds to
The default settings corresponds to

```csv
dirty,staged,untracked,local_ahead,remote_ahead,diverged,in_sync,no_remote
!,+,?,↑,↓,⇕,,∅
*,+,?,↑,↓,⇕,,∅
```
Only the symbols to be overridden need to be defined.

You can search unicode symbols [here](https://www.compart.com/en/unicode/).

### customize git command flags
Expand Down
61 changes: 22 additions & 39 deletions gita/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_info_funcs(no_colors=False) -> List[Callable[[str], str]]:
all_info_items = {
"branch": partial(get_repo_status, no_colors=no_colors),
"branch_name": get_repo_branch,
"spaceship_status": get_spaceship_status,
"commit_msg": get_commit_msg,
"commit_time": get_commit_time,
"path": get_path,
Expand Down Expand Up @@ -196,64 +195,52 @@ def get_commit_time(prop: Dict[str, str]) -> str:
return f"({result.stdout.strip()})"


# better solution exists for python3.7+
# https://stackoverflow.com/questions/11351032/named-tuple-and-default-values-for-optional-keyword-arguments
Symbols = namedtuple(
"Symbols",
"dirty staged untracked local_ahead remote_ahead diverged in_sync no_remote",
)
Symbols.__new__.__defaults__ = ("",) * len(Symbols._fields)
default_symbols = Symbols("*", "+", "?")
default_symbols = {
"dirty": "*",
"staged": "+",
"untracked": "?",
"local_ahead": "↑",
"remote_ahead": "↓",
"diverged": "⇕",
"in_sync": "",
"no_remote": "∅",
"": "",
}


@lru_cache()
def get_symbols(baseline: Symbols = default_symbols) -> Dict[str, str]:
def get_symbols() -> Dict[str, str]:
"""
return status symbols with customization
"""
symbols = baseline._asdict()
symbols[""] = ""
custom = {}
csv_config = Path(common.get_config_fname("symbols.csv"))
if csv_config.is_file():
with open(csv_config, "r") as f:
reader = csv.DictReader(f)
custom = next(reader)
symbols.update(custom)
return symbols
default_symbols.update(custom)
return default_symbols


def get_repo_status(prop: Dict[str, str], no_colors=False) -> str:
head = get_head(prop["path"])
colors = {situ: Color[name].value for situ, name in get_color_encoding().items()}
dirty, staged, untracked, situ = _get_repo_status(prop, skip_situ=no_colors)
symbols = get_symbols(default_symbols)
info = f"{head:<10} [{symbols[dirty]+symbols[staged]+symbols[untracked]}]"
branch = get_head(prop["path"])
dirty, staged, untracked, situ = _get_repo_status(prop)
symbols = get_symbols()
info = f"{branch:<10} [{symbols[dirty]+symbols[staged]+symbols[untracked]+symbols[situ]}]"

if no_colors:
return f"{info:<17}"
return f"{info:<18}"
colors = {situ: Color[name].value for situ, name in get_color_encoding().items()}
color = colors[situ]
return f"{color}{info:<17}{Color.end}"


spaceship = Symbols("!", "+", "?", "↑", "↓", "⇕", "", "∅")


def get_spaceship_status(prop: Dict[str, str]) -> str:
head = get_head(prop["path"])
dirty, staged, untracked, situ = _get_repo_status(prop)
symbols = get_symbols(spaceship)
info = f"{head:<10} [{symbols[dirty]+symbols[staged]+symbols[untracked]+symbols[situ]}]"
return f"{info:<18}"
return f"{color}{info:<18}{Color.end}"


def get_repo_branch(prop: Dict[str, str]) -> str:
return get_head(prop["path"])


def _get_repo_status(
prop: Dict[str, str], skip_situ=False
) -> Tuple[str, str, str, str]:
def _get_repo_status(prop: Dict[str, str]) -> Tuple[str, str, str, str]:
"""
Return the status of one repo
"""
Expand All @@ -263,9 +250,6 @@ def _get_repo_status(
staged = "staged" if run_quiet_diff(flags, ["--cached"], path) else ""
untracked = "untracked" if has_untracked(flags, path) else ""

if skip_situ:
return dirty, staged, untracked, ""

diff_returncode = run_quiet_diff(flags, ["@{u}", "@{0}"], path)
if diff_returncode == 128:
situ = "no_remote"
Expand All @@ -285,7 +269,6 @@ def _get_repo_status(
ALL_INFO_ITEMS = {
"branch",
"branch_name",
"spaceship_status",
"commit_msg",
"commit_time",
"path",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name="gita",
packages=["gita"],
version="0.16.5",
version="0.16.6",
license="MIT",
description="Manage multiple git repos with sanity",
long_description=long_description,
Expand Down
7 changes: 3 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ def test_ls(self, monkeypatch, capfd):
[
(
PATH_FNAME,
"repo1 \x1b[31mmaster [*+?] \x1b[0m msg \nrepo2 \x1b[31mmaster [*+?] \x1b[0m msg \nxxx \x1b[31mmaster [*+?] \x1b[0m msg \n",
"repo1 \x1b[31mmaster [*+?] \x1b[0m msg \nrepo2 \x1b[31mmaster [*+?] \x1b[0m msg \nxxx \x1b[31mmaster [*+?] \x1b[0m msg \n",
),
(PATH_FNAME_EMPTY, ""),
(
PATH_FNAME_CLASH,
"repo1 \x1b[31mmaster [*+?] \x1b[0m msg \nrepo2 \x1b[31mmaster [*+?] \x1b[0m msg \n",
"repo1 \x1b[31mmaster [*+?] \x1b[0m msg \nrepo2 \x1b[31mmaster [*+?] \x1b[0m msg \n",
),
],
)
Expand Down Expand Up @@ -535,8 +535,7 @@ def test_ll(self, _, capfd):
__main__.f_info(args)
out, err = capfd.readouterr()
assert (
"In use: branch,commit_msg,commit_time\nUnused: branch_name,path,spaceship_status\n"
== out
"In use: branch,commit_msg,commit_time\nUnused: branch_name,path\n" == out
)
assert err == ""

Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ def test_auto_group(repos, paths, expected):
(
[{"abc": {"path": "/root/repo/", "type": "", "flags": []}}, False],
True,
"abc \x1b[31mrepo [*+?] \x1b[0m msg xx",
"abc \x1b[31mrepo [*+?] \x1b[0m msg xx",
),
(
[{"abc": {"path": "/root/repo/", "type": "", "flags": []}}, True],
True,
"abc repo [*+?] msg xx",
"abc repo [*+?] msg xx",
),
(
[{"repo": {"path": "/root/repo2/", "type": "", "flags": []}}, False],
False,
"repo \x1b[32mrepo [?] \x1b[0m msg xx",
"repo \x1b[32mrepo [?] \x1b[0m msg xx",
),
],
)
Expand Down

0 comments on commit 25a2ea0

Please sign in to comment.