From a02dd3f6959d4d6ded9d340f90aea045f05eba4b Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Thu, 12 Dec 2024 05:27:20 +0300 Subject: [PATCH] Bottles: Add cli. --- home/file/ssh/default.nix | 1 + home/file/yazi/module/Yazi.nix | 7 +- home/program/bash/module/Archive.nix | 2 +- home/program/bash/module/Ask.nix | 2 +- home/program/bash/module/Autocomplete.nix | 64 +++------------- home/program/bash/module/Bottle.nix | 89 +++++++++++++++++++++++ home/program/bash/module/Cd.nix | 2 +- home/program/bash/module/Git.nix | 2 +- home/program/bash/module/Help.nix | 2 +- home/program/bash/module/Name.nix | 2 +- home/program/bash/module/Own.nix | 2 +- home/program/bash/module/Su.nix | 2 +- home/program/bash/module/Tmux.nix | 4 +- host/x86_64-linux/home/Network.nix | 1 + 14 files changed, 116 insertions(+), 66 deletions(-) create mode 100644 home/program/bash/module/Bottle.nix diff --git a/home/file/ssh/default.nix b/home/file/ssh/default.nix index 40e1ce05..88b753a8 100644 --- a/home/file/ssh/default.nix +++ b/home/file/ssh/default.nix @@ -20,6 +20,7 @@ in { + mkHost "fmpmaven" "10.30.22.10" 22 "root" + mkHost "home" "10.0.0.1" 22143 "root" + mkHost "laptop" "192.168.1.9" 22143 "root" + + mkHost "max" "10.0.0.13" 22143 "root" + mkHost "nixbuilder" "10.0.0.1" 22143 "nixbuilder" + mkHost "pi" "192.168.1.6" 22143 "root" + mkHost "pocket" "192.168.1.11" 22143 "root" diff --git a/home/file/yazi/module/Yazi.nix b/home/file/yazi/module/Yazi.nix index 3bccaf5a..61dbfdf8 100644 --- a/home/file/yazi/module/Yazi.nix +++ b/home/file/yazi/module/Yazi.nix @@ -100,6 +100,10 @@ desc = "Run"; run = openWith "steam-run"; }]; + bottle_run = [{ + desc = "Run bottle"; + run = openWith "btp"; + }]; unlock = [{ desc = "Unlock"; block = true; @@ -125,7 +129,6 @@ rules = let defaultUse = [ "text" - "archive" "archive_fast" "hex" ]; @@ -157,8 +160,8 @@ (mkMime "text/html" [ "browser" ]) (mkMime "application/vnd.openxmlformats-officedocument.*" [ "document" ]) (mkName "*.xlsx" [ "document" ]) - (mkMime "inode/directory" [ "audio_shuffle" ]) (mkMime "application/x-executable" [ "steam_run" ]) + { mime = "inode/directory"; use = [ "archive" "bottle_run" "audio_shuffle" ]; } (mkMime "*" [ ]) ]; }; diff --git a/home/program/bash/module/Archive.nix b/home/program/bash/module/Archive.nix index ca755ab7..6e8606eb 100644 --- a/home/program/bash/module/Archive.nix +++ b/home/program/bash/module/Archive.nix @@ -357,7 +357,7 @@ # Autocomplete with names of all archives. function _comp_archive_names() { - _autocomplete_first $(_archive_names) + _autocomplete $(_archive_names) } # Check if file is an archive. diff --git a/home/program/bash/module/Ask.nix b/home/program/bash/module/Ask.nix index 62681a8c..deade895 100644 --- a/home/program/bash/module/Ask.nix +++ b/home/program/bash/module/Ask.nix @@ -19,7 +19,7 @@ function _complete_ask_model() { local IFS=$'\n' local models=($(ollama list | sed -e "1d" | cut -f1)) - _autocomplete_first ''${models[@]} + _autocomplete ''${models[@]} } complete -F _complete_ask_model ask_model diff --git a/home/program/bash/module/Autocomplete.nix b/home/program/bash/module/Autocomplete.nix index ecf1b122..56b56c06 100644 --- a/home/program/bash/module/Autocomplete.nix +++ b/home/program/bash/module/Autocomplete.nix @@ -4,54 +4,14 @@ # There are also options like -o nospace. see man for more info. # Usage: _foo() { _autocomplete "{foo,bar}" } ; complete -F _foo foo function _autocomplete() { - local IFS=$'\n' - local commands="''${*}" - - COMPREPLY=() - - local cur="''${COMP_WORDS[COMP_CWORD]}" - local prev="''${COMP_WORDS[COMP_CWORD-1]}" - local command="''${COMP_WORDS[0]}" - - COMPREPLY=( $(compgen -W "''${commands}" -- ''${cur}) ) - return 0 - } - - # Autocomplete only first argument. - function _autocomplete_first() { - local IFS=$'\n' - local commands="''${*}" - - COMPREPLY=() - - local cur="''${COMP_WORDS[COMP_CWORD]}" - local prev="''${COMP_WORDS[COMP_CWORD-1]}" - local command="''${COMP_WORDS[0]}" - - if [[ "''${prev}" = "''${command}" ]]; then - COMPREPLY=( $(compgen -W "''${commands}" -- ''${cur}) ) - return 0 - fi - } - - # Autocomplete only first argument and the rest with files. - function _autocomplete_first_ls() { - local IFS=$'\n' - local commands="''${*}" - - COMPREPLY=() - - local cur="''${COMP_WORDS[COMP_CWORD]}" - local prev="''${COMP_WORDS[COMP_CWORD-1]}" - local command="''${COMP_WORDS[0]}" - - if [[ "''${prev}" = "''${command}" ]]; then - COMPREPLY=( $(compgen -W "''${commands}" -- ''${cur}) ) - return 0 - else - COMPREPLY=( $(compgen -W "$(ls)" -- ''${cur}) ) - return 0 - fi + local iter use cur + cur=''${COMP_WORDS[COMP_CWORD]} + use="''${@//\\ /___}" + for iter in $use; do + if [[ $iter =~ ^$cur ]]; then + COMPREPLY+=( $(printf "%q" "''${iter//___/ }") ) + fi + done } # Autocomplete by grepping file names. @@ -60,12 +20,8 @@ COMPREPLY=() local pattern="''${1}" - local cur="''${COMP_WORDS[COMP_CWORD]}" - local prev="''${COMP_WORDS[COMP_CWORD-1]}" - local command="''${COMP_WORDS[0]}" - - COMPREPLY=( $(compgen -W "$(ls | grep -E ''${pattern})" -- ''${cur}) ) - return 0 + local candidates=$("$(ls | grep -E ''${pattern})") + _autocomplete ''${candidates} } # Autocomplete nested program. diff --git a/home/program/bash/module/Bottle.nix b/home/program/bash/module/Bottle.nix new file mode 100644 index 00000000..4d2f3b21 --- /dev/null +++ b/home/program/bash/module/Bottle.nix @@ -0,0 +1,89 @@ +{ ... }: { + text = '' + # Switch bottle. + # Usage: bt + function bt() { + export SHELL_NAME="''${*}" + } + + # Create new bottle. + # Usage: btc [ENV] [EXTRA] + function btc() { + local env="''${1}" + [[ "''${env}" = "" ]] && env="gaming" + bottles-cli new --bottle-name "''${SHELL_NAME}" --environment "''${env}" "''${@:2}" 2> /dev/null + } + + # Run a file inside a bottle. + # Usage: btre [EXTRA] + function btre() { + bottles-cli run -b "''${SHELL_NAME}" -e "''${@}" + } + + # Run a program inside a bottle. + # Usage: btr [EXTRA] + function btr() { + bottles-cli run -b "''${SHELL_NAME}" -p "''${@}" + } + + # List bottles. + function btl() { + bottles-cli list bottles 2> /dev/null + } + + # List programs in a bottle. + function btlp() { + bottles-cli programs -b "''${SHELL_NAME}" 2> /dev/null + } + + # Add a program to bottle. + # Usage: bta [EXTRA] + function bta() { + local name="''${1}" + local exe=$(realpath "''${2}") + if [[ "''${exe}" = "" ]]; then + help bta + return 2 + fi + + bottles-cli add -b "''${SHELL_NAME}" -n "''${name}" -p "''${exe}" "''${@:3}" 2> /dev/null + } + + # Set bottle env var. + # Usage: bte + function bte() { + local env="''${1}" + if [[ "''${env}" = "" ]]; then + help bte + return 2 + fi + + bottles-cli edit -b "''${SHELL_NAME}" --env-var "''${@}" 2> /dev/null + } + + # Play bottle. + # Usage: btp + function btp() { + local bottle="''${1##*/}" + if [[ "''${bottle}" = "" ]]; then + help btp + return 2 + fi + + local program=$(bottles-cli programs -b "''${bottle}" 2> /dev/null | sed -n -e "s/^- //; 2p") + bottles-cli run -b "''${bottle}" -p "''${program}" + } + + function _comp_bottles_list() { + _autocomplete $(bottles-cli list bottles 2> /dev/null | sed -e "1d; s/^- //") + } + + function _comp_programs_list() { + local IFS=$'\n' + _autocomplete $(bottles-cli programs -b "''${SHELL_NAME}" 2> /dev/null | sed -e "1d; s/^- //") + } + + complete -F _comp_bottles_list bt btp + complete -F _comp_programs_list btr + ''; +} diff --git a/home/program/bash/module/Cd.nix b/home/program/bash/module/Cd.nix index e390d8bb..c02f3699 100644 --- a/home/program/bash/module/Cd.nix +++ b/home/program/bash/module/Cd.nix @@ -65,7 +65,7 @@ function _comp_cdd() { local IFS=$'\n' local dirs=($(_cdd_directories)) - _autocomplete_first ''${dirs[@]} + _autocomplete ''${dirs[@]} } complete -o nosort -o filenames -F _comp_cdd cdd diff --git a/home/program/bash/module/Git.nix b/home/program/bash/module/Git.nix index b6b2de81..231b5a69 100644 --- a/home/program/bash/module/Git.nix +++ b/home/program/bash/module/Git.nix @@ -329,7 +329,7 @@ # Autocomplete with my git emails. function _gu() { - _autocomplete_first hi@voronind.com dd.voronin@fsight.ru + _autocomplete hi@voronind.com dd.voronin@fsight.ru } complete -F _gu gu diff --git a/home/program/bash/module/Help.nix b/home/program/bash/module/Help.nix index 1949c923..c11dfcda 100644 --- a/home/program/bash/module/Help.nix +++ b/home/program/bash/module/Help.nix @@ -21,7 +21,7 @@ # Autocomplete with available functions. function _help_functions() { - _autocomplete_first $(find_function) + _autocomplete $(find_function) } complete -F _help_functions help h diff --git a/home/program/bash/module/Name.nix b/home/program/bash/module/Name.nix index 130581d7..749caaf8 100644 --- a/home/program/bash/module/Name.nix +++ b/home/program/bash/module/Name.nix @@ -391,7 +391,7 @@ } function _comp_name_parse() { - _autocomplete_first_ls $(find_function | grep ^parse) + _autocomplete $(find_function | grep ^parse) } complete -o filenames -F _comp_name_parse name_parse diff --git a/home/program/bash/module/Own.nix b/home/program/bash/module/Own.nix index 067d9c29..3be790ff 100644 --- a/home/program/bash/module/Own.nix +++ b/home/program/bash/module/Own.nix @@ -32,7 +32,7 @@ } function _complete_own() { - _autocomplete_first_ls $(_get_users) + _autocomplete $(_get_users) } complete -F _complete_own own diff --git a/home/program/bash/module/Su.nix b/home/program/bash/module/Su.nix index 26670a89..c4f1193e 100644 --- a/home/program/bash/module/Su.nix +++ b/home/program/bash/module/Su.nix @@ -25,7 +25,7 @@ } function _complete_s() { - _autocomplete_first $(_get_users) + _autocomplete $(_get_users) } complete -F _complete_s s diff --git a/home/program/bash/module/Tmux.nix b/home/program/bash/module/Tmux.nix index 39aa94f5..a84cae11 100644 --- a/home/program/bash/module/Tmux.nix +++ b/home/program/bash/module/Tmux.nix @@ -78,7 +78,7 @@ # Autocomplete with running sessions once. function _complete_tmux_session() { - _autocomplete_first "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')" + _autocomplete "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')" } # Autocomplete with running sessions. @@ -88,7 +88,7 @@ # Autocomplete with current dir name and dirs inside this one. function _complete_tmux_name() { - _autocomplete_first "''${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//') + _autocomplete "''${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//') } complete -F _complete_tmux_session ta diff --git a/host/x86_64-linux/home/Network.nix b/host/x86_64-linux/home/Network.nix index b02eb14c..568e7559 100644 --- a/host/x86_64-linux/home/Network.nix +++ b/host/x86_64-linux/home/Network.nix @@ -131,6 +131,7 @@ in { (mkStatic "10.0.0.10" "9c:1c:37:62:3f:d5") # Printer. (mkStatic "10.0.0.11" "dc:a6:32:f5:77:95") # RPi. (mkStatic "10.0.0.12" "ec:9c:32:ad:bc:4a") # Camera. + (mkStatic "10.0.0.13" "c0:a5:e8:b5:d9:16") # Max. ]; }; };