diff options
Diffstat (limited to 'dotfiles/dotfile.py')
-rw-r--r-- | dotfiles/dotfile.py | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/dotfiles/dotfile.py b/dotfiles/dotfile.py index 0b15dbd..1a86f63 100644 --- a/dotfiles/dotfile.py +++ b/dotfiles/dotfile.py @@ -23,22 +23,25 @@ class Dotfile(object): called, and not in the layers above. """ + states = { + 'error': {'text': '(error)', 'color': 'red'}, + 'missing': {'text': '(missing)', 'color': 'yellow'}, + 'conflict': {'text': '(conflict)', 'color': 'yellow'}, + 'ok': {'text': '(ok)', 'color': 'green'}, + } + def __init__(self, name, target): self.name = name self.target = target - self.state = '(unknown)' + self._set_state() def __str__(self): - short_name, short_target = self._truncate_paths() - return '%s -> %s %s' % (short_name, short_target, self.state) + short_name, _ = self._truncate_paths() + return '%-18s %-s' % (short_name, self.state['text']) def __repr__(self): return '<Dotfile %r>' % self.name - def _truncate_paths(self): - discard = len(str(self.name.common(self.target))) + 1 - return (str(self.name)[discard:], str(self.target)[discard:]) - def add(self): if self.target.check(exists=1): raise OSError(errno.EEXIST, self.target) @@ -54,5 +57,27 @@ class Dotfile(object): def sync(self): self.name.mksymlinkto(self.target) - def invalid(self): - return self.state != '(ok)' + def is_ok(self): + return self.state == self.states['ok'] + + def _set_state(self): + + # only for testing, cli should never reach this state + if self.target.check(exists=0): + self.state = self.states['error'] + + # no $HOME symlink + elif self.name.check(exists=0): + self.state = self.states['missing'] + + # if name exists but isn't a link to the target + elif self.name.check(link=0) or not self.name.samefile(self.target): + self.state = self.states['conflict'] + + # all good + else: + self.state = self.states['ok'] + + def _truncate_paths(self): + discard = len(str(self.name.common(self.target))) + 1 + return (str(self.name)[discard:], str(self.target)[discard:]) |