summaryrefslogtreecommitdiff
path: root/pywal/theme.py
blob: 010e90f79099b69853e64251b62dad4133f1654e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Theme file handling.
"""
import os
import random
import sys

from .settings import CONF_DIR, MODULE_DIR
from . import util


def index():
    """List all installed theme files."""
    return [*os.scandir(os.path.join(CONF_DIR, "colorschemes")),
            *os.scandir(os.path.join(MODULE_DIR, "colorschemes"))]


def terminal_sexy_to_wal(data):
    """Convert terminal.sexy json schema to wal."""
    data["colors"] = {}
    data["special"] = {
        "foreground": data["foreground"],
        "background": data["background"],
        "cursor": data["color"][9]
    }

    for i, color in enumerate(data["color"]):
        data["colors"]["color%s" % i] = color

    return data


def file(input_file):
    """Import colorscheme from json file."""
    theme_name = ".".join((input_file, "json"))
    user_theme_file = os.path.join(CONF_DIR, "colorschemes", theme_name)
    theme_file = os.path.join(MODULE_DIR, "colorschemes", theme_name)

    # Find the theme file.
    if os.path.isfile(input_file):
        theme_file = input_file

    elif os.path.isfile(user_theme_file):
        theme_file = user_theme_file

    elif os.path.isfile(theme_file):
        theme_file = theme_file

    elif input_file == "random":
        themes = [theme.path for theme in index()]
        random.shuffle(themes)
        theme_file = themes[0]

    # Parse the theme file.
    if os.path.isfile(theme_file):
        data = util.read_file_json(theme_file)

        if "wallpaper" not in data:
            data["wallpaper"] = "None"

        if "alpha" not in data:
            data["alpha"] = "100"

        # Terminal.sexy format.
        if "color" in data:
            data = terminal_sexy_to_wal(data)

        return data

    else:
        print("No colorscheme file found, exiting...")
        sys.exit(1)