You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB
Plaintext

#!/bin/env nu
use task.nu
use dirs.nu
const ZELLIJ_LAYOUT = ($dirs.CONFIG | path join zellij/development.toml)
export def --env main [] {
if $env.ZELLIJ? == null {
if $env.WEZTERM_PANE? != null {
let name = ($env.PWD | path basename)
wezterm cli set-tab-title $"IDE ($name)"
}
let tmpdir = init
zellij --layout $ZELLIJ_LAYOUT --session (session-name)
rm -r $tmpdir
if $env.WEZTERM_PANE? != null {
wezterm cli set-tab-title ""
}
} else {
zellij action new-tab --layout $ZELLIJ_LAYOUT --name (tab-name)
}
}
export def open-file [path: string] {
if ($path | str starts-with "http") {
/usr/bin/xdg-open $path
return
}
match ($path | path parse | get extension | str downcase) {
"png" => { open-image $path }
"jpg" => { open-image $path }
"jpeg" => { open-image $path }
"webp" => { open-image $path }
_ => { open-editor $path }
}
}
export def floating-prompt [] {
task spawn -i {
zellij action new-pane --floating -- nu -c 'nu -e clear; zellij action close-pane';
}
zellij action close-pane
}
export def last-notification [] {
let notif = open $env.NOTIFY_FILE | str trim
if ($notif | str length) == 0 {
return
}
$env.NTFY_CHKSUM = (shasum $env.NOTIFY_FILE)
if not (task group exists "notify") {
task group add notify -p 4
}
task spawn -d 5sec -g notify {
if (shasum $env.NOTIFY_FILE) == $env.NTFY_CHKSUM {
"" | save -f $env.NOTIFY_FILE
}
}
$notif
}
def open-editor [path: string] {
zellij ac move-focus right
zellij ac write-chars $":open ($path)\r"
}
def open-image [path: string] {
zellij ac new-pane -f -- chafa -C on --scale max $path
}
def session-name [] {
let dirname = $env.PWD | path parse | get stem
let suffix = ( zellij list-sessions -s
| split row "\n"
| where $it =~ $"ide-($dirname)"
| get 0?
| default ""
| hash md5
| str substring 0..4
)
$"ide-($dirname)-($suffix)"
}
def tab-name [] {
let dirname = $env.PWD | path parse | get stem
$"Editor \(($dirname)\)"
}
def --env init [] {
let tmpdir = mktemp -d --suffix "-ide"
let tmpbin = ($tmpdir | path join "bin")
mkdir $tmpbin
$env.PATH = ($env.PATH | prepend $tmpbin)
create-xdg-open $tmpbin
create-notify-send $tmpdir $tmpbin
$tmpdir
}
def create-xdg-open [tmpbin: string] {
let new_open = ($tmpbin | path join "xdg-open")
("#!/bin/env nu
use ide.nu
def main [file: string] {
ide open-file $file
}
" | save -f $new_open)
chmod +x $new_open
}
def --env create-notify-send [tmpdir: string, tmpbin: string] {
$env.NOTIFY_FILE = ($tmpdir | path join "notifications.txt")
touch $env.NOTIFY_FILE
let new_ntfy = ($tmpbin | path join "notify-send")
("#!/bin/env nu
def main [title: string, content: string] {
$\"(ansi {attr: bi})($title)(ansi reset) ($content)\\n\" | save -f $env.NOTIFY_FILE
}
" | save -f $new_ntfy)
chmod +x $new_ntfy
}