Skip to content

Commit

Permalink
- Allow smart-units to be used as a library.
Browse files Browse the repository at this point in the history
Example:

	include("smart-units.sf")
	var r = process_input("120 F to C")
	say "result = #{r}"

- Allow `lb` to mean `lbs`.
  • Loading branch information
trizen committed Mar 11, 2020
1 parent 9253226 commit 8b72ac7
Showing 1 changed file with 71 additions and 53 deletions.
124 changes: 71 additions & 53 deletions smart-units.sf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
define VERSION = 0.11

func more_help {
print <<"HELP"
<<"HELP"
Mass:
42 kg to lbs : convert kilograms to pounds
30 t to grams : convert tonnes to grams
Expand Down Expand Up @@ -63,7 +63,8 @@ HELP
}

class Units {
static conversions = :(

has conversions = :(
mass => :( # all relative to 1 kilogram
nanograms => :(re => /^(ng|nanograms?)$/in, factor => 1e-12),
micrograms => :(re => /^(μg|micrograms?)$/in, factor => 1e-9),
Expand All @@ -87,7 +88,7 @@ class Units {
hectolitres => :(re => /^(hl|hectolit(re|er)s?)$/in, factor => 1e2),
kilolitres => :(re => /^(kl|kilolit(re|er)s?)$/in, factor => 1e3),

pounds => :(re => /^(pounds?|lbs)$/in, factor => 0.45359237),
pounds => :(re => /^(pounds?|lbs?)$/in, factor => 0.45359237),
stones => :(re => /^(stones?|st)$/in, factor => 6.35029318),
gallons => :(re => /^(gall?ons?|gall?s?)$/in, factor => 3.78541),
),
Expand Down Expand Up @@ -225,7 +226,7 @@ var precision = 10
var interactive = false

func help {
print <<"USAGE"
<<"USAGE"
usage: #{File.new(__FILE__).basename} [text]

options:
Expand All @@ -238,36 +239,16 @@ examples:
whats the equivalent of 21 inches in meters
what is the speed of light in miles per hour
USAGE
Sys.exit(0)
}

var cmd = [];
while (!ARGV.is_empty) {
given (var arg = ARGV.shift) {
when ('-p') {
precision = Num(ARGV.shift)
}
when ('-i') {
interactive = true
}
when ('-h') {
help()
}
default {
cmd << arg
}
}
}

if (cmd.is_empty && STDIN.is_on_tty) {
interactive = true
}

func round_result (value, precision) {
value.is_int ? value : value.round(-precision)
}

func process_input (command) {

static units = Units()

var match = command.match(/^\s*

# command or question
Expand Down Expand Up @@ -296,8 +277,6 @@ func process_input (command) {
amount = Number(amount)
}

var units = Units()

var m1 = (from =~ %r{(.+?)/(.+)})
var m2 = (to =~ %r{(.+?)/(.+)})

Expand All @@ -318,46 +297,85 @@ func process_input (command) {
var time1 = time_aliases.fetch(m1[1].lc, m1[1])
var time2 = time_aliases.fetch(m2[1].lc, m2[1])

var r1 = units.convert(amount, len1, len2)
var r2 = units.convert(amount, time1, time2)
var r1 = units.convert(1, len1, len2)
var r2 = units.convert(1, time1, time2)

if (defined(r1) && defined(r2)) {
say round_result(amount * r1/r2, precision)
return true
return round_result(amount * r1/r2, precision)
}
}

with (units.convert(amount, from, to)) { |r|
say round_result(r, precision)
return true
return round_result(r, precision)
}
}
else {
return try { eval(command) }
}

if (command =~ /help/i) {
return more_help()
return nil
}

if (__FILE__ == __MAIN__) {

var cmd = []

while (ARGV) {
given (var arg = ARGV.shift) {
when ('-p') {
precision = Num(ARGV.shift)
}
when ('-i') {
interactive = true
}
when ('-h') {
print help()
Sys.exit(0)
}
when ('-v') {
say "smart-units #{VERSION}"
Sys.exit(0)
}
default {
cmd << arg
}
}
}

say (try { eval(command) } \\ "...")
if (cmd.is_empty && STDIN.is_on_tty) {
interactive = true
}

return false
}
if (interactive) {
print <<-"EOT"
Welcome to smart-units #{VERSION} in interactive mode.
EOT
loop {
var line = read(">> ", String) \\ break

if (interactive) {
print <<"EOT"
Welcome to smart-units #{VERSION} in interactive mode.
EOT
loop {
var line = read(">> ", String) \\ break
if (line ~~ ['q', 'exit']) {
break
}

if (line ~~ ['q', 'exit']) {
break
}
if (line =~ /help/i) {
print more_help()
next
}

process_input(line)
line =~ /\S/ || next

with (process_input(line)) {|r|
say r
}
}
}
else {
cmd || do { print help(); Sys.exit(1) }
with (process_input(cmd.join(' '))) { |r|
say r
}
else {
Sys.exit(1)
}
}
}
else {
Sys.exit((cmd.is_empty ? help() : process_input(cmd.join(' '))) ? 0 : 1)
}

0 comments on commit 8b72ac7

Please sign in to comment.