[modules/git] Nicer formatting of stati

Add separate widgets for each state, so that each state can have a
custom icon *and* a custom coloring.
This commit is contained in:
Tobias Witek 2019-01-23 19:56:16 +01:00
parent 1698e4ad4c
commit 87f185e62b
11 changed files with 75 additions and 31 deletions

View file

@ -23,50 +23,53 @@ import bumblebee.util
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.gitinfo)
)
widgets = []
super(Module, self).__init__(engine, config, widgets)
self._engine = engine
self._fmt = self.parameter("format", "{branch} {flags}")
self._error = False
self.update(self.widgets())
def hidden(self):
return self._error
def gitinfo(self, widget):
info = ""
directory = None
data = {
"branch": "n/a",
"directory": "n/a",
"flags": {},
}
def update(self, widgets):
state = {}
new_widgets = []
try:
directory = bumblebee.util.execute("xcwd").strip()
directory = self._get_git_root(directory)
repo = pygit2.Repository(directory)
new_widgets.append(bumblebee.output.Widget(name='git.main', full_text=repo.head.shorthand))
for filepath, flags in repo.status().items():
if flags == pygit2.GIT_STATUS_WT_NEW or \
flags == pygit2.GIT_STATUS_INDEX_NEW:
data["flags"]["new"] = True
state['new'] = True
if flags == pygit2.GIT_STATUS_WT_DELETED or \
flags == pygit2.GIT_STATUS_INDEX_DELETED:
data["flags"]["deleted"] = True
state['deleted'] = True
if flags == pygit2.GIT_STATUS_WT_MODIFIED or \
flags == pygit2.GIT_STATUS_INDEX_MODIFIED:
data["flags"]["modified"] = True
data["branch"] = repo.head.shorthand
data["directory"] = directory
data["flags"] = " ".join([self._engine._theme.symbol(widget, name, name[0]) for name in data["flags"].keys()])
state['modified'] = True
self._error = False
if 'new' in state:
new_widgets.append(bumblebee.output.Widget(name='git.new'))
if 'modified' in state:
new_widgets.append(bumblebee.output.Widget(name='git.modified'))
if 'deleted' in state:
new_widgets.append(bumblebee.output.Widget(name='git.deleted'))
while len(widgets) > 0:
del widgets[0]
for widget in new_widgets:
widgets.append(widget)
except Exception as e:
self._error = True
logging.error(e)
return "n/a"
return string.Formatter().vformat(self._fmt, (), data)
def state(self, widget):
return widget.name.split('.')[1]
def _get_git_root(self, directory):
while len(directory) > 1:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -49,5 +49,10 @@
"fg": "#1d2021",
"bg": "#b8bb26"
}
}
},
"git": {
"modified": { "bg": "#458588" },
"deleted": { "bg": "#9d0006" },
"new": { "bg": "#b16286" }
}
}

View file

@ -49,5 +49,10 @@
"fg": "#1d2021",
"bg": "#b8bb26"
}
},
"git": {
"modified": { "bg": "#458588" },
"deleted": { "bg": "#9d0006" },
"new": { "bg": "#b16286" }
}
}

View file

@ -49,5 +49,10 @@
"fg": "#1d2021",
"bg": "#b8bb26"
}
},
"git": {
"modified": { "bg": "#458588" },
"deleted": { "bg": "#9d0006" },
"new": { "bg": "#b16286" }
}
}

View file

@ -49,5 +49,10 @@
"fg": "#1d2021",
"bg": "#b8bb26"
}
}
},
"git": {
"modified": { "bg": "#458588" },
"deleted": { "bg": "#9d0006" },
"new": { "bg": "#b16286" }
}
}

View file

@ -121,5 +121,11 @@
"ONLINE": {"prefix": "pool"},
"FAULTED": {"prefix": "pool (!)"},
"DEGRADED": {"prefix": "pool (!)"}
},
"git": {
"main": { "prefix": "" },
"new": { "prefix": "[n]" },
"modified": { "prefix": "[m]" },
"deleted": { "prefix": "[d]" }
}
}

View file

@ -186,9 +186,9 @@
}
},
"git": {
"prefix": "",
"new": "",
"modified": "",
"deleted": ""
"main": { "prefix": "" },
"new": { "prefix": "" },
"modified": { "prefix": "" },
"deleted": { "prefix": "" }
}
}

View file

@ -40,5 +40,10 @@
"fg": "#002b36",
"bg": "#859900"
}
}
},
"git": {
"modified": { "bg": "#2aa198" },
"deleted": { "bg": "#d33682" },
"new": { "bg": "#859900" }
}
}

View file

@ -42,5 +42,10 @@
"fg": "#002b36",
"bg": "#859900"
}
}
},
"git": {
"modified": { "bg": "#2aa198" },
"deleted": { "bg": "#d33682" },
"new": { "bg": "#859900" }
}
}

View file

@ -44,5 +44,10 @@
"fg": "#002b36",
"bg": "#859900"
}
}
},
"git": {
"modified": { "bg": "#2aa198" },
"deleted": { "bg": "#d33682" },
"new": { "bg": "#859900" }
}
}