#! /usr/bin/env elixir defmodule ArgParser do def parse do {opts, _} = System.argv() |> OptionParser.parse!(strict: [option: :string, text: :string]) cond do opts[:option] == "First letter of each word in sentence" -> TextPlumber.firstLetterOfWordsIn(opts[:text]) |> IO.binwrite() opts[:option] == "Single line of text" -> TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite() opts[:option] == "Thesaurus" -> TextPlumber.synonymOfFirstWordIn(opts[:text]) TextPlumber.firstWordOf(opts[:text]) |> IO.binwrite() opts[:option] == "KJV Verse Lookup" -> System.cmd("urxvt", ["-e", "sh", "-c", "kjv #{TextPlumber.singleLineOf(opts[:text])}"]) TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite() opts[:option] == "Title Case" -> TextPlumber.titleCaseOf(opts[:text]) |> IO.binwrite() true -> "No plumber #{opts[:option] || "specified"}." |> IO.puts() end end end defmodule TextPlumber do def firstLetterOfWordsIn(text) do String.replace(text, ~r/(\w)\w*/, "\\1", global: true) |> String.replace(~r/\s\s+/, " ", global: true) end def singleLineOf(text) do String.replace(text, "\n", " ") |> String.replace(~r/\s\s+/, " ", global: true) end def firstWordOf(text) do String.split(text) |> List.first() end def titleCaseOf(text) do text |> String.split() |> Enum.map(fn word -> :string.titlecase(word) end) |> Enum.join(" ") end def synonymOfFirstWordIn(text) do System.cmd("urxvt", ["-e", "sh", "-c", "dict #{TextPlumber.firstWordOf(text)} | vim -"]) end end ArgParser.parse()