Skip to content

Commit 17b4953

Browse files
author
Seeker04
committed
Option added to only dump settings that differ from the defaults
1 parent 505a7ff commit 17b4953

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ hooks([
250250

251251
* `set/2` and `add/2` can be used to overwrite or append to existing settings, respectively. You can invoke them via a keymap, the [command fifo](#scriptability) or the [command menu](#menus).
252252
* The whole configuration file can be reloaded by calling `reload_config/0`.
253-
* You can use `dump_settings/1` to dump all current settings to a file.
253+
* You can use `dump_settings(Path, false)` to dump all current settings and `dump_settings(Path, true)` to dump only those that differ from the defaults to a file. Both are available in the command menu.
254254

255255
## External bars
256256

src/menu.pl

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@
367367
cmd_desc(menu:reindex_workspace, "Reindex selected workspace").
368368
cmd_desc(menu:delete_workspaces, "Delete selected workspaces").
369369
cmd_desc(menu:list_keymaps , "List all defined keymaps").
370+
cmd_desc(shellcmd , "Run a shell command").
371+
cmd_desc(reload_config , "Reload configuration file").
372+
cmd_desc(dump_settings , "Dump current settings to a file").
373+
cmd_desc(dump_changed_settings , "Dump settings that differ from the defaults to a file").
370374
cmd_desc(shellcmd(Cmd), D) :- format(string(D), "Run `~s`", [Cmd]).
371-
cmd_desc(shellcmd, "Run a shell command").
372-
cmd_desc(reload_config, "Reload configuration file").
373-
cmd_desc(dump_settings, "Dump current settings to a file").
374375
cmd_desc(set(Setting), D) :- string_concat("Change setting ", Setting, D).
375376
cmd_desc(add(Setting), D) :- string_concat("Add to setting ", Setting, D).
376377

@@ -437,10 +438,19 @@
437438

438439
%! dump_settings_prompt() is det
439440
%
440-
% Prompts the user for a path to dump settings to. See dump_settings/1 for details.
441+
% Prompts the user for a path to dump settings to. See dump_settings/2 for details.
441442
dump_settings_prompt() :-
442443
(read_from_prompt("Dump settings to", Input) ->
443-
dump_settings(Input)
444+
dump_settings(Input, false)
445+
; true)
446+
.
447+
448+
%! dump_changed_settings_prompt() is det
449+
%
450+
% Prompts the user for a path to dump changed settings to. See dump_settings/2 for details.
451+
dump_changed_settings_prompt() :-
452+
(read_from_prompt("Dump changed settings to", Input) ->
453+
dump_settings(Input, true)
444454
; true)
445455
.
446456

@@ -454,12 +464,13 @@
454464
% @arg Selection list of selections - unused, filled be spawn_menu/3
455465
run_cmd(MenuEntries, [Selection]) :-
456466
(member(Cmd-Selection, MenuEntries) ->
457-
(Cmd = set(Setting) -> change_setting_prompt(Setting, false)
458-
;Cmd = add(Setting) -> change_setting_prompt(Setting, true)
459-
;Cmd = change_nmaster -> change_nmaster_prompt
460-
;Cmd = change_mfact -> change_mfact_prompt
461-
;Cmd = shellcmd -> shellcmd_prompt
462-
;Cmd = dump_settings -> dump_settings_prompt
467+
(Cmd = set(Setting) -> change_setting_prompt(Setting, false)
468+
;Cmd = add(Setting) -> change_setting_prompt(Setting, true)
469+
;Cmd = change_nmaster -> change_nmaster_prompt
470+
;Cmd = change_mfact -> change_mfact_prompt
471+
;Cmd = shellcmd -> shellcmd_prompt
472+
;Cmd = dump_settings -> dump_settings_prompt
473+
;Cmd = dump_changed_settings -> dump_changed_settings_prompt
463474
;call(Cmd))
464475
; true)
465476
.
@@ -591,6 +602,7 @@
591602
shellcmd,
592603
reload_config,
593604
dump_settings,
605+
dump_changed_settings,
594606
set(default_nmaster),
595607
set(default_mfact),
596608
set(default_layout),

src/plwm.pl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,18 +2215,21 @@
22152215
nb_delete(settings_snapshot)
22162216
.
22172217

2218-
%! dump_settings(++FilePath:string) is det
2218+
%! dump_settings(++FilePath:string, ++OnlyChanged:bool) is det
22192219
%
2220-
% Dumps all current settings to a file.
2220+
% Dumps current settings to a file.
22212221
%
22222222
% @arg FilePath path of file to create
2223-
dump_settings(FilePath) :-
2223+
% @arg OnlyChanged whether to only dump settings that differ from their default values
2224+
dump_settings(FilePath, OnlyChanged) :-
22242225
open(FilePath, write, DumpFile),
22252226
forall(setting:setting(Setting), (
22262227
call(Setting, Value),
2227-
compound_name_arguments(Config, Setting, [Value]),
2228-
write_term(DumpFile, Config, [quoted(true), spacing(next_argument)]),
2229-
writeln(DumpFile, ".")
2228+
((OnlyChanged = false ; \+ setting:default_set(Setting, Value)) ->
2229+
compound_name_arguments(Config, Setting, [Value]),
2230+
write_term(DumpFile, Config, [quoted(true), spacing(next_argument)]),
2231+
writeln(DumpFile, ".")
2232+
; true)
22302233
)),
22312234
close(DumpFile)
22322235
.

tests/unit_tests/menu.plt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,20 +657,34 @@ test("change_setting_prompt - (add)", [
657657
menu:change_setting_prompt(hooks, true)
658658
.
659659

660-
test("change_setting_prompt", [
660+
test("dump_settings_prompt", [
661661
setup((
662662
% we omit from the output the prompt read_from_prompt/0 appends
663663
assertz(menu:menucmd(["sh", "-c", "echo $0", "somefile"])),
664-
assertz(menu:dump_settings(FilePath) :- string(FilePath))
664+
assertz(menu:dump_settings(FilePath, false) :- string(FilePath))
665665
)),
666666
cleanup((
667667
retract(menu:menucmd(_)),
668-
retract(menu:dump_settings(_))
668+
retract(menu:dump_settings(_, _))
669669
))
670670
]) :-
671671
assertion(menu:dump_settings_prompt)
672672
.
673673

674+
test("dump_changed_settings_prompt", [
675+
setup((
676+
% we omit from the output the prompt read_from_prompt/0 appends
677+
assertz(menu:menucmd(["sh", "-c", "echo $0", "somefile"])),
678+
assertz(menu:dump_settings(FilePath, true) :- string(FilePath))
679+
)),
680+
cleanup((
681+
retract(menu:menucmd(_)),
682+
retract(menu:dump_settings(_, _))
683+
))
684+
]) :-
685+
assertion(menu:dump_changed_settings_prompt)
686+
.
687+
674688
test("run_cmd +") :-
675689
assertion(menu:run_cmd([false-"foo", true-"selected", false-"bar"], ["selected"])),
676690
assertion(menu:run_cmd([false-"foo", false-"bar"], ["selected"]))

0 commit comments

Comments
 (0)