Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Usage: gen-compose [OPTIONS] [MAPPINGS]...
Options:
--key TEXT key to use as compose key [default: §]
-r, --raw just keymap without prefix
-e, --escape-modifier-keys escape keys such as ^, so they're treated as the character and not a modifier
--help Show this message and exit.
```

Expand Down
11 changes: 8 additions & 3 deletions gencompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ def object_hook(dict_, func: Callable, types: Union[Tuple[Type], Type] = dict):
@click.argument('mappings', type=click.File(), nargs=-1)
@click.option('--key', default='§', show_default=True, help='key to use as compose key')
@click.option('-r', '--raw', is_flag=True, help='just keymap without prefix')
def main(mappings, raw, key):
@click.option('-e', '--escape-modifier-keys', is_flag=True, help='escape modifier keys such as "^"')
def main(mappings, raw, key, escape_modifier_keys):
"""Generate macos rebind file from compose json mapping"""
all_maps = {}
for mapping in mappings:
yamldata = yaml.load(mapping.read(), Loader=yaml.Loader)
all_maps.update(**{str(k): str(v) for k, v in yamldata.items()})
all_maps = read_paths(all_maps)
text = data_to_mac_dict(all_maps)
text = data_to_mac_dict(all_maps, escape_modifier_keys)
if raw:
echo(text)
else:
Expand Down Expand Up @@ -114,7 +115,7 @@ def read_paths(data):
return parsed


def data_to_mac_dict(data):
def data_to_mac_dict(data, escape_modifer_keys=False):
"""
converts dictionary data to macos keymap.dict format

Expand All @@ -127,6 +128,10 @@ def data_to_mac_dict(data):
};
};
"""
if escape_modifer_keys:
escape_special_chars = lambda c: f'\\{c}' if c in '^@~#$' else c
escape_keys = lambda d: {escape_special_chars(k): (escape_keys(v) if type(v) is dict else v) for k, v in d.items()}
data = escape_keys(data)
updated = object_hook(data, lambda value: f'INSERT:{value}', str)
text = json.dumps(updated, indent=2, ensure_ascii=False)
repl = lambda value: f'("insertText:", "{value.groups()[0]}");'
Expand Down
14 changes: 14 additions & 0 deletions tests/test_gencompose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path

from click.testing import CliRunner
from gencompose import main


def test_special_characters():
runner = CliRunner()
file = Path(__file__).parent / 'test_special_chars.yaml'
result = runner.invoke(main, args=['--escape-modifier-keys', str(file)])
print(result.output)
assert result.exit_code == 0
assert '"\\\\^"' in result.output
assert '"2"' in result.output
5 changes: 5 additions & 0 deletions tests/test_special_chars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
^2: ²
$$: ﹩
~~: ~
"@@": @
"#x": ⌧