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
This commit is contained in:
Sami Samhuri 2022-01-16 23:11:40 -08:00
parent 9faa33fbf2
commit 0d0bb73114
No known key found for this signature in database
GPG key ID: 4B4195422742FC16
3 changed files with 7 additions and 3 deletions

View file

@ -1,2 +1,4 @@
source 'https://rubygems.org'
ruby '3.1.0'
gem 'wordexp', '~> 0.1'

View file

@ -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

View file

@ -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)