From c87434d2c2a5a783c24f2f18ca786fa7952b879d Mon Sep 17 00:00:00 2001 From: saicharankandukuri Date: Wed, 1 Feb 2023 10:44:11 +0530 Subject: [PATCH] feat!: make pretty print default Cause it looks good :) - fetch distro data from online when --download-size - add a lil message to make user wait - fix spacing in footer - some formatting --- udroid/src/udroid.sh | 245 +++++++++++++++++-------------------------- 1 file changed, 96 insertions(+), 149 deletions(-) diff --git a/udroid/src/udroid.sh b/udroid/src/udroid.sh index e985b59..c9ed14f 100755 --- a/udroid/src/udroid.sh +++ b/udroid/src/udroid.sh @@ -736,10 +736,9 @@ parser() { ## List # list all the avalible suites varients and their status list() { - + INFO "list()" export size=false export show_installed_only=false - local show_pretty=false local show_remote_download_size=false local path=$DEFAULT_FS_INSTALL_DIR @@ -750,171 +749,119 @@ list() { --list-installed) show_installed_only=true; shift 1;; --path) path=$2; LOG "list(): looking in $path"; shift 2;; --help) help_list; exit 0;; - --pretty) show_pretty=true; shift 1;; *) shift ;; esac done - fetch_distro_data "offline" - + if ! $show_remote_download_size; then + fetch_distro_data "offline" + else + fetch_distro_data "online" + fi + suites=$(cat $distro_data | jq -r '.suites[]') + tempfile=$(mktemp udroid-list-table-XXXXXX) - if ! $show_pretty; then - # loop over suites - for suite in $suites; do - echo "$suite: " - varients=$(cat $distro_data | jq -r ".$suite.varients[]") - - # loop over varients - for varient in $varients; do - # get name - name=$(cat $distro_data | jq -r ".$suite.$varient.Name") - supported_arch=$(cat $distro_data | jq -r ".$suite.$varient.arch") - - host_arch=$(dpkg --print-architecture) - if [[ $host_arch =~ $supported_arch ]]; then - supported=true - else - supported=false - fi - - # check if installed - if [[ -d $path/$name ]]; then - _installed="[installed]" - else - _installed="" - fi - - # check size - if [[ $size == true ]]; then - if [[ -d $path/$name ]]; then - _size="[ SIZE: $(du -sh $path/$name | awk '{print $1}') ]" - else - _size="" - fi - else - _size="" - fi - - # set support status - if [[ $supported == true ]]; then - support_status="\e[1;32m [supported]\e[0m" - else - support_status="\e[31m [unsupported]\e[0m" - fi - - # print out - if ! $show_installed_only; then - echo -e "\t- $varient $support_status $_installed $_size" - else - if [[ -d $path/$name ]]; then - echo -e "\t- $varient $support_status $_installed $_size" - fi - fi - done - done + if $size; then + _size_header=" size |" + _size_line="--|" else - tempfile=$(mktemp udroid-list-table-XXXXXX) - - if $size; then - _size_header=" size |" - _size_line="--|" - else - _size_header="" - _size_line="" - fi - - if $show_remote_download_size; then - _r_size_header=" down* size |" - _r_size_line="--|" - else - _r_size_header="" - _r_size_line="" - fi - - # header - echo -e "| suites | supported | status |$_size_header$_r_size_header" > $tempfile - echo -e "|--------|-----------|--------|$_size_line"$_r_size_line >> $tempfile - - for suite in $suites; do - varients=$(cat $distro_data | jq -r ".$suite.varients[]") - - # loop over varients - for varient in $varients; do - # get name - name=$(cat $distro_data | jq -r ".$suite.$varient.Name") - supported_arch=$(cat $distro_data | jq -r ".$suite.$varient.arch") - - host_arch=$(dpkg --print-architecture) - if [[ $host_arch =~ $supported_arch ]]; then - supported=true - else - supported=false - fi - - if $supported; then - if $show_remote_download_size; then - link=$(cat $distro_data | jq -r ".$suite.$varient.${host_arch}url") - remote_size=$( wget --spider -m -np $link 2>&1 | grep -i Length | awk '{print $2}') - - if [[ -z $remote_size ]]; then - remote_size="?" - else - remote_size=$(numfmt --to=iec-i --suffix=B --padding=7 $remote_size) # <- By GitHub Copilot - fi - fi - fi - - # check if installed - if [[ -d $path/$name ]]; then - _installed="[installed]" + _size_header="" + _size_line="" + fi + + if $show_remote_download_size; then + _r_size_header=" down* size |" + _r_size_line="--|" + else + _r_size_header="" + _r_size_line="" + fi + + echo -e "reading data ( This a may take a minute ) ..." + + # header + echo -e "| suites | supported | status |$_size_header$_r_size_header" > $tempfile + echo -e "|--------|-----------|--------|$_size_line"$_r_size_line >> $tempfile + + for suite in $suites; do + varients=$(cat $distro_data | jq -r ".$suite.varients[]") + # loop over varients + for varient in $varients; do + # get name + name=$(cat $distro_data | jq -r ".$suite.$varient.Name") + supported_arch=$(cat $distro_data | jq -r ".$suite.$varient.arch") + host_arch=$(dpkg --print-architecture) + + if [[ $host_arch =~ $supported_arch ]]; then + supported=true else - _installed="" - fi + supported=false + fi - # check size - if [[ $size == true ]]; then - if [[ -d $path/$name ]]; then - _size="$(du -sh $path/$name 2> /dev/null | awk '{print $1}') |" + if $supported; then + if $show_remote_download_size; then + link=$(cat $distro_data | jq -r ".$suite.$varient.${host_arch}url") + remote_size=$( wget --spider -m -np $link 2>&1 | grep -i Length | awk '{print $2}') + if [[ -z $remote_size ]]; then + remote_size="?" else - _size="|" + remote_size=$(numfmt --to=iec-i --suffix=B --padding=7 $remote_size) # <- By GitHub Copilot fi - else - _size="" fi + fi - # set support status - if [[ $supported == true ]]; then - support_status="YES" + # check if installed + if [[ -d $path/$name ]]; then + _installed="[installed]" + else + _installed="" + fi + + # check size + if [[ $size == true ]]; then + if [[ -d $path/$name ]]; then + _size="$(du -sh $path/$name 2> /dev/null | awk '{print $1}') |" else - support_status="NO" + _size="|" fi - - # print out - if ! $show_installed_only; then + else + _size="" + fi + + # set support status + if [[ $supported == true ]]; then + support_status="YES" + else + support_status="NO" + fi + + # print out + if ! $show_installed_only; then + echo -e "|$suite:$varient|$support_status|$_installed|$_size$remote_size" >> $tempfile + else + if [[ -d $path/$name/bin ]]; then echo -e "|$suite:$varient|$support_status|$_installed|$_size$remote_size" >> $tempfile - else - if [[ -d $path/$name/bin ]]; then - echo -e "|$suite:$varient|$support_status|$_installed|$_size$remote_size" >> $tempfile - fi fi - done + fi done - # footer - { - echo "" - echo "" - echo "**SIZE**: space occupied by installed distro" - echo "**DOWN* SIZE**: download size of suite" - echo "" - echo "To install one of suite (ex: **jammy:xfce**), run:" - echo "\`\`\`bash" - echo "udroid install jammy:xfce" - echo "\`\`\`" - } >> $tempfile - - g_format $tempfile - fi + done + + # footer + { + echo "" + echo "" + echo "**SIZE**: space occupied by installed distro" + echo "**DOWN SIZE**: download size of suite" + echo "" + echo "To install a suite (ex: **jammy:raw**), run:" + echo "\`\`\`bash" + echo "udroid install jammy:raw" + echo "\`\`\`" + echo "" + } >> $tempfile + + g_format $tempfile } remove() {