From 77d1df41c9b13e306616853a2318c0821ed9cd5d Mon Sep 17 00:00:00 2001 From: Nguyen Dinh Linh <50461902+linhx@users.noreply.github.com> Date: Mon, 8 Jan 2024 01:09:14 +0700 Subject: [PATCH 1/2] feat: support fishshell; enhance install script (#7) * feat: - support fish - using config file instead of ENV - enhance install script - uninstall script --- README.md | 17 +++-- bookmark/bookmark.go | 13 +++- config.yaml | 2 + go.mod | 24 ++++++- go.sum | 46 ++++++++++++ install | 73 +++++++++++++++----- main.go | 22 ++++++ key-bindings.bash => shell/key-bindings.bash | 4 +- shell/key-bindings.fish | 18 +++++ key-bindings.zsh => shell/key-bindings.zsh | 4 +- uninstall | 36 ++++++++++ 11 files changed, 228 insertions(+), 31 deletions(-) create mode 100644 config.yaml rename key-bindings.bash => shell/key-bindings.bash (86%) create mode 100644 shell/key-bindings.fish rename key-bindings.zsh => shell/key-bindings.zsh (78%) create mode 100755 uninstall diff --git a/README.md b/README.md index aaeb6f2..1043fec 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,7 @@ A commands bookmark for terminal ## Worked on -- Linux bash -- Linux zsh +- Linux: bash, zsh, fish - Mac zsh ## How to install @@ -15,7 +14,11 @@ A commands bookmark for terminal 1. Download built file on release page 2. Extract the file. e.g. /somepath/tbmk 3. Run `cd /somepath/tbmk` -4. Run `./install` (don't install by execute `absolute-path/install`), it will appends keybinding to `~/.bashrc` and `~/.zsh` +4. Run `./install` (don't install by execute `/absolute-path/install`), it will appends keybinding to `~/.bashrc`, `~/.zsh` and `~/.config/fish/config.fish` +5. Restart your shell or reload config file: + - `source ~/.bashrc # bash` + - `source ~/.zshrc # zsh` + - `source ~/.config/fish/config.fish #fish` ## How to use @@ -30,7 +33,13 @@ TODO - [ ] Windows -## Develop +## Development + +### Run + +```shell +APP_ENV=dev go run . +``` ### Build diff --git a/bookmark/bookmark.go b/bookmark/bookmark.go index 5f7294f..4910744 100644 --- a/bookmark/bookmark.go +++ b/bookmark/bookmark.go @@ -2,13 +2,16 @@ package bookmark import ( "fmt" + "strings" "os" + "path/filepath" common "linhx.com/tbmk/common" "github.com/sahilm/fuzzy" "github.com/sonyarouje/simdb" "github.com/google/uuid" + "github.com/spf13/viper" ) type BookmarkItem struct { @@ -27,9 +30,17 @@ type BookmarkRepo struct { db *simdb.Driver } +func getAbsolutePath(path string) string { + if strings.HasPrefix(path, "~/") { + home, _ := os.UserHomeDir() + return filepath.Join(home, path[2:]) + } + return path +} + func NewBookmarkRepo() (*BookmarkRepo, error) { repo := new(BookmarkRepo) - driver, err := simdb.New(os.Getenv("TBMK_DATA_DIR")) + driver, err := simdb.New(getAbsolutePath(viper.GetString("tbmk.dataDir"))) repo.db = driver return repo, err } diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..6261336 --- /dev/null +++ b/config.yaml @@ -0,0 +1,2 @@ +tbmk: + dataDir: ~/.tbmk diff --git a/go.mod b/go.mod index 156c779..84d49c9 100644 --- a/go.mod +++ b/go.mod @@ -23,22 +23,40 @@ require ( github.com/charmbracelet/bubbles v0.14.0 // indirect github.com/charmbracelet/lipgloss v0.5.0 // indirect github.com/containerd/console v1.0.3 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/google/uuid v1.5.0 // indirect github.com/gookit/color v1.5.2 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.16 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-localereader v0.0.1 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.13.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sahilm/fuzzy v0.1.0 // indirect github.com/sonyarouje/simdb v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.18.2 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect - golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect linhx.com/tbmk/common v0.0.0-00010101000000-000000000000 // indirect ) diff --git a/go.sum b/go.sum index bde1215..5f49e25 100644 --- a/go.sum +++ b/go.sum @@ -15,17 +15,26 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI= github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= @@ -33,6 +42,8 @@ github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= @@ -45,22 +56,51 @@ github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs= github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0= github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/sonyarouje/simdb v0.1.0 h1:oCxF05HdVmG3vdZZf5oLINIS0UYLoZBVx/IOJzpMA2A= github.com/sonyarouje/simdb v0.1.0/go.mod h1:sBxWOZxv78yOmCzIyXbUWzHua9+QpXwwnFdlLK/UiUU= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -69,12 +109,18 @@ golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/install b/install index 6d3274e..5407e15 100755 --- a/install +++ b/install @@ -1,34 +1,69 @@ +#!/usr/bin/env bash + DATA_DIR="$HOME/.tbmk" +mkdir -p $DATA_DIR + APP_DIR=$(pwd) +echo "Create symlink /usr/local/bin/tbmk" +sudo ln -sf "$APP_DIR/tbmk" /usr/local/bin/tbmk # for bash FILE_BASHRC=~/.bashrc if [ -f "$FILE_BASHRC" ]; then - BASH_SCRIPT_KEYBINDING="$APP_DIR/key-bindings.bash" - - if ! grep -q "^export TBMK" ~/.bashrc ; then - printf "export TBMK='${APP_DIR}/tbmk'\n" >> ~/.bashrc - printf "export TBMK_DATA_DIR='${DATA_DIR}'\n" >> ~/.bashrc - printf "if [ -f '$BASH_SCRIPT_KEYBINDING' ]; then\n" >> ~/.bashrc - printf " . '$BASH_SCRIPT_KEYBINDING'\n" >> ~/.bashrc - printf "fi\n" >> ~/.bashrc + BASH_SCRIPT_KEYBINDING="$APP_DIR/shell/key-bindings.bash" + RC_FILE="$DATA_DIR/.bash" + tee $RC_FILE > /dev/null <> $FILE_BASHRC fi -else - echo "$FILE_BASHRC does not exist." +else + echo "warn: $FILE_BASHRC does not exist." fi # for zsh FILE_ZSHRC=~/.zshrc if [ -f "$FILE_ZSHRC" ]; then - ZSH_SCRIPT_KEYBINDING="$APP_DIR/key-bindings.zsh" + ZSH_SCRIPT_KEYBINDING="$APP_DIR/shell/key-bindings.zsh" + RC_FILE="$DATA_DIR/.zsh" + tee $RC_FILE > /dev/null <> $FILE_ZSHRC + fi +else + echo "warn: $FILE_ZSHRC does not exist." +fi - if ! grep -q "^export TBMK" ~/.zshrc ; then - printf "export TBMK='${APP_DIR}/tbmk'\n" >> ~/.zshrc - printf "export TBMK_DATA_DIR='${DATA_DIR}'\n" >> ~/.zshrc - printf "if [ -f '$ZSH_SCRIPT_KEYBINDING' ]; then\n" >> ~/.zshrc - printf " . '$ZSH_SCRIPT_KEYBINDING'\n" >> ~/.zshrc - printf "fi\n" >> ~/.zshrc +# for fish +FILE_FISHRC=~/.config/fish/config.fish +if [ -f "$FILE_FISHRC" ]; then + FISH_SCRIPT_KEYBINDING="$APP_DIR/shell/key-bindings.fish" + RC_FILE="$DATA_DIR/.fish" + tee $RC_FILE > /dev/null <> $FILE_FISHRC fi else - echo "$FILE_ZSHRC does not exist." -fi \ No newline at end of file + echo "warn: $FILE_FISHRC does not exist." +fi + +echo 'Installation done.' +echo 'Restart your shell or reload config file.' \ No newline at end of file diff --git a/main.go b/main.go index 3756bc0..424e721 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "fmt" "log" "os" + "path/filepath" tea "github.com/charmbracelet/bubbletea" + "github.com/spf13/viper" "linhx.com/tbmk/bookmark" saveView "linhx.com/tbmk/views/save" searchView "linhx.com/tbmk/views/search" @@ -27,9 +29,28 @@ func NewCancellationSignal() (func(), func()) { return cancel, exit } + +func getAppDir() string { + if os.Getenv("APP_ENV") == "dev" { + return "." + } + path, err := os.Executable() + if err != nil { + panic(fmt.Errorf("Cannot get executable: %w", err)) + } + return filepath.Dir(path); +} + func main() { _, exit := NewCancellationSignal() defer exit() + viper.AddConfigPath(getAppDir()) + viper.SetConfigName("config"); + viper.SetDefault("tbmk.dataDir", "./data") + err := viper.ReadInConfig() + if err != nil { + panic(fmt.Errorf("fatal error config file: %w", err)) + } saveCmd := flag.NewFlagSet("save", flag.ExitOnError) saveCommand := saveCmd.String("command", "", "command") @@ -39,6 +60,7 @@ func main() { if len(os.Args) < 2 { fmt.Println("expected 'save' or 'search' subcommands") exit() + return } bmk, err := bookmark.NewBookmark() diff --git a/key-bindings.bash b/shell/key-bindings.bash similarity index 86% rename from key-bindings.bash rename to shell/key-bindings.bash index be80891..e7044f5 100644 --- a/key-bindings.bash +++ b/shell/key-bindings.bash @@ -3,7 +3,7 @@ __tbmk_save__() { READLINE_POINT=0 command=$(echo "${READLINE_LINE:0}" | grep -oE '(.)+$') - $TBMK save -command="$command" + tbmk save -command="$command" } __tbmk_search__() { @@ -12,7 +12,7 @@ __tbmk_search__() { local output query=$(echo "${READLINE_LINE:0}" | grep -oE '(.)+$') - output=$($TBMK search -query="$query") + output=$(tbmk search -query="$query") READLINE_LINE=${output} if [[ -z "$READLINE_POINT" ]]; then echo "$READLINE_LINE" diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish new file mode 100644 index 0000000..9682ccc --- /dev/null +++ b/shell/key-bindings.fish @@ -0,0 +1,18 @@ +status is-interactive; or exit 0 + +function __tbmk_save__ + set -l commandlineVal (commandline -t) + eval tbmk save --command="$commandlineVal" + commandline -f repaint +end + +function __tbmk_search__ + set -l commandlineVal (commandline -t) + set output (eval tbmk search --query="$commandlineVal") + commandline -t "$output" + commandline -f repaint +end + +bind \ct __tbmk_save__ + +bind -k nul __tbmk_search__ diff --git a/key-bindings.zsh b/shell/key-bindings.zsh similarity index 78% rename from key-bindings.zsh rename to shell/key-bindings.zsh index eebf38e..92f8a4a 100644 --- a/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -1,5 +1,5 @@ __tbmk_save__() { - $TBMK save -command="${LBUFFER}" + tbmk save -command="${LBUFFER}" LBUFFER="" local ret=$? zle reset-prompt @@ -10,7 +10,7 @@ zle -N __tbmk_save__ bindkey '\C-t' __tbmk_save__ __tbmk_search__() { - output=$($TBMK search -query="${LBUFFER}") + output=$(tbmk search -query="${LBUFFER}") LBUFFER="${output}" local ret=$? zle reset-prompt diff --git a/uninstall b/uninstall new file mode 100755 index 0000000..0059d0a --- /dev/null +++ b/uninstall @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +DATA_DIR="$HOME/.tbmk" + +echo "rm /usr/local/bin/tbmk" +sudo rm /usr/local/bin/tbmk + +# for bash +FILE_BASHRC=~/.bashrc +if [ -f "$FILE_BASHRC" ]; then + RC_FILE="$DATA_DIR/.bash" + SOURCE_STR=$(echo "[ -f $RC_FILE ] && source $RC_FILE" | sed 's/[][\\.*^$\-\/]/\\&/g') + sed -i "/^$SOURCE_STR/d" $FILE_BASHRC +else + echo "warn: $FILE_BASHRC does not exist." +fi + +# for zsh +FILE_ZSHRC=~/.zshrc +if [ -f "$FILE_ZSHRC" ]; then + RC_FILE="$DATA_DIR/.zsh" + SOURCE_STR=$(echo "[ -f $RC_FILE ] && source $RC_FILE" | sed 's/[][\\.*^$\-\/]/\\&/g') + sed -i "/^$SOURCE_STR/d" $FILE_ZSHRC +else + echo "warn: $FILE_ZSHRC does not exist." +fi + +# for fish +FILE_FISHRC=~/.config/fish/config.fish +if [ -f "$FILE_FISHRC" ]; then + RC_FILE="$DATA_DIR/.fish" + SOURCE_STR=$(echo "if test -f $RC_FILE; source $RC_FILE; end" | sed 's/[][\\.*^$\-\/]/\\&/g') + sed -i "/^$SOURCE_STR/d" $FILE_FISHRC +fi + +echo "uninstall done" From d7fd03e51698e574fcd1c829ce8ea1a5d447054a Mon Sep 17 00:00:00 2001 From: Nguyen Dinh Linh <50461902+linhx@users.noreply.github.com> Date: Mon, 8 Jan 2024 11:08:40 +0700 Subject: [PATCH 2/2] fix(#6): fix bug cannot run in macos due to symlink and wrong keybinding path (#9) --- install | 4 ++-- main.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/install b/install index 5407e15..fb978cd 100755 --- a/install +++ b/install @@ -32,8 +32,8 @@ if [ -f "$FILE_ZSHRC" ]; then ZSH_SCRIPT_KEYBINDING="$APP_DIR/shell/key-bindings.zsh" RC_FILE="$DATA_DIR/.zsh" tee $RC_FILE > /dev/null <