From 4858194ef4d6a093156c8eaed7453f43be32cae1 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 11 Apr 2020 19:21:52 -0700 Subject: [PATCH] wezterm: lua: add glob and read_dir These functions are useful when dynamically constructing configuration. --- Cargo.lock | 195 ++++++++++++++++++++++----------------- Cargo.toml | 1 + docs/config/lua.markdown | 41 ++++++++ src/scripting/mod.rs | 39 ++++++++ 4 files changed, 190 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72dede111..bf91d9985 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,7 @@ dependencies = [ "rusttype 0.7.9", "walkdir", "xdg", - "xml-rs 0.8.0", + "xml-rs 0.8.2", ] [[package]] @@ -82,9 +82,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.27" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "013a6e0a2cbe3d20f9c60b65458f7a7f7a5e636c5d0f45a5a6aee5d4b1f01785" +checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff" [[package]] name = "approx" @@ -159,11 +159,11 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.27" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "991d0a1a3e790c835fd54ab41742a59251338d8c7577fe7d7f0170c7072be708" +checksum = "da71fef07bc806586090247e971229289f64c210a278ee5ae419314eb386b31d" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -277,6 +277,17 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bstr" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59604ece62a407dc9164732e5adea02467898954c3a5811fd2dc140af14ef15b" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", +] + [[package]] name = "bstr" version = "0.2.12" @@ -787,9 +798,9 @@ dependencies = [ [[package]] name = "euclid" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57d9ae79e19d69ffb5b47b0a744c4215c115dfed4039314c9ca1b8ddc6c333be" +checksum = "0c6a5b0c779cd0b744c73a1d2083faf181080d696903cdad99a3b03d015d7030" dependencies = [ "num-traits 0.2.11", ] @@ -830,7 +841,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "030a733c8287d6213886dd487564ff5c8f6aae10278b3588ed177f9d18f8d231" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "synstructure", @@ -847,10 +858,22 @@ dependencies = [ ] [[package]] -name = "filetime" -version = "0.2.8" +name = "filenamegen" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d" +checksum = "48d91c770518d7b0f5637f87b4305cb9258cf886d29a51bb862dee9b88d55f6c" +dependencies = [ + "anyhow", + "bstr 0.1.4", + "regex", + "walkdir", +] + +[[package]] +name = "filetime" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f59efc38004c988e4201d11d263b8171f49a2e7ec0bdbb71773433f271504a5e" dependencies = [ "cfg-if", "libc", @@ -1048,7 +1071,7 @@ checksum = "ca98bbde17256e02d17336a6bdb5a50f7d0ccacee502e191d3e3d0ec2f96f84a" dependencies = [ "khronos_api", "log", - "xml-rs 0.8.0", + "xml-rs 0.8.2", ] [[package]] @@ -1059,7 +1082,7 @@ checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" dependencies = [ "khronos_api", "log", - "xml-rs 0.8.0", + "xml-rs 0.8.2", ] [[package]] @@ -1073,7 +1096,7 @@ dependencies = [ "gl_generator 0.14.0", "lazy_static", "memoffset", - "smallvec 1.2.0", + "smallvec 1.3.0", "takeable-option", ] @@ -1132,9 +1155,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.8" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8" +checksum = "725cf19794cf90aa94e65050cb4191ff5d8fa87a498383774c47b332e3af952e" dependencies = [ "libc", ] @@ -1399,9 +1422,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79b2de95ecb4691949fea4716ca53cdbcfccb2c612e19644a8bad05edcf9f47b" +checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" dependencies = [ "scopeguard", ] @@ -1644,7 +1667,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "869c35a04c6b414eee44268afb7c75a4904ffeb31cf3b27a5c7bc10d1fe62cca" dependencies = [ - "bstr", + "bstr 0.2.12", "cc", "lua-src", "luajit-src", @@ -1890,9 +1913,9 @@ dependencies = [ [[package]] name = "openssl" -version = "0.10.28" +version = "0.10.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "973293749822d7dd6370d6da1e523b0d1db19f06c459134c658b2a4261378b52" +checksum = "cee6d85f4cb4c4f59a6a85d5b68a233d280c82e29e822913b9c8b129fbf20bdd" dependencies = [ "bitflags 1.2.1", "cfg-if", @@ -1904,18 +1927,18 @@ dependencies = [ [[package]] name = "openssl-src" -version = "111.7.0+1.1.1e" +version = "111.8.1+1.1.1f" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fde5a8c01ef8aa31ff8d0aaf9bae248581ed8840fca0b66e51cc9f294a8cb2c" +checksum = "f04f0299a91de598dde58d2e99101895498dcf3d58896a3297798f28b27c8b72" dependencies = [ "cc", ] [[package]] name = "openssl-sys" -version = "0.9.54" +version = "0.9.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1024c0a59774200a555087a6da3f253a9095a5f344e353b212ac4c8b8e450986" +checksum = "7717097d810a0f2e2323f9e5d11e71608355e24828410b55b9d4f18aa5f9a5d8" dependencies = [ "autocfg 1.0.0", "cc", @@ -1962,7 +1985,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b4b5f600e60dd3a147fb57b4547033d382d1979eb087af310e91cb45a63b1f4" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -1980,12 +2003,12 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.10.0" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" +checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" dependencies = [ "lock_api", - "parking_lot_core 0.7.0", + "parking_lot_core 0.7.1", ] [[package]] @@ -2005,15 +2028,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7582838484df45743c8434fbff785e8edf260c28748353d44bc0da32e0ceabf1" +checksum = "0e136c1904604defe99ce5fd71a28d473fa60a12255d511aa78a9ddf11237aeb" dependencies = [ "cfg-if", "cloudabi", "libc", "redox_syscall", - "smallvec 1.2.0", + "smallvec 1.3.0", "winapi 0.3.8", ] @@ -2167,12 +2190,12 @@ dependencies = [ [[package]] name = "proc-macro-error" -version = "0.4.12" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "version_check 0.9.1", @@ -2180,11 +2203,11 @@ dependencies = [ [[package]] name = "proc-macro-error-attr" -version = "0.4.12" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "syn-mid", @@ -2193,9 +2216,9 @@ dependencies = [ [[package]] name = "proc-macro-hack" -version = "0.5.14" +version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcfdefadc3d57ca21cf17990a28ef4c0f7c61383a28cb7604cf4a18e6ede1420" +checksum = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" [[package]] name = "proc-macro2" @@ -2208,9 +2231,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435" +checksum = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3" dependencies = [ "unicode-xid 0.2.0", ] @@ -2254,7 +2277,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", ] [[package]] @@ -2506,17 +2529,16 @@ version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5" dependencies = [ - "rusttype 0.8.2", + "rusttype 0.8.3", ] [[package]] name = "rusttype" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14a911032fb5791ccbeec9f28fdcb9bf0983b81f227bafdfd227c658d0731c8a" +checksum = "9f61411055101f7b60ecf1041d87fb74205fb20b0c7a723f07ef39174cf6b4c0" dependencies = [ "approx", - "arrayvec 0.5.1", "ordered-float", "stb_truetype", ] @@ -2565,20 +2587,20 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e707fbbf255b8fc8c3b99abb91e7257a622caeb20a9818cbadbeeede4e0932ff" +checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac5d00fc561ba2724df6758a17de23df5914f20e41cb00f94d5b7ae42fffaff8" +checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -2715,9 +2737,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c2fb2ec9bcd216a5b0d0ccf31ab17b5ed1d627960edff65bbe95d3ce221cefc" +checksum = "05720e22615919e4734f6a99ceae50d00226c3c5aca406e102ebc33298214e0a" [[package]] name = "smithay-client-toolkit" @@ -2737,9 +2759,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.3.11" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" +checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" dependencies = [ "cfg-if", "libc", @@ -2762,7 +2784,7 @@ dependencies = [ "bitflags 1.2.1", "libc", "libssh2-sys", - "parking_lot 0.10.0", + "parking_lot 0.10.2", ] [[package]] @@ -2794,9 +2816,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "structopt" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8faa2719539bbe9d77869bfb15d4ee769f99525e707931452c97b693b3f159d" +checksum = "ff6da2e8d107dfd7b74df5ef4d205c6aebee0706c647f6bc6a2d5789905c00fb" dependencies = [ "clap", "lazy_static", @@ -2805,13 +2827,13 @@ dependencies = [ [[package]] name = "structopt-derive" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f88b8e18c69496aad6f9ddf4630dd7d585bcaf765786cb415b9aec2fe5a0430" +checksum = "a489c87c08fbaf12e386665109dd13470dcc9c4583ea3e10dd2b4523e5ebd9ac" dependencies = [ "heck", "proc-macro-error", - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -2860,7 +2882,7 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "unicode-xid 0.2.0", ] @@ -2871,7 +2893,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -2891,7 +2913,7 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "unicode-xid 0.2.0", @@ -2973,9 +2995,9 @@ dependencies = [ [[package]] name = "termios" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" +checksum = "6f0fcee7b24a25675de40d5bb4de6e41b0df07bc9856295e7e2b3a3600c400c2" dependencies = [ "libc", ] @@ -3004,7 +3026,7 @@ dependencies = [ "signal-hook", "smallvec 0.6.13", "terminfo", - "termios 0.3.1", + "termios 0.3.2", "unicode-segmentation", "unicode-width", "varbincode", @@ -3024,20 +3046,20 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3711fd1c4e75b3eff12ba5c40dba762b6b65c5476e8174c1a664772060c49bf" +checksum = "54b3d3d2ff68104100ab257bb6bb0cb26c901abe4bd4ba15961f3bf867924012" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae2b85ba4c9aa32dd3343bd80eb8d22e9b54b7688c17ea3907f236885353b233" +checksum = "ca972988113b7715266f91250ddb98070d033c62a011fa0fcc57434a649310dd" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -3076,9 +3098,9 @@ dependencies = [ [[package]] name = "tokio" -version = "0.2.13" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa5e81d6bc4e67fe889d5783bd2a128ab2e0cfa487e0be16b6a8d177b101616" +checksum = "39fb9142eb6e9cc37f4f29144e62618440b149a138eee01a7bbe9b9226aaf17c" dependencies = [ "bytes", "fnv", @@ -3104,7 +3126,7 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", ] @@ -3145,7 +3167,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" dependencies = [ - "smallvec 1.2.0", + "smallvec 1.3.0", ] [[package]] @@ -3316,7 +3338,7 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "wasm-bindgen-shared", @@ -3338,7 +3360,7 @@ version = "0.2.60" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d68a5b36eef1be7868f668632863292e37739656a80fc4b9acec7b0bd35a4931" dependencies = [ - "proc-macro2 1.0.9", + "proc-macro2 1.0.10", "quote 1.0.3", "syn 1.0.17", "wasm-bindgen-backend", @@ -3396,7 +3418,7 @@ checksum = "93b02247366f395b9258054f964fe293ddd019c3237afba9be2ccbe9e1651c3d" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.13", - "xml-rs 0.8.0", + "xml-rs 0.8.2", ] [[package]] @@ -3429,7 +3451,7 @@ dependencies = [ "base64 0.10.1", "base91", "bitflags 1.2.1", - "bstr", + "bstr 0.2.12", "core-foundation 0.7.0", "core-graphics 0.19.0", "core-text 15.0.0", @@ -3443,6 +3465,7 @@ dependencies = [ "failure", "failure_derive", "filedescriptor", + "filenamegen", "font-kit", "font-loader", "fontconfig", @@ -3525,9 +3548,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" +checksum = "fa515c5163a99cc82bab70fd3bfdd36d827be85de63737b40fcef2ce084a436e" dependencies = [ "winapi 0.3.8", ] @@ -3693,9 +3716,9 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5" +checksum = "2bb76e5c421bbbeb8924c60c030331b345555024d56261dae8f3e786ed817c23" [[package]] name = "yasna" diff --git a/Cargo.toml b/Cargo.toml index d3b6fe796..daff38572 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ dirs = "1.0" downcast-rs = "1.0" euclid = "0.20" filedescriptor = { version="0.7", path = "filedescriptor" } +filenamegen = "0.2" pretty_env_logger = "0.4" failure = "0.1" failure_derive = "0.1" diff --git a/docs/config/lua.markdown b/docs/config/lua.markdown index b52acf286..dbafeed04 100644 --- a/docs/config/lua.markdown +++ b/docs/config/lua.markdown @@ -211,3 +211,44 @@ return { font_size = font_size } ``` + +### `wezterm.read_dir(path)` + +*Since: nightly* + +This function returns an array containing the absolute file names of the +directory specified. Due to limitations in the lua bindings, all of the paths +must be able to be represented as UTF-8 or this function will generate an +error. + +```lua +local wezterm = require 'wezterm'; + +-- logs the names of all of the entries under `/etc` +for _, v in ipairs(wezterm.read_dir("/etc")) do + wezterm.log_error("entry: " .. v) +end +``` + +### `wezterm.glob(pattern [, relative_to])` + +*Since: nightly* + +This function evalutes the glob `pattern` and returns an array containing the +absolute file names of the matching results. Due to limitations in the lua +bindings, all of the paths must be able to be represented as UTF-8 or this +function will generate an error. + +The optional `relative_to` parameter can be used to make the results relative +to a path. If the results have the same prefix as `relative_to` then it will +be removed from the returned path. + +```lua +local wezterm = require 'wezterm'; + +-- logs the names of all of the conf files under `/etc` +for _, v in ipairs(wezterm.glob("/etc/*.conf")) do + wezterm.log_error("entry: " .. v) +end +``` + diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index 3ad3a5b89..40ffd1dee 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -127,6 +127,9 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result { wezterm_mod.set("hostname", lua.create_function(hostname)?)?; wezterm_mod.set("action", lua.create_function(action)?)?; + wezterm_mod.set("read_dir", lua.create_function(read_dir)?)?; + wezterm_mod.set("glob", lua.create_function(glob)?)?; + package.set("path", path_array.join(";"))?; let loaded: Table = package.get("loaded")?; @@ -212,3 +215,39 @@ fn action<'lua>(lua: &'lua Lua, action: Table<'lua>) -> mlua::Result(_: &'lua Lua, path: String) -> mlua::Result> { + let dir = std::fs::read_dir(path).map_err(|e| mlua::Error::external(e))?; + let mut entries = vec![]; + for entry in dir { + let entry = entry.map_err(|e| mlua::Error::external(e))?; + if let Some(utf8) = entry.path().to_str() { + entries.push(utf8.to_string()); + } else { + return Err(mlua::Error::external(anyhow!( + "path entry {} is not representable as utf8", + entry.path().display() + ))); + } + } + Ok(entries) +} + +fn glob<'lua>( + _: &'lua Lua, + (pattern, path): (String, Option), +) -> mlua::Result> { + let mut entries = vec![]; + let glob = filenamegen::Glob::new(&pattern).map_err(|e| mlua::Error::external(e))?; + for path in glob.walk(path.as_ref().map(|s| s.as_str()).unwrap_or(".")) { + if let Some(utf8) = path.to_str() { + entries.push(utf8.to_string()); + } else { + return Err(mlua::Error::external(anyhow!( + "path entry {} is not representable as utf8", + path.display() + ))); + } + } + Ok(entries) +}