aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/plumber
blob: 71a19ad3eaff6e9886aa6403ddb56a8116a8124b (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
58
59
60
#! /usr/bin/env elixir

defmodule ArgParser do
  def parse do
    {opts, _} =
      System.argv()
      |> OptionParser.parse!(strict: [option: :string, text: :string])

    options = [
      "First letter of each word in sentence",  #0
      "Single line of text",                    #1
      "Thesaurus",                              #2
      "KJV Verse Lookup"                        #3
    ]

    cond do
      opts[:option] == options |> Enum.at(0) ->
        TextPlumber.firstLetterOfWordsIn(opts[:text]) |> IO.binwrite()

      opts[:option] == options |> Enum.at(1) ->
        TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite()

      opts[:option] == options |> Enum.at(2) ->
        TextPlumber.synonymOfFirstWordIn(opts[:text])
        TextPlumber.firstWordOf(opts[:text]) |> IO.binwrite()

      opts[:option] == options |> Enum.at(3) ->
        System.cmd("urxvt", ["-e", "sh", "-c", "kjv #{TextPlumber.singleLineOf(opts[:text])}"])
        TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite()

      opts[:option] == "list" ->
        Enum.map(options, fn option -> IO.puts(option) end)

      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 synonymOfFirstWordIn(text) do
    System.cmd("urxvt", ["-e", "sh", "-c", "dict #{TextPlumber.firstWordOf(text)} | vim -"])
  end
end

ArgParser.parse()