diff --git a/src/config.py b/src/config.py index b59f79d..bd2366f 100755 --- a/src/config.py +++ b/src/config.py @@ -134,8 +134,13 @@ def write_theme(tmp_config,theme): f.close() -def apply(config_file,theme): +def apply(config_file,theme,dry=False): tmp=extract(config_file) write_theme(tmp,theme) - shutil.move(tmp,config_file) + f=open(tmp,mode="r") + new_config=f.read() + f.close() + if not(dry): + shutil.move(tmp,config_file) + return(new_config) diff --git a/src/i3-colors.py b/src/i3-colors.py index 9450c9c..ceb96e0 100755 --- a/src/i3-colors.py +++ b/src/i3-colors.py @@ -1,59 +1,63 @@ #!/usr/bin/python import config, theme, os, argparse, subprocess,sys -##### Utils Functions ##### -def log(msg,title=""): - if len(title)>0: - print("\033[92m{}\033[00m: {}" .format(title,msg)) - else: - print(msg) -########################### - ##### Apply Theme ##### def apply(args): + if not(args.theme_path): + argsMainParser.print_help() + exit(1) loaded_theme=theme.load(args.theme_path) config_file=os.environ["HOME"]+"/.config/i3/config" - if args.config_path: - config_file=args.config_path - config.apply(config_file,loaded_theme) - for meta_key,meta_value in loaded_theme["meta"].items(): - log(meta_value,title=meta_key.title()) - if args.restart: - subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if not(args.dry): + config.apply(config_file,loaded_theme) + for meta_key,meta_value in loaded_theme["meta"].items(): + print("\033[92m{}\033[00m: {}" .format(meta_key.title(),meta_value)) + if args.restart: + subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE) + else: + new_config=config.apply(config_file,loaded_theme,dry=True) + print(new_config) ####################### + +##### Aleatory ##### def aleatory(args): t=theme.random_theme().as_dict() config.apply(os.environ["HOME"]+"/.config/i3/config",t) if args.restart: - subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE) + subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE) +#################### + ##### Extract Theme ##### def extract(args): + if not(args.config_path): + argsMainParser.print_help() + exit(1) t=config.extract_theme(args.config_path) print(t.as_yaml()) ####################### -##### Parse Arguments ##### -argsMainParser = argparse.ArgumentParser(description='I3 Window Manager Colors Themer.') -argsSubParsers = argsMainParser.add_subparsers() -argsApplyParser = argsSubParsers.add_parser("apply") -argsApplyParser.add_argument('theme_path', type=str, nargs='?', - help='I3 YAML theme path.') -argsApplyParser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.') -argsApplyParser.add_argument('config_path', type=str, nargs='?', - help='I3 configuration file.') -argsApplyParser.set_defaults(func=apply) - -argsExtractParser = argsSubParsers.add_parser("extract") -argsExtractParser.add_argument('config_path', type=str, nargs='?', - help='Extract theme from config file.') -argsExtractParser.set_defaults(func=extract) - -argsAleatoryParser = argsSubParsers.add_parser("aleatory") -argsAleatoryParser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.') -argsAleatoryParser.set_defaults(func=aleatory) - - +##### Entry Point ##### if __name__ == "__main__": + ##### Main Parser + argsMainParser = argparse.ArgumentParser(description='I3 Window Manager Colors Themer.') + argsSubParsers = argsMainParser.add_subparsers() + ##### Apply Parser + argsApplyParser = argsSubParsers.add_parser("apply") + argsApplyParser.add_argument('theme_path', type=str, nargs='?', + help='I3 YAML theme path.') + argsApplyParser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.') + argsApplyParser.add_argument('-d', '--dry' ,action='store_true', help='Do not apply theme, just print config file.') + argsApplyParser.set_defaults(func=apply) + ##### Extract Parser + argsExtractParser = argsSubParsers.add_parser("extract") + argsExtractParser.add_argument('config_path', type=str, nargs='?', + help='Extract theme from config file.') + argsExtractParser.set_defaults(func=extract) + ##### Aleatory Parser + argsAleatoryParser = argsSubParsers.add_parser("aleatory") + argsAleatoryParser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.') + argsAleatoryParser.set_defaults(func=aleatory) + ##### Launch i3-colors args = argsMainParser.parse_args() if len(sys.argv)>1: args.func(args) diff --git a/tests/run.sh b/tests/run.sh index 296eccf..93cad38 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -36,7 +36,7 @@ do exit 1 fi else - nb_pass=$(( nb_test + 1 )) + nb_pass=$(( nb_pass + 1 )) passed "${test_name}" fi done diff --git a/tests/test-simple-apply.out b/tests/test-simple-apply.out index 064868d..ac630f1 100644 --- a/tests/test-simple-apply.out +++ b/tests/test-simple-apply.out @@ -1,5 +1,3 @@ -Author: lasers -Url: https://github.com/okraits/j4-make-config ##### Variables ##### ##### Define workspaces names set $ws1 "1 " @@ -289,7 +287,7 @@ bar { position top font pango:DejaVu Sans Mono 14 status_command i3status -c ~/.config/i3/i3status.conf - separator_symbol "|" +separator_symbol "|" colors { active_workspace #414141 #2d2d2d #ffffff background #2d2d2d @@ -305,7 +303,7 @@ client.focused #4183F6 #2d76f6 #ffffff #d8442e #4183F6 client.focused_inactive #C1C1C1 #bbbbbb #ffffff #009c58 #C1C1C1 client.unfocused #414141 #2d2d2d #ffffff #bbbbbb #414141 client.urgent #FFCB21 #ffc609 #ffffff #d8442e #FFCB21 -Description: seti theme by Jody Ribton - based on the seti Atom theme at https://atom.io/themes/seti-ui + ##### Variables ##### ##### Define workspaces names set $ws1 "1 " @@ -595,7 +593,7 @@ bar { position top font pango:DejaVu Sans Mono 14 status_command i3status -c ~/.config/i3/i3status.conf - separator_symbol "|" +separator_symbol "|" colors { separator #AAAAAA background #1f2326 @@ -611,3 +609,4 @@ client.focused #4F99D3 #4F99D3 #151718 #9FCA56 #4F99D3 client.focused_inactive #9FCA56 #9FCA56 #151718 #A074C4 #9FCA56 client.unfocused #1f2326 #1f2326 #AAAAAA #A074C4 #1f2326 client.urgent #CE4045 #CE4045 #FFFFFF #DCCD69 #CE4045 + diff --git a/tests/test-simple-apply.sh b/tests/test-simple-apply.sh index e5ab960..2516906 100644 --- a/tests/test-simple-apply.sh +++ b/tests/test-simple-apply.sh @@ -3,15 +3,7 @@ wai=$(dirname $(readlink -f $0)) source "${wai}/include.sh" -##### Load config file -config_file=$(load ${data}/config) - ##### Test apply on theme with no variables -$exec apply ${data}/google ${config_file} -cat $config_file +$exec apply -d ${data}/google ##### Test apply on theme with variables -$exec apply ${data}/seti ${config_file} -cat $config_file - -##### Clear temporary file -rm $config_file +$exec apply -d ${data}/seti diff --git a/tests/test-simple-extract.out b/tests/test-simple-extract.out new file mode 100644 index 0000000..04aa616 --- /dev/null +++ b/tests/test-simple-extract.out @@ -0,0 +1,48 @@ +bar_colors: + active_workspace: + background: '#2d2d2d' + border: '#414141' + text: '#ffffff' + background: '#2d2d2d' + focused_workspace: + background: '#2d76f6' + border: '#4183F6' + text: '#ffffff' + inactive_workspace: + background: '#2d2d2d' + border: '#414141' + text: '#bbbbbb' + separator: '#bbbbbb' + statusline: '#ffffff' + urgent_workspace: + background: '#ffc609' + border: '#FFCB21' + text: '#ffffff' +meta: + description: Generated From i3-colors +window_colors: + focused: + background: '#2d76f6' + border: '#4183F6' + child_border: '#4183F6' + indicator: '#d8442e' + text: '#ffffff' + focused_inactive: + background: '#bbbbbb' + border: '#C1C1C1' + child_border: '#C1C1C1' + indicator: '#009c58' + text: '#ffffff' + unfocused: + background: '#2d2d2d' + border: '#414141' + child_border: '#414141' + indicator: '#bbbbbb' + text: '#ffffff' + urgent: + background: '#ffc609' + border: '#FFCB21' + child_border: '#FFCB21' + indicator: '#d8442e' + text: '#ffffff' + diff --git a/tests/test-simple-extract.sh b/tests/test-simple-extract.sh new file mode 100644 index 0000000..61e2844 --- /dev/null +++ b/tests/test-simple-extract.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +wai=$(dirname $(readlink -f $0)) +source "${wai}/include.sh" + +##### Test extract on a config file +$exec extract ${data}/config +