From a6a02289f08e746ffc22685524f5a7fe3b6a22ea Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Thu, 21 Jul 2022 17:49:47 -0400 Subject: [PATCH] docs: show how to embed `gum` in python and ruby scripts --- examples/gum.py | 7 +++++++ examples/gum.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 examples/gum.py create mode 100644 examples/gum.rb diff --git a/examples/gum.py b/examples/gum.py new file mode 100644 index 0000000..5c20520 --- /dev/null +++ b/examples/gum.py @@ -0,0 +1,7 @@ +import subprocess + +print("What's your favorite language?") + +result = subprocess.run(["gum", "choose", "Go", "Python"], stdout=subprocess.PIPE, text=True) + +print(f"I like {result.stdout.strip()}, too!") diff --git a/examples/gum.rb b/examples/gum.rb new file mode 100644 index 0000000..6b241fc --- /dev/null +++ b/examples/gum.rb @@ -0,0 +1,29 @@ +puts 'What is your name?' +name = `gum input --placeholder "Your name"`.chomp + +puts "Hello #{name}!" + +puts 'Pick your 2 favorite colors' + +COLORS = { + 'Red' => '#FF0000', + 'Blue' => '#0000FF', + 'Green' => '#00FF00', + 'Yellow' => '#FFFF00', + 'Orange' => '#FFA500', + 'Purple' => '#800080', + 'Pink' => '#FF00FF' +}.freeze + +colors = `gum choose #{COLORS.keys.join(' ')} --limit 2`.chomp.split("\n") + +if colors.length == 2 + first = `gum style --foreground '#{COLORS[colors[0]]}' '#{colors[0]}'`.chomp + second = `gum style --foreground '#{COLORS[colors[1]]}' '#{colors[1]}'`.chomp + puts "You chose #{first} and #{second}." +elsif colors.length == 1 + first = `gum style --foreground '#{COLORS[colors[0]]}' '#{colors[0]}'`.chomp + puts "You chose #{first}." +else + puts "You didn't pick any colors!" +end