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 Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ coveralls = "*"
black = "*"
pylint = "*"
jedi = "*"
debugpy = "*"

[packages]
click = "*"
Expand Down
98 changes: 88 additions & 10 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion i3_resurrect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
__all__ = ["config", "layout", "main", "programs", "treeutils", "util"]
__all__ = ["config", "layout", "main", "programs", "treeutils", "types", "util"]

from . import config
from . import layout
from . import main
from . import programs
from . import treeutils
from . import types
from . import util
56 changes: 49 additions & 7 deletions i3_resurrect/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import json
from pathlib import Path
from typing import Any

from i3_resurrect.types import WindowCommandMapping, WindowSwallowMapping

from . import util


def create_default():
Expand All @@ -18,11 +23,25 @@ def create_default():
"directory": "~/.i3/i3-resurrect/",
"window_command_mappings": [
{
"class": "^Gnome-terminal$",
"filters": {
"class": "^Gnome-terminal$",
},
"command": "gnome-terminal",
},
],
"window_swallow_criteria": {},
"window_swallow_criteria": [
{
"filters": {
"class": "^PCSX2$",
"title": "^Kingdom Hearts [0-9]*FPS - PCSX2$",
},
"swallows": {
"class": "",
"instance": "",
"title": "^Kingdom Hearts [0-9]*FPS - PCSX2$",
},
},
],
"terminals": ["Gnome-terminal", "Alacritty"],
}

Expand All @@ -34,7 +53,7 @@ def create_default():
f.write(json.dumps(_config, indent=2))


def get(key, default):
def get(key: str, default: Any) -> Any:
"""
Gets a config value.
"""
Expand All @@ -45,19 +64,42 @@ def get(key, default):
try:
_config = json.loads(_config_file.read_text())
except json.decoder.JSONDecodeError as e:
print(f'Error in config file: "{str(e)}"')
util.eprint(f'Error in config file: "{str(e)}"')
exit(1)
except PermissionError as e:
print(f"Could not read config file: {str(e)}")
util.eprint(f"Could not read config file: {str(e)}")
exit(1)
except FileNotFoundError:
# Create default config if no config exists.
create_default()
except Exception as e:
util.eprint(f"Unknown error")

if _config is not None:
return _config.get(key, default)

return None


def get_window_command_mappings() -> list[WindowCommandMapping]:
return [
WindowCommandMapping(
command_mapping["filters"], command_mapping.get("command", None)
)
for command_mapping in get("window_command_mappings", [])
]


return _config.get(key, default)
def get_window_swallow_mappings() -> list[WindowSwallowMapping]:
return [
WindowSwallowMapping(
swallow_mapping.get("filters", {}), swallow_mapping.get("swallows", [])
)
for swallow_mapping in get("window_swallow_criteria", [])
]


_config = None
_config: dict | None = None

_config_dir = Path("~/.config/i3-resurrect/").expanduser()
_config_file = _config_dir / "config.json"
Expand Down
2 changes: 1 addition & 1 deletion i3_resurrect/layout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
from pathlib import Path
import shlex
import subprocess
import sys
import tempfile
from pathlib import Path

import i3ipc

Expand Down
2 changes: 1 addition & 1 deletion i3_resurrect/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from pathlib import Path
import sys

import click
import i3ipc
Expand Down
Loading