sigs.k8s.io/kubebuilder/v3@v3.14.0/scripts/demo/util.sh (about) 1 #!/bin/bash 2 # Copyright 2016 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # This file is copied from @thockin's micro-demos repository: 17 # https://github.com/thockin/micro-demos/blob/master/util.sh 18 19 readonly reset=$(tput sgr0) 20 readonly green=$(tput bold; tput setaf 2) 21 readonly yellow=$(tput bold; tput setaf 3) 22 readonly blue=$(tput bold; tput setaf 6) 23 readonly timeout=$(if [ "$(uname)" == "Darwin" ]; then echo "1"; else echo "0.1"; fi) 24 25 function desc() { 26 maybe_first_prompt 27 rate=25 28 if [ -n "$DEMO_RUN_FAST" ]; then 29 rate=1000 30 fi 31 echo "$blue# $@$reset" | pv -qL $rate 32 prompt 33 } 34 35 function prompt() { 36 echo -n "$yellow\$ $reset" 37 } 38 39 started="" 40 function maybe_first_prompt() { 41 if [ -z "$started" ]; then 42 prompt 43 started=true 44 fi 45 } 46 47 # After a `run` this variable will hold the stdout of the command that was run. 48 # If the command was interactive, this will likely be garbage. 49 DEMO_RUN_STDOUT="" 50 51 function run() { 52 maybe_first_prompt 53 rate=25 54 if [ -n "$DEMO_RUN_FAST" ]; then 55 rate=1000 56 fi 57 echo "$green$1$reset" | pv -qL $rate 58 if [ -n "$DEMO_RUN_FAST" ]; then 59 sleep 0.5 60 fi 61 OFILE="$(mktemp -t $(basename $0).XXXXXX)" 62 if [ "$(uname)" == "Darwin" ]; then 63 script -q "$OFILE" $1 64 else 65 script -eq -c "$1" -f "$OFILE" 66 fi 67 r=$? 68 read -d '' -t "${timeout}" -n 10000 # clear stdin 69 prompt 70 if [ -z "$DEMO_AUTO_RUN" ]; then 71 read -s 72 fi 73 DEMO_RUN_STDOUT="$(tail -n +2 $OFILE | sed 's/\r//g')" 74 return $r 75 } 76 77 function relative() { 78 for arg; do 79 echo "$(realpath $(dirname $(which $0)))/$arg" | sed "s|$(realpath $(pwd))|.|" 80 done 81 } 82 83 trap "echo" EXIT