From 7b23da80d2e2a78deeeea6b9e93a7229db484932 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/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 | 76 +++++++++++++++++++++++ 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 + 13 files changed, 98 insertions(+), 64 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 40e1ce0..88b753a 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/program/bash/module/Archive.nix b/home/program/bash/module/Archive.nix index ca755ab..6e8606e 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 62681a8..deade89 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 ecf1b12..56b56c0 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 0000000..171bda5 --- /dev/null +++ b/home/program/bash/module/Bottle.nix @@ -0,0 +1,76 @@ +{ ... }: { + 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}" + } + + # 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 + } + + # List programs in a bottle. + function btlp() { + bottles-cli programs -b "''${SHELL_NAME}" + } + + # 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}" + } + + # 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 "''${@}" + } + + 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 + complete -F _comp_programs_list btr + ''; +} diff --git a/home/program/bash/module/Cd.nix b/home/program/bash/module/Cd.nix index e390d8b..c02f369 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 b6b2de8..231b5a6 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 1949c92..c11dfcd 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 130581d..749caaf 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 067d9c2..3be790f 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 26670a8..c4f1193 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 39aa94f..a84cae1 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 b02eb14..568e755 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. ]; }; };