2023-03-02 gum A tool for glamorous shell scripts

Calling from python

import subprocess

print("What's your favorite language?")

result = subprocess.run(["gum", "choose", "Rust", "Go", "Python"], stdout=subprocess.PIPE, text=True)

print(f"I like {result.stdout.strip()}, too!")

Example virt-install-helper : User is interactively prompted for common options thanks to Gum.

Voir GNU-Linux Pratique N° 136, p.8 à 19

 1#!/usr/bin/env bash
 2
 3# This script is a virt-install helper. # It prompts user for
 4# the most common options to create and launch a vm.
 5#
 6# By default file explorer opens ~/Documents to search for
 7# a iso file. You can use ISODIR variable at the command line
 8# to choose a different directory, e.g.:
 9#
10# ISODIR="/tmp" ./virtinstall-helper.sh
11
12set -euo pipefail
13
14ISODIR=${ISODIR:-${HOME}/Documents}
15
16gum style \
17  --foreground 99 \
18  --border double \
19  --border-foreground 99 \
20  --padding "1 2" \
21  --margin 1 "virt-install Helper"
22
23sleep 1
24clear
25
26NAME=$(gum input --prompt "VM Name: " --placeholder "" \
27  | tr -d '[:space:][:blank:]')
28clear
29
30OS=$(osinfo-query --fields=short-id os \
31  | tail -n +3 \
32  | gum filter --placeholder="Choose OS" \
33  | tr -d '[:space:][:blank:]')
34clear
35
36echo "Number of vcpus: "
37VCPUS=$(seq 1 6 | gum choose)
38clear
39
40echo "Memory:"
41MEM=$(gum choose --selected=1024 256 512 1024 2048 4096)
42clear
43
44echo "SIZE (Go): "
45SIZE=$(gum choose --selected=10 5 10 20 30)
46clear
47
48ISO=$(gum file "$ISODIR")
49
50display_variable() {
51  echo "$(gum style --foreground 99 "$1:")" "$2"
52}
53
54display_variable NAME "$NAME"
55display_variable VCPUS "$VCPUS"
56display_variable MEM "$MEM"
57display_variable SIZE "$SIZE"
58display_variable ISO "$ISO"
59
60echo ""
61gum confirm "Create and Launch VM?" || exit
62
63gum spin --title "Creating and launching VM in virt-viewer..." -- virt-install \
64  --name="$NAME" \
65  --os-variant="$OS" \
66  --vcpus="$VCPUS" \
67  --memory="$MEM" \
68  --disk size="$SIZE" \
69  --cdrom="$ISO"
70