Skip to content

Instantly share code, notes, and snippets.

@michaelvobrien
Created January 11, 2017 23:09
Show Gist options
  • Save michaelvobrien/098865620f8a2c5f86448a98b37688bb to your computer and use it in GitHub Desktop.
Save michaelvobrien/098865620f8a2c5f86448a98b37688bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env elixir
# similar to `while gets` and prints line
defmodule Helpers do
def chomp(string) do
String.trim_trailing(string, "\n")
end
def p(arg) do
inspect(arg, pretty: true)
end
end
defmodule Command do
import Helpers, warn: false
import IO, warn: false, except: [inspect: 1, inspect: 2]
import String, warn: false, except: [reverse: 1]
import Enum, warn: false, except: [
at: 2,
chunk: 2,
slice: 2,
slice: 3,
split: 2
]
def run(commands) do
code = """
IO.stream(:stdio, :line)
|> Enum.each(fn(line) ->
chomp(line)
|> #{commands}
|> IO.puts
end)
"""
Code.eval_string(code, [commands: commands], __ENV__)
end
end
[command | _rest] = System.argv
Command.run(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment