From 0d0bb731147e7ce1cbd5904fe608d6b8763ebff6 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 16 Jan 2022 23:11:40 -0800 Subject: [PATCH] Use wordexp to support super fancy expansions - Expands environment variables, e.g. $HOME -> /home/tina - Expands tildes, e.g. ~/bin -> /home/queso/bin - Performs command substitution, e.g. `date +%F` -> 2022-01-16 or $(date +%F) -> 2022-01-16 --- ruby/Gemfile | 2 ++ ruby/Gemfile.lock | 2 ++ ruby/main.rb | 6 +++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ruby/Gemfile b/ruby/Gemfile index 60f3b06..f7b4b42 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,2 +1,4 @@ source 'https://rubygems.org' ruby '3.1.0' + +gem 'wordexp', '~> 0.1' diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index 395f44b..c35f4d2 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -1,11 +1,13 @@ GEM remote: https://rubygems.org/ specs: + wordexp (0.1.1) PLATFORMS arm64-darwin-21 DEPENDENCIES + wordexp (~> 0.1) RUBY VERSION ruby 3.1.0p0 diff --git a/ruby/main.rb b/ruby/main.rb index 32e8276..0e0efa0 100755 --- a/ruby/main.rb +++ b/ruby/main.rb @@ -3,7 +3,7 @@ require 'English' require 'open3' require 'readline' -require 'shellwords' +require 'wordexp' require './builtins' require './colours' @@ -92,9 +92,9 @@ class Shell exit 0 if line.nil? # EOF, ctrl-d return if line.empty? # no input, no-op - args = Shellwords.split(line) + args = Wordexp.expand(line) cmd = args.shift - logger.verbose "Words: #{cmd} #{args.inspect}" + logger.verbose "Parsed command: #{cmd} #{args.inspect}" if @builtins.builtin?(cmd) logger.verbose "Executing builtin #{cmd}" @builtins.exec(cmd, args)