Skip to content

Commit

Permalink
Add rm subcommand, remove set subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Jan 29, 2021
1 parent 62fec96 commit b82f905
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 107 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ pip install --install-option="--themes=all" pycritty
Change your current config:

```bash
pycritty set --font UbuntuMono --size 14 --opacity 0.95
pycritty --font UbuntuMono --size 14 --opacity 0.95
```

Save multiple configs and reuse them later:

```bash
pycritty save MyConfig
# 3 bugs later ...
pycritty load MyConfig
pycritty save ThisConfig
pycritty load AnotherConfig
```

Install themes and configs from URLs:
```bash
pycritty install -t https://raw.githubusercontent.com/antoniosarosi/pycritty/master/config/themes/breeze.yaml
pycritty set -t breeze
pycritty -t breeze
pycritty install -c -n SomeCoolConfig https://raw.githubusercontent.com/antoniosarosi/dotfiles/master/.config/alacritty/config.yaml
pycritty load SomeCoolConfig
```
Expand All @@ -50,7 +49,7 @@ Check help for all available options:
```bash
pycritty -h
# pycritty subcomand -h
pycritty set -h
pycritty save -h
```

## Fonts Config
Expand All @@ -61,7 +60,7 @@ fonts:
Alias: Font Name
```

When applied using ```pycritty set -f Alias```, the previous format will be
When applied using ```pycritty -f Alias```, the previous format will be
converted into the alacritty equivalent:

```yaml
Expand Down Expand Up @@ -123,13 +122,13 @@ colors:
Then you can apply it using the name of the file:

```bash
pycritty set -t custom
pycritty -t custom
```

## Custom scripts

If you want to apply different configs programmatically, you can either use
the cli in a shell script or use ```pycritty``` as a python module:
the CLI in a shell script or use ```pycritty``` as a python module:

```python
# Dummy script that changes the theme every 10 minutes
Expand Down
3 changes: 1 addition & 2 deletions pycritty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Automated tools for managing alacritty configurations"""

__version__ = "0.3.1"
__version__ = "0.3.2"


class PycrittyError(Exception):
"""General error that might be thrown at any point"""
pass
4 changes: 2 additions & 2 deletions pycritty/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .pycritty import parser, subparsers
from .set import set_parser
from .list import list_parser
from .ls import list_parser
from .save import save_parser
from .load import load_parser
from .install import install_parser
from .rm import remove_parser
4 changes: 1 addition & 3 deletions pycritty/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

install_parser.add_argument(
'url',
metavar='URL',
help='URL where the config is located',
)

Expand Down Expand Up @@ -40,7 +39,6 @@

group.add_argument(
'-c', '--config',
dest='config',
action='store_true',
help='Install as config, you can load it with pycritty load',
help='Install as a config file in your saves directory (default)',
)
2 changes: 1 addition & 1 deletion pycritty/cli/list.py → pycritty/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


list_parser = subparsers.add_parser(
'list',
'ls',
help='List available resources',
formatter_class=formatter(),
argument_default=argparse.SUPPRESS,
Expand Down
48 changes: 45 additions & 3 deletions pycritty/cli/pycritty.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,55 @@ def formatter(indent_increment=2, max_help_position=40, width=None):
prog='pycritty',
description='Change your Alacritty config on the fly!',
argument_default=argparse.SUPPRESS,
formatter_class=formatter(max_help_position=17),
formatter_class=formatter(),
)

parser.add_argument(
'-v', '--version',
action='version',
version=__version__,
)

subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand',)
parser.add_argument(
'-t', '--theme',
dest='change_theme',
metavar='THEME',
help='Change theme, choose from ~/.config/alacritty/themes',
)
parser.add_argument(
'-f', '--font',
dest='change_font',
metavar='FONT',
help='Change font family, choose from ~/.config/alacritty/fonts.yaml',
)
parser.add_argument(
'-s', '--size',
type=float,
dest='change_font_size',
metavar='SIZE',
help='Change font size',
)
parser.add_argument(
'-o', '--opacity',
type=float,
dest='change_opacity',
metavar='OPACITY',
help='Change background opacity',
)
parser.add_argument(
'-p', '--padding',
metavar=('X', 'Y'),
type=int,
nargs=2,
dest='change_padding',
help='Change window padding X Y values',
)
parser.add_argument(
'-O', '--offset',
metavar=('X', 'Y'),
type=int,
nargs=2,
dest='change_font_offset',
help='Change offset, X is space between chars and Y is line height',
)

subparsers = parser.add_subparsers(title='subcommands', dest='subcommand',)
38 changes: 38 additions & 0 deletions pycritty/cli/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import argparse
from .pycritty import subparsers, formatter


remove_parser = subparsers.add_parser(
'rm',
help='Remove config files (themes or saves)',
formatter_class=formatter(),
argument_default=argparse.SUPPRESS,
)

remove_parser.add_argument(
'configs',
metavar='name',
nargs='+',
help='Themes or saved configs to be removed',
)

group = remove_parser.add_mutually_exclusive_group()

group.add_argument(
'-c', '--config',
action='store_true',
help='Remove saved config (default)',
)

group.add_argument(
'-t', '--theme',
dest='theme',
action='store_true',
help='Remove theme',
)

remove_parser.add_argument(
'-f', '--force',
action='store_true',
help="Don't prompt for confirmation",
)
53 changes: 0 additions & 53 deletions pycritty/cli/set.py

This file was deleted.

9 changes: 5 additions & 4 deletions pycritty/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from .set import SetConfig
from .list import ListResource
from .pycritty import SetConfig
from .ls import ListResource
from .save import SaveConfig
from .load import LoadConfig
from .install import Install
from .rm import Remove

subcommands = {
'set': SetConfig,
'list': ListResource,
'ls': ListResource,
'save': SaveConfig,
'load': LoadConfig,
'install': Install,
'rm': Remove,
}
10 changes: 7 additions & 3 deletions pycritty/commands/list.py → pycritty/commands/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ def list_fonts(self) -> List[str]:
def print_list(self, option: str):
header, color, get_list = self.options[option]
log.color_print(f'{header}:', default_color=log.Color.BOLD)
for item in get_list():
log.color_print(f' {item}', default_color=color)
ls = get_list()
if len(ls) < 1:
log.color_print(log.Color.ITALIC, log.Color.YELLOW, ' Empty directory')
else:
for item in get_list():
log.color_print(f' {item}', default_color=color)

def list_resource(self, to_be_listed: List[str]):
if 'all' in to_be_listed:
Expand All @@ -68,4 +72,4 @@ def list_resource(self, to_be_listed: List[str]):
if len(incorrenct_options) > 0:
raise PycrittyError(
f'Failed listing "{",".join(incorrenct_options)}" (unknown options)'
)
)
File renamed without changes.
27 changes: 27 additions & 0 deletions pycritty/commands/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Dict, Any, List
from .command import Command
from ..resources import saves_dir, themes_dir
from ..resources.resource import Resource, ConfigFile
from ..io import log


class Remove(Command):
def remove(self, configs: List[str], config_parent: Resource, force=False):
for conf in configs:
file = ConfigFile(config_parent.path, conf, ConfigFile.YAML)
if not file.exists():
log.warn(f'{conf} ->', log.Color.BOLD, file, log.Color.YELLOW, 'not found')
continue
confirmed = force
if not confirmed:
log.color_print(f'Removing {conf} ->', log.Color.BLUE, log.Color.BOLD, file)
confirmed = 'y' in input('Confirm (y/n): ').lower()
if confirmed:
file.path.unlink()

def execute(self, actions: Dict[str, Any]):
if 'configs' not in actions:
return
force = 'force' in actions
config_parent = themes_dir if 'theme' in actions else saves_dir
self.remove(actions['configs'], config_parent, force)
27 changes: 11 additions & 16 deletions pycritty/io/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,17 @@ def color_print(
if print_colors:
string.append(str(default_color))

messages_iter = iter(messages)
# Print first message and deal with 'sep' later
first = next(messages_iter)
is_color = isinstance(first, Color)
if is_color and print_colors or not is_color:
string.append(str(first))

# Print sep only when message is a string
for m in messages_iter:
is_color = isinstance(m, Color)
if is_color and print_colors:
string.append(str(m))
elif not is_color:
string.append(f'{sep}{m}')

# Back to normal
for i in range(len(messages) - 1):
# Print separators only after strings
is_color = isinstance(messages[i], Color)
if not is_color or print_colors:
string.append(str(messages[i]))
if not is_color:
string.append(sep)

# Last message being a color makes no sense
if not isinstance(messages[-1], Color):
string.append(str(messages[-1]))
if print_colors:
string.append(str(Color.NORMAL))

Expand Down
Loading

0 comments on commit b82f905

Please sign in to comment.