aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/plumber
blob: b791adc5cbed9bf8cf6f869b843d68452d9f28f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /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()