aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/plumber
blob: 99cc7f3a5eea0126d457dcc8bb1452e95db5f6c4 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/env elixir

defmodule Settings do
  def terminal do
    "urxvt"
  end
end

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

    options = [
      thesaurus: "Thesaurus",
      title_case: "Title Case",
      kjv_lookup: "KJV Verse Lookup",
      single_line: "Single line of text",
      first_letter: "First letter of each word in sentence",
      slug_text: "Slug",
      lower_case: "Lower Case"
    ]

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

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

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

      opts[:option] == options[:kjv_lookup] ->
        System.cmd(Settings.terminal(), [
          "-e",
          "sh",
          "-c",
          "kjv #{TextPlumber.singleLineOf(opts[:text])}"
        ])

        TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite()

      opts[:option] == options[:title_case] ->
        TextPlumber.titleCaseOf(opts[:text]) |> IO.binwrite()

      opts[:option] == options[:title_case] ->
        TextPlumber.titleCaseOf(opts[:text]) |> IO.binwrite()

      opts[:option] == options[:slug_text] ->
        TextPlumber.lowerCaseOf(opts[:text]) |> TextPlumber.slugOf() |> IO.binwrite()

      opts[:option] == options[:lower_case] ->
        TextPlumber.lowerCaseOf(opts[:text]) |> IO.binwrite()

      true ->
        Enum.each(options, fn {_, value} -> IO.puts(value) end)
    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(Settings.terminal(), [
      "-e",
      "sh",
      "-c",
      "dict -h localhost #{TextPlumber.firstWordOf(text)} 2>&1 | vim -"
    ])
  end

  def slugOf(text) do
    String.replace(text, " ", "-")
  end

  def lowerCaseOf(text) do
    String.downcase(text)
  end
end

ArgParser.parse()