From bdf70b93147df3f139bfdb25c6748bec9bacdf70 Mon Sep 17 00:00:00 2001 From: Uriah Date: Mon, 9 Dec 2024 12:44:29 +0800 Subject: [PATCH] added ability to open config on default editor or specified editor --- Cargo.lock | 270 +++++++++++++++-------- Cargo.toml | 22 +- README.md | 10 + provision.sh | 2 +- scripts/postinstall | 2 +- src/actions.rs | 1 + src/actions/create_directory_and_file.rs | 1 + src/actions/open_file_with.rs | 49 ++++ src/helpers/get_app_config.rs | 3 +- src/helpers/is_fuzzy_match.rs | 8 +- src/main.rs | 14 +- src/models/commands_model.rs | 4 + src/models/snip_config_model.rs | 3 +- 13 files changed, 267 insertions(+), 122 deletions(-) create mode 100644 src/actions/open_file_with.rs diff --git a/Cargo.lock b/Cargo.lock index c683c20..b4a6d43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -28,23 +28,24 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.4" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anstyle-parse" @@ -76,9 +77,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.75" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "atty" @@ -124,6 +125,17 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" +[[package]] +name = "bstr" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bytes" version = "1.5.0" @@ -162,9 +174,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.11" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -172,21 +184,21 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.11" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim 0.10.0", + "strsim 0.11.1", ] [[package]] name = "clap_derive" -version = "4.4.7" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck", "proc-macro2", @@ -196,9 +208,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.6.0" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "colorchoice" @@ -227,6 +239,17 @@ dependencies = [ "memchr", ] +[[package]] +name = "dbus" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bb21987b9fb1613058ba3843121dd18b163b254d8a6e797e144cbac14d96d1b" +dependencies = [ + "libc", + "libdbus-sys", + "winapi", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -265,9 +288,9 @@ checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", "windows-sys 0.52.0", @@ -275,9 +298,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" [[package]] name = "getrandom" @@ -298,9 +321,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -328,6 +351,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.10" @@ -342,26 +371,35 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.151" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" + +[[package]] +name = "libdbus-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06085512b750d640299b79be4bad3d2fa90a9c00b1fd9e1b46364f66f0485c72" +dependencies = [ + "cc", + "pkg-config", +] [[package]] name = "libredox" -version = "0.0.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ "bitflags 2.4.1", "libc", - "redox_syscall", ] [[package]] name = "linux-raw-sys" -version = "0.4.12" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lock_api" @@ -375,9 +413,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.4" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" @@ -390,23 +428,22 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.10" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ "libc", "wasi", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] -name = "num_cpus" -version = "1.16.0" +name = "normpath" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +checksum = "c8911957c4b1549ac0dc74e30db9c8b0e66ddcd6d7acc33098f4c63a64a6d7ed" dependencies = [ - "hermit-abi 0.3.3", - "libc", + "windows-sys 0.59.0", ] [[package]] @@ -418,6 +455,24 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "opener" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0812e5e4df08da354c851a3376fead46db31c2214f849d3de356d774d057681" +dependencies = [ + "bstr", + "dbus", + "normpath", + "windows-sys 0.59.0", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -447,6 +502,12 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + [[package]] name = "prettytable" version = "0.10.0" @@ -463,18 +524,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.70" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -490,15 +551,21 @@ dependencies = [ [[package]] name = "redox_users" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" dependencies = [ "getrandom", "libredox", "thiserror", ] +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -507,9 +574,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.28" +version = "0.38.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" dependencies = [ "bitflags 2.4.1", "errno", @@ -538,18 +605,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", @@ -558,11 +625,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -584,12 +652,13 @@ checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" [[package]] name = "snip-cli" -version = "0.1.1" +version = "0.1.2" dependencies = [ "anyhow", - "clap 4.4.11", + "clap 4.5.23", "dirs-next", "dotenv", + "opener", "prettytable", "serde", "serde_json", @@ -615,15 +684,15 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "strsim" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.41" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -632,15 +701,15 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.8.1" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" dependencies = [ "cfg-if", "fastrand", - "redox_syscall", + "once_cell", "rustix", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -685,28 +754,27 @@ dependencies = [ [[package]] name = "tokio" -version = "1.35.0" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841d45b238a16291a4e1584e61820b8ae57d696cc5015c459c229ccc6990cc1c" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", @@ -780,7 +848,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -800,17 +877,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -821,9 +899,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -833,9 +911,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -845,9 +923,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.0" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -857,9 +941,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -869,9 +953,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -881,9 +965,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -893,6 +977,6 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.0" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index cd529ad..f74f2ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snip-cli" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["Uriah G. "] description = "A CLI tool (snip-cli) for managing Neovim and VSCode snippets" @@ -14,24 +14,20 @@ categories = ["command-line-utilities"] name = "snip" path = "src/main.rs" -# Used when Bunding for OSX -[[bin]] -name = "snip-cli" -path = "src/main.rs" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.75" -clap = { version = "4.4.11", features = ["cargo", "derive", "string"] } -serde = { version = "1.0.193", features = ["derive"] } -serde_json = "1.0.108" -tokio = { version = "1.35.0", features = ["full"] } -tempfile = { version = "3.8.1", features = [] } +anyhow = "1.0.94" +clap = { version = "4.5.23", features = ["cargo", "derive", "string"] } +serde = { version = "1.0.215", features = ["derive"] } +serde_json = "1.0.133" +tokio = { version = "1.42.0", features = ["full"] } +tempfile = { version = "3.14.0", features = [] } prettytable = "0.10.0" dirs-next = "2.0.0" dotenv = { version = "0.15.0", features = ["clap"] } +opener = "0.7.2" [package.metadata.bundle] name = "snip" # The name of your application @@ -40,7 +36,7 @@ copyright = "Copyright (c) codeitlikemiley 2023. All rights reserved." category = "Developer Tool" short_description = "A CLI tool for managing Neovim and VSCode snippets" long_description = "A CLI tool for managing Neovim and VSCode snippets" -version = "0.1.1" # Version of your application +version = "0.1.2" # Version of your application osx_url_schemes = [ "com.codeitlikemiley.snip", ] # URL schemes your application supports diff --git a/README.md b/README.md index 55f62d6..fc2c02d 100644 --- a/README.md +++ b/README.md @@ -230,3 +230,13 @@ snip config --help # Config Snippet snip config ``` + +10. Edit Config + +```sh +snip open +# or we can pass in the editor command e.g. code +snip open --editor code +``` + +This will open the configuration file in the default editor, or the editor specified with the `--editor` flag. diff --git a/provision.sh b/provision.sh index b411885..2425fac 100755 --- a/provision.sh +++ b/provision.sh @@ -4,4 +4,4 @@ cargo clean rm snip.pkg cargo zigbuild --release cargo bundle --release -pkgbuild --root ./target/release/bundle/osx/snip.app --install-location "/Applications/snip.app" --identifier com.codeitlikemiley.snip --version 0.1.0 --scripts ./scripts snip.pkg \ No newline at end of file +pkgbuild --root ./target/release/bundle/osx/snip.app --install-location "/Applications/snip.app" --identifier com.codeitlikemiley.snip --version 0.1.2 --scripts ./scripts snip.pkg \ No newline at end of file diff --git a/scripts/postinstall b/scripts/postinstall index db7f172..f67da52 100755 --- a/scripts/postinstall +++ b/scripts/postinstall @@ -17,4 +17,4 @@ if [ -L "$USER_HOME/.local/bin/snip" ]; then fi # Create the symlink in ~/.local/bin -ln -s "/Applications/snip.app/Contents/MacOS/snip-cli" "$USER_HOME/.local/bin/snip" +ln -s "/Applications/snip.app/Contents/MacOS/snip" "$USER_HOME/.local/bin/snip" diff --git a/src/actions.rs b/src/actions.rs index cf36bc5..21d3abc 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,6 +1,7 @@ pub mod create_directory_and_file; pub mod edit_snippet_in_file; pub mod list_snippets; +pub mod open_file_with; pub mod remove_snippet_from_file; pub mod search_snippets; pub mod show_snippet; diff --git a/src/actions/create_directory_and_file.rs b/src/actions/create_directory_and_file.rs index 61c7815..cb0c9fa 100644 --- a/src/actions/create_directory_and_file.rs +++ b/src/actions/create_directory_and_file.rs @@ -15,6 +15,7 @@ pub fn create_directory_and_file(file_path: &str) -> anyhow::Result<()> { OpenOptions::new() .write(true) .create(true) // This will create the file if it doesn't exist + .truncate(false) .open(file_path) .context("Failed to create or open the file")?; diff --git a/src/actions/open_file_with.rs b/src/actions/open_file_with.rs new file mode 100644 index 0000000..8f53009 --- /dev/null +++ b/src/actions/open_file_with.rs @@ -0,0 +1,49 @@ +use anyhow::{Context, Result}; +use opener; +use std::{fs, process::Command}; + +/// Opens the file specified in the configuration JSON file using the system's default text editor. +/// +/// # Arguments +/// +/// * `config_path` - A string slice that holds the path to the JSON configuration file. +/// +/// # Returns +/// +/// * `Result<()>` - Returns `Ok` if the file is successfully opened, otherwise returns an error. +pub fn open_file_with(config_path: &str, editor: Option) -> Result<()> { + // Read the configuration file + let config_content = fs::read_to_string(config_path) + .with_context(|| format!("Failed to read the configuration file at: {}", config_path))?; + + // Parse the JSON to extract the `path` field + let config: serde_json::Value = serde_json::from_str(&config_content) + .with_context(|| "Failed to parse the configuration file as JSON")?; + + // Extract the `path` field + let file_path = config["path"].as_str().ok_or_else(|| { + anyhow::anyhow!("`path` field is missing or invalid in configuration file") + })?; + + // Open the file with the default editor + // If the editor is not provided, use the default editor + if editor.is_none() { + opener::open(file_path) + .with_context(|| format!("Failed to open the file at path: {}", file_path)) + .map_err(|err| anyhow::anyhow!("Failed to open the file: {}", err))?; + } else { + // Use the provided editor + let editor = editor.unwrap(); + // get the path to the editor + Command::new(editor.clone()) + .arg(file_path) + .spawn() + .with_context(|| { + format!( + "Failed to open the file at path: {} with program: {}", + file_path, editor + ) + })?; + } + Ok(()) +} diff --git a/src/helpers/get_app_config.rs b/src/helpers/get_app_config.rs index 119fabe..d76bf18 100644 --- a/src/helpers/get_app_config.rs +++ b/src/helpers/get_app_config.rs @@ -19,7 +19,6 @@ pub fn get_app_config() -> String { #[cfg(test)] mod tests { use super::*; - use std::io::{stdout, Write}; #[test] #[ignore] @@ -34,7 +33,7 @@ mod tests { // Act let actual = get_app_config(); // For debugging purpose on CI - writeln!(stdout(), "DEBUGGER ENV CONFIG PATH: {}", actual).unwrap(); + println!("DEBUGGER ENV CONFIG PATH: {}", actual); // Assert assert_eq!(actual, expected); } diff --git a/src/helpers/is_fuzzy_match.rs b/src/helpers/is_fuzzy_match.rs index 678c446..8f0eb3c 100644 --- a/src/helpers/is_fuzzy_match.rs +++ b/src/helpers/is_fuzzy_match.rs @@ -16,9 +16,9 @@ mod tests { #[test] fn test_is_fuzzy_match() { - assert_ne!(is_fuzzy_match("moon", "bd"), true); - assert_ne!(is_fuzzy_match("moon", "mp"), true); - assert_eq!(is_fuzzy_match("moon", "mn"), true); - assert_eq!(is_fuzzy_match("moon", "oon"), true); + assert!(!is_fuzzy_match("moon", "bd")); + assert!(!is_fuzzy_match("moon", "mp")); + assert!(is_fuzzy_match("moon", "mn")); + assert!(is_fuzzy_match("moon", "oon")); } } diff --git a/src/main.rs b/src/main.rs index 429f7f8..0b72454 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use clap::Parser; use snip_cli::actions::create_directory_and_file::create_directory_and_file; use snip_cli::actions::edit_snippet_in_file::edit_snippet_in_file; use snip_cli::actions::list_snippets::list_snippets; +use snip_cli::actions::open_file_with::open_file_with; use snip_cli::actions::remove_snippet_from_file::remove_snippet_from_file; use snip_cli::actions::search_snippets::search_snippets; use snip_cli::actions::show_snippet::show_snippet; @@ -14,16 +15,13 @@ use snip_cli::helpers::get_app_config::get_app_config; use snip_cli::models::cli_model::Cli; use snip_cli::models::commands_model::Commands; use snip_cli::models::snip_config_model::SnipConfig; -use std::io::Write; #[tokio::main] async fn main() -> Result<()> { let config_path = get_app_config(); - // Ensure the config directory exists create_directory_and_file(&config_path)?; - // Load or create the configuration let config = match SnipConfig::load(&config_path) { Ok(cfg) => cfg, Err(_) => { @@ -59,7 +57,7 @@ async fn main() -> Result<()> { dbg!(list_option); let output = list_snippets(&config.path, list_option) .context("Failed to list snippets from file")?; - writeln!(std::io::stdout(), "{}", output).unwrap(); + println!("{}", output); } Commands::Edit { key, @@ -75,7 +73,7 @@ async fn main() -> Result<()> { dbg!(&key); let output = show_snippet(&config.path, key).context("Failed to show snippet from file")?; - writeln!(std::io::stdout(), "{}", output).unwrap(); + println!("{}", output); } Commands::Search { id, name } => { dbg!(id, &name); @@ -83,7 +81,7 @@ async fn main() -> Result<()> { .context("Failed to search snippet from file")?; for result in output { - writeln!(std::io::stdout(), "{}\n", result).unwrap(); + println!("{}\n", result); } } Commands::UpdateKey { old_key, new_key } => { @@ -101,6 +99,10 @@ async fn main() -> Result<()> { println!("Configuration updated."); } } + Commands::Open { editor } => { + open_file_with(&config_path, editor) + .context("Failed to open the configuration file")?; + } } Ok(()) diff --git a/src/models/commands_model.rs b/src/models/commands_model.rs index 174d144..ee936d6 100644 --- a/src/models/commands_model.rs +++ b/src/models/commands_model.rs @@ -49,4 +49,8 @@ pub enum Commands { #[arg(short, long)] new_key: String, }, + Open { + #[arg(short, long)] + editor: Option, + }, } diff --git a/src/models/snip_config_model.rs b/src/models/snip_config_model.rs index 3a9ee99..197848f 100644 --- a/src/models/snip_config_model.rs +++ b/src/models/snip_config_model.rs @@ -2,7 +2,6 @@ use crate::helpers::expand_home_dir::expand_home_dir; use anyhow::Context; use serde::{Deserialize, Serialize}; use std::fs; -use std::io::{stdout, Write}; #[derive(Serialize, Deserialize, Debug)] pub struct SnipConfig { @@ -14,7 +13,7 @@ impl SnipConfig { let config_content = fs::read_to_string(path).context("Failed to read config file")?; let config: SnipConfig = serde_json::from_str(&config_content).context("Failed to parse config file")?; - writeln!(stdout(), "{}", &config.path).unwrap(); + println!("{}", &config.path); Ok(config) }