efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / __main__.py
1 """
2                                       '||
3 ... ...  .... ... ... ... ...  ....    ||
4  ||'  ||  '|.  |   ||  ||  |  '' .||   ||
5  ||    |   '|.|     ||| |||   .|' ||   ||
6  ||...'     '|       |   |    '|..'|' .||.
7  ||      .. |
8 ''''      ''
9 Created by Dylan Araps.
10 """
11
12 import argparse
13 import logging
14 import os
15 import shutil
16 import sys
17
18 from .settings import __version__, CACHE_DIR, CONF_DIR
19 from . import colors
20 from . import export
21 from . import image
22 from . import reload
23 from . import sequences
24 from . import theme
25 from . import util
26 from . import wallpaper
27
28
29 def get_args():
30     """Get the script arguments."""
31     description = "wal - Generate colorschemes on the fly"
32     arg = argparse.ArgumentParser(description=description)
33
34     arg.add_argument("-a", metavar="\"alpha\"",
35                      help="Set terminal background transparency. \
36                            *Only works in URxvt*")
37
38     arg.add_argument("-b", metavar="background",
39                      help="Custom background color to use.")
40
41     arg.add_argument("--backend", metavar="backend",
42                      help="Which color backend to use. \
43                            Use 'wal --backend' to list backends.",
44                      const="list_backends", type=str, nargs="?")
45
46     arg.add_argument("--theme", "-f", metavar="/path/to/file or theme_name",
47                      help="Which colorscheme file to use. \
48                            Use 'wal --theme' to list builtin themes.",
49                      const="list_themes", nargs="?")
50
51     arg.add_argument("--iterative", action="store_true",
52                      help="When pywal is given a directory as input and this "
53                           "flag is used: Go through the images in order "
54                           "instead of shuffled.")
55
56     arg.add_argument("--saturate", metavar="0.0-1.0",
57                      help="Set the color saturation.")
58
59     arg.add_argument("--preview", action="store_true",
60                      help="Print the current color palette.")
61
62     arg.add_argument("--vte", action="store_true",
63                      help="Fix text-artifacts printed in VTE terminals.")
64
65     arg.add_argument("-c", action="store_true",
66                      help="Delete all cached colorschemes.")
67
68     arg.add_argument("-i", metavar="\"/path/to/img.jpg\"",
69                      help="Which image or directory to use.")
70
71     arg.add_argument("-l", action="store_true",
72                      help="Generate a light colorscheme.")
73
74     arg.add_argument("-n", action="store_true",
75                      help="Skip setting the wallpaper.")
76
77     arg.add_argument("-o", metavar="\"script_name\"", action="append",
78                      help="External script to run after \"wal\".")
79
80     arg.add_argument("-q", action="store_true",
81                      help="Quiet mode, don\'t print anything.")
82
83     arg.add_argument("-r", action="store_true",
84                      help="'wal -r' is deprecated: Use \
85                            (cat ~/.cache/wal/sequences &) instead.")
86
87     arg.add_argument("-R", action="store_true",
88                      help="Restore previous colorscheme.")
89
90     arg.add_argument("-s", action="store_true",
91                      help="Skip changing colors in terminals.")
92
93     arg.add_argument("-t", action="store_true",
94                      help="Skip changing colors in tty.")
95
96     arg.add_argument("-v", action="store_true",
97                      help="Print \"wal\" version.")
98
99     arg.add_argument("-e", action="store_true",
100                      help="Skip reloading gtk/xrdb/i3/sway/polybar")
101
102     return arg
103
104
105 def parse_args_exit(parser):
106     """Process args that exit."""
107     args = parser.parse_args()
108
109     if len(sys.argv) <= 1:
110         parser.print_help()
111         sys.exit(1)
112
113     if args.v:
114         parser.exit(0, "wal %s\n" % __version__)
115
116     if args.preview:
117         print("Current colorscheme:", sep='')
118         colors.palette()
119         sys.exit(0)
120
121     if args.i and args.theme:
122         parser.error("Conflicting arguments -i and -f.")
123
124     if args.r:
125         reload.colors()
126         sys.exit(0)
127
128     if args.c:
129         scheme_dir = os.path.join(CACHE_DIR, "schemes")
130         shutil.rmtree(scheme_dir, ignore_errors=True)
131         sys.exit(0)
132
133     if not args.i and \
134        not args.theme and \
135        not args.R and \
136        not args.backend:
137         parser.error("No input specified.\n"
138                      "--backend, --theme, -i or -R are required.")
139
140     if args.theme == "list_themes":
141         theme.list_out()
142         sys.exit(0)
143
144     if args.backend == "list_backends":
145         print("\n - ".join(["\033[1;32mBackends\033[0m:",
146                             *colors.list_backends()]))
147         sys.exit(0)
148
149
150 def parse_args(parser):
151     """Process args."""
152     args = parser.parse_args()
153
154     if args.q:
155         logging.getLogger().disabled = True
156         sys.stdout = sys.stderr = open(os.devnull, "w")
157
158     if args.a:
159         util.Color.alpha_num = args.a
160
161     if args.i:
162         image_file = image.get(args.i, iterative=args.iterative)
163         colors_plain = colors.get(image_file, args.l, args.backend,
164                                   sat=args.saturate)
165
166     if args.theme:
167         colors_plain = theme.file(args.theme, args.l)
168
169     if args.R:
170         colors_plain = theme.file(os.path.join(CACHE_DIR, "colors.json"))
171
172     if args.b:
173         args.b = "#%s" % (args.b.strip("#"))
174         colors_plain["special"]["background"] = args.b
175         colors_plain["colors"]["color0"] = args.b
176
177     if not args.n:
178         wallpaper.change(colors_plain["wallpaper"])
179
180     sequences.send(colors_plain, to_send=not args.s, vte_fix=args.vte)
181
182     if sys.stdout.isatty():
183         colors.palette()
184
185     export.every(colors_plain)
186
187     if not args.e:
188         reload.env(tty_reload=not args.t)
189
190     if args.o:
191         for cmd in args.o:
192             util.disown([cmd])
193
194     if not args.e:
195         reload.gtk()
196
197
198 def main():
199     """Main script function."""
200     util.create_dir(os.path.join(CONF_DIR, "templates"))
201     util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/"))
202     util.create_dir(os.path.join(CONF_DIR, "colorschemes/dark/"))
203
204     util.setup_logging()
205     parser = get_args()
206
207     parse_args_exit(parser)
208     parse_args(parser)
209
210
211 if __name__ == "__main__":
212     main()