Skip to content

FEATURE: allow to pass paths from outside without searching in them #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ in my `config.nu` file, i've added the following binding under `$env.config.keyb
mode: [emacs, vi_insert, vi_normal]
event: {
send: executehostcommand
cmd: "zellij-sessionizer.nu $env.GIT_REPOS_HOME"
cmd: "zellij-sessionizer.nu [--depth/-d] ...paths"
}
}
```
given that
- `zellij-sessionizer.nu` is in your `$env.PATH`,
- `$env.GIT_REPOS_HOME` is some variable defined in `env.nu` that points to a
place with lots of projects,
given that `zellij-sessionizer.nu` is in your `$env.PATH`.

here the `...` represents any set of arguments and options, e.g.
- a command that lists all the paths directly: `gm list --full-path`
- a more complex one: `[~/.local/share/repos/ ~/documents/] | each { ls $in | where type == dir | get name } | flatten`
- or let `zellij-sessionizer.nu` handle the search at some depth: `~/.local/share/repos/ ~/documents/ --depth 1`

this binding will run the sessionizer simply by pressing `<C-f>` :ok_hand:

Expand Down
56 changes: 44 additions & 12 deletions zellij-sessionizer.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,57 @@ def is-inside-zellij [] {

# attach to a Zellij session by fuzzy finding projects
#
# the projects are computed by listing all the directories at depth between 1
# and 2 recursively under the `path` argument.
# # Examples
# opening a session by listing `nu-git-manager` repositories
# > zellij-sessionizer.nu (gm list --full-path)
#
# open a session in local repos and documents
# > zellij-sessionizer.nu (
# > [~/.local/share/repos/ ~/documents/] | each { ls $in | where type == dir | get name } | flatten
# > )
Comment on lines +13 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Sorry for my absence!

isn't it the same as:

zellij-sessionizer $( fd . ~/Documents --type d) # Then it just gets piped into fzf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Sorry for my absence!

hey 👋
no worries 😉

isn't it the same as:

zellij-sessionizer $( fd . ~/Documents --type d) # Then it just gets piped into fzf?

yeah it looks about the same 👍
i'm just using pure Nushell commands but they are looking for depth-1 directories in all the directories in the list to the left 😋

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!
So I'd probably add it as part of the alias (we can update the readme for it) instead of pushing it into the code as it feels like something some users would like where others would defer to an external app (such as fd)

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! So I'd probably add it as part of the alias (we can update the readme for it) instead of pushing it into the code as it feels like something some users would like where others would defer to an external app (such as fd)

WDYT?

not sure i see what you mean here by "add it as part of the alias instead of pushing it into the code" 😕

and you're talking about the Bash implementation, right?

Copy link
Owner

@silicakes silicakes Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add it as part of the alias instead of pushing it into the code

I mean that I think it should be part of the users config - like a parameter passed to their .rc aliases for zellij-sessionizer

I'm talking about both implementations as I try to maintain a single-responsibility philosophy as much as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i see, does that mean that you'd like the script not mentioning find or fd directly?

i might miss something here 😱 😆

Copy link
Owner

@silicakes silicakes Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!
give it some sane defaults and let the user decide about everything that's under the title of 'customization'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh yeah, i see what you mean now 😌

i'd say, as find and fd were already there before this PR, we can leave this as-is for now and tackle the removal of these commands and making them configurable in another PR? 😋

Copy link
Owner

@silicakes silicakes Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the solution I was thinking about is sort of a flat way of passing params into your find cmd, without explicitly mentioning it within the code.

Assuming you're using fd - I envision something like

$ zellij_sessionizer [paths] -- ...rest_of_fds options

Then the script will just pass it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm we can't do that really in Nushell right now 😕

#
# open a session in local repos and documents (same as above)
# > zellij-sessionizer.nu ~/.local/share/repos/ ~/documents/ --depth 1
def main [
path: path # the directory to search projects inside
...paths: path # the list of paths to fuzzy find
--depth (-d): int = 0 # when greater or equal to 1, searches all the paths at the given depth
] {
let project = (if (which fd | is-empty) {
^find $path -mindepth 1 -maxdepth 2 -type d
} else {
^fd . $path --min-depth 1 --max-depth 2 --type d
} | fzf)
if ($paths | is-empty) {
error make --unspanned {msg: "no path given"}
}

if ($project | is-empty) {
let choices = (
$paths | if $depth > 0 { each {|path|
if (which fd | is-empty) {
^find $path -mindepth ($depth - 1) -maxdepth $depth -type d | lines
} else {
^fd . $path --min-depth ($depth - 1) --max-depth $depth --type d | lines
}
} | flatten } else {} | each {
let tokens = ($in | path split)

{
project: ($tokens | last)
path: ($tokens | drop 1 | path join)
}
}
)

let choice = (
$choices.project | input list --fuzzy
$"Please (ansi red)choose a directory(ansi reset) to (ansi cyan)attach to(ansi reset): "
)
if ($choice | is-empty) {
return
}

let session = ($project | path basename)
let choice = ($choices | where project == $choice | get 0)
let directory = ($choice.path | path join $choice.project)

let session = ($directory | path basename)

if not (is-inside-zellij) {
cd $project
cd $directory
zellij attach --create $session options --default-shell nu
return
}
Expand All @@ -34,6 +66,6 @@ def main [
# Hopefully they'll someday support specifying a directory and this won't be
# as laggy thanks to @msirringhaus for getting this from the community some
# time ago!
zellij action write-chars $"cd ($project)"
zellij action write-chars $"cd ($directory)"
zellij action write 10
}