From 1a07781c4dbd102a8f681d51c897b863872302b8 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Wed, 17 Jul 2024 00:47:45 +0100 Subject: [PATCH] feat: the right width for records will now be auto calculated --- applications/dns/dns.nix | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/applications/dns/dns.nix b/applications/dns/dns.nix index ff80e89..6c355e2 100644 --- a/applications/dns/dns.nix +++ b/applications/dns/dns.nix @@ -30,7 +30,18 @@ sort_records_ptr = builtins.sort (a: b: (lib.strings.toInt a.record) < (lib.strings.toInt b.record)) (process_ptr (filter_records_type "PTR")); sort_records_srv = builtins.sort (a: b: a.record < b.record) (filter_records_type "SRV"); - format_records = records: offset: lib.strings.concatMapStrings (x: "${padString x.record offset} IN ${padString x.r_type 5} ${x.value}\n") records; + max = x: y: + assert builtins.isInt x; + assert builtins.isInt y; + if x < y + then y + else x; + max_len = records: lib.lists.foldr (a: b: (max a b)) 0 (lib.lists.forEach records (record: lib.strings.stringLength record.record)); + + format_records = records: let + offset = (max_len records) + 1; + in + lib.strings.concatMapStrings (x: "${padString x.record offset} IN ${padString x.r_type 5} ${x.value}\n") records; # small function to trim it down a tad padString = text: length: fixedWidthString_post length " " text; @@ -72,32 +83,32 @@ ; ------------------------------------------ ; Server Names (A Records) ; ------------------------------------------ - ${format_records sort_records_server 31} + ${format_records sort_records_server} ; ------------------------------------------ ; A (non server names ; ------------------------------------------ - ${format_records sort_records_a 31} + ${format_records sort_records_a} ; ------------------------------------------ ; CNAMES ; ------------------------------------------ - ${format_records sort_records_cname 31} + ${format_records sort_records_cname} ; ------------------------------------------ ; TXT ; ------------------------------------------ - ${format_records (filter_records_type "TXT") 31} + ${format_records (filter_records_type "TXT")} ; ------------------------------------------ ; MX ; ------------------------------------------ - ${format_records (filter_records_type "MX") 31} + ${format_records (filter_records_type "MX")} ; ------------------------------------------ ; SRV ; ------------------------------------------ - ${format_records sort_records_srv 65} + ${format_records sort_records_srv} '' @@ -125,7 +136,7 @@ ; ------------------------------------------ ; PTR ; ------------------------------------------ - ${format_records sort_records_ptr 3} + ${format_records sort_records_ptr} '' );