Skip to content
Snippets Groups Projects
Verified Commit b66fedd2 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

wip

parent 72e9e686
Branches tmux-sessionizer
No related tags found
No related merge requests found
def lpad [width: int, --character: string = " "]: [ string -> string ] { fill --alignment right --width $width --character $character }
def rpad [width: int, --character: string = " "]: [ string -> string ] { fill --alignment left --width $width --character $character }
def "str color" [color: string]: [ string -> string ] { $"(ansi $color)($in)(ansi reset)" }
const DEFAULT_FINDER = "~/documents/bin/find-git-repos-e96486f52751828ff5f8ebdfadacbd37cdb802afa559d098bbd59cac4df8b46d" | path expand
const SESSION_FILE = $nu.home-path | path join ".local" "state" "tmux" "last-session"
const LOG_FILE = "/tmp/log.txt"
def log [msg: string] { $"($msg)" out>> $LOG_FILE }
def logln [msg: string] { $"($msg)\n" out>> $LOG_FILE }
def get [
root : path,
--finder (-f) : string = $DEFAULT_FINDER,
--no-ansi (-A),
--sort (-s),
]: [
nothing -> record<
root : path,
host : string,
group : string,
project : string,
path : path,
>
] {
if not ($finder | path exists) {
error make {
msg: $"(ansi red_bold)unknown_finder(ansi reset)",
label: {
text: $"(ansi purple)($finder)(ansi reset): no such file",
span: (metadata $finder).span,
},
}
}
let finder = $finder | path expand
let repos = ^$finder $root | lines | if $sort { sort } else { $in } | each {
str replace --regex $"^($root)/" ''
| path split
| {
host: $in.0,
group: ($in | skip 1 | reverse | skip 1 | reverse | str join "/"),
project: ($in | last),
}
}
let width = {
host: ($repos.host | str length | math max),
group: ($repos.group | str length | math max),
project: ($repos.project | str length | math max),
}
try {
$repos
| insert display {
update host { rpad $width.host | if $no_ansi { $in } else { str color "cyan_dimmed" } }
| update group { rpad $width.group | if $no_ansi { $in } else { str color "default_dimmed" } }
| update project { rpad $width.project | if $no_ansi { $in } else { str color "yellow" } }
| $"($in.host) ($in.group) ($in.project)"
}
| input list --fuzzy --display display
| reject display
| insert root $root
| select root host group project
| insert path { values | path join }
} catch {
null
}
}
def get-current-session []: [ nothing -> string ] {
^tmux display-message -p '#{session_name}' | str trim
}
def get-previous-session []: [ nothing -> string ] {
$SESSION_FILE | if ($in | path exists) { open $in | str trim } else { "" }
}
def list-sessions []: [ nothing -> list<string> ] {
^tmux list-sessions -F "#{session_name}" | lines
}
def save-session [current: string] {
log $"save-session `($current)`"
mkdir ($SESSION_FILE | path dirname)
let previous = get-previous-session
if $previous != $current {
logln " (saved)"
$current | save --force $SESSION_FILE
} else {
logln ""
}
}
def switch-to-session [name: string] {
logln $"switch-to-session `($name)`"
^tmux switch-client -t $name
}
def create-session [name: string, --path: path] {
logln $"create-session `($name)`"
^tmux new-session -ds $name -c $path
}
def switch-to-or-create-session [name: string, --path: path = $nu.home-path] {
logln $"switch-to-or-create-session `($name)`"
if $name not-in (list-sessions) {
create-session $name --path $path
}
switch-to-session $name
}
def alternate-session [] {
let current = get-current-session
let previous = get-previous-session
logln "alternate-session"
logln $" previous : ($previous)"
logln $" current : ($current)"
if $previous in (list-sessions) {
save-session $current
switch-to-session $previous
}
}
def "path shorten" []: [ string -> string ] {
path split
| each {
if ($in | str starts-with '.') {
str substring ..1
} else {
str substring ..0
}
}
| path join
}
......@@ -25,7 +25,7 @@ bind-key -T copy-mode-vi t send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'wl-copy'
# sessionizer
TMUX_SESSIONIZER="tmux-sessionizer.nu"
SESSIONIZER="~/.config/tmux/sessionizer.nu"
FIND_GIT_REPOS_DIR="~/documents/bin"
FIND_GIT_REPOS_SHA="e96486f52751828ff5f8ebdfadacbd37cdb802afa559d098bbd59cac4df8b46d"
FIND_GIT_REPOS_BIN="$FIND_GIT_REPOS_DIR/find-git-repos-$FIND_GIT_REPOS_SHA"
......@@ -38,20 +38,32 @@ TMUX_SESSIONIZER_KEY="a"
TMUX_SESSIONIZER_PREFIX="prefix_$TMUX_SESSIONIZER_KEY"
bind-key -T prefix $TMUX_SESSIONIZER_KEY switch-client -T $TMUX_SESSIONIZER_PREFIX
CTX="nu --env-config ~/.config/nushell/env.nu --commands '$TMUX_SESSIONIZER"
TSP=$TMUX_SESSIONIZER_PREFIX
bind-key -r -T $TSP h display-popup -EE -w $W -h $H -y 0 "$CTX ...($FIND_GIT_REPOS_BIN ~/documents/repos/ | lines) --short'"
bind-key -r -T $TSP n run-shell "$CTX new-session'"
bind-key -r -T $TSP s display-popup -EE -w $W -h $H -y 0 "$CTX switch-session'"
bind-key -r -T $TSP r display-popup -EE -w $W -h $H -y 0 "$CTX remove-sessions'"
bind-key -r -T $TSP % run-shell "$CTX alternate'"
bind-key -r -T $TSP e display-popup -EE -w $W -h $H -y 0 "$CTX harpoon edit'"
bind-key -r -T $TSP j display-popup -EE -w $W -h $H -y 0 "$CTX harpoon entries'"
bind-key -r -T $TSP a run-shell "$CTX harpoon add'"
bind-key -r -T $TSP 1 run-shell "$CTX harpoon jump 0'"
bind-key -r -T $TSP 2 run-shell "$CTX harpoon jump 1'"
bind-key -r -T $TSP 3 run-shell "$CTX harpoon jump 2'"
bind-key -r -T $TSP 4 run-shell "$CTX harpoon jump 3'"
bind-key -r -T $TSP h display-popup -EE -w $W -h $H -y 0 "nu --commands 'source $SESSIONIZER; \
const UCOLON = char -u \"2236\"; \
const UDOT = char -u \"2024\"; \
\
let res = get -sA ~/documents/repos/ --finder $FIND_GIT_REPOS_BIN; \
if \$res != null { \
let name = \$\"(\$res.host)(\$UCOLON)(\$res.group)/(\$res.project)\" \
| str replace --all \".\" \$UDOT; \
let current = get-current-session; \
logln \"\"; \
logln \$\"SWITCH from (\$current) to (\$name)\"; \
if \$current != \$name { \
save-session \$current; \
}; \
switch-to-or-create-session \$name --path \$res.path; \
} \
'"
# bind-key -r -T $TSP n run-shell "$CTX new-session'"
# bind-key -r -T $TSP s display-popup -EE -w $W -h $H -y 0 "$CTX switch-session'"
# bind-key -r -T $TSP r display-popup -EE -w $W -h $H -y 0 "$CTX remove-sessions'"
bind-key -r -T $TSP % run-shell "nu --commands 'source $SESSIONIZER; \
logln \"\"; \
logln \"ALTERNATE\"; \
alternate-session; \
'"
# from https://github.com/codingjerk/dotfiles/blob/main/config/tmux/tmux.conf
# Theme: borders
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment