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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#! /usr/bin/env elixir
defmodule Settings do
def terminal do
System.get_env("TERMINAL")
end
end
defmodule ArgParser do
def parse do
{opts, _} =
System.argv()
|> OptionParser.parse!(strict: [option: :string, text: :string])
options = [
camelize: "text->camelize",
case_lower: "case->lower",
case_title: "case->title",
case_titleize: "case->titleize",
case_upper: "case->upper",
date8601: "text->date8601",
dictionary: "word->dictionary",
jumbleize: "text->jumbleize",
kjv: "verse->kjv",
letterize: "text->letterize",
lorem_paragraph: "lorem->paragraph",
lorem_title: "lorem->title",
reverse_letters: "reverse->letters",
reverse_words: "reverse->words",
singleline: "text->singleline",
slugize: "text->slugize",
urlize: "text->urlize",
]
cond do
opts[:option] == options[:letterize] ->
TextPlumber.firstLetterOfWordsIn(opts[:text]) |> IO.puts()
opts[:option] == options[:urlize] ->
URI.encode(opts[:text]) |> IO.puts()
opts[:option] == options[:slugize] ->
opts[:text]
|> String.normalize(:nfd)
|> String.replace(~r/[[:space:]]+/, " ")
|> String.replace(~r/[[:punct:]]+/, " ")
|> Macro.underscore()
|> String.replace(~r/[[:space:]]+/, "-")
|> String.replace(~r/[[:punct:]]+/, "-")
|> String.trim("-")
|> IO.puts()
opts[:option] == options[:camelize] ->
opts[:text]
|> String.normalize(:nfd)
|> String.replace(~r/[[:space:]]+/, "_")
|> String.replace(~r/[[:punct:]]+/, "_")
|> Macro.camelize()
|> String.replace(~r/[[:space:]]+/, "")
|> String.replace(~r/[[:punct:]]+/, "")
|> IO.puts()
opts[:option] == options[:singleline] ->
TextPlumber.singleLineOf(opts[:text]) |> IO.puts()
opts[:option] == options[:jumbleize] ->
String.split(opts[:text] |> String.replace(~r/[[:punct:]]+/, ""))
|> Enum.map(&String.codepoints/1)
|> Enum.map(&TextPlumber.jumble/1)
|> Enum.map(&Enum.join/1)
|> Enum.join(" ")
|> IO.puts()
opts[:option] == options[:reverse_words] ->
TextPlumber.reverse(opts[:text]) |> IO.puts()
opts[:option] == options[:reverse_letters] ->
String.split(opts[:text])
|> Enum.map(&String.codepoints/1)
|> Enum.map(&Enum.reverse/1)
|> Enum.join(" ")
|> IO.puts()
opts[:option] == options[:dictionary] ->
TextPlumber.dictionOfFirstWordIn(opts[:text])
TextPlumber.firstWordOf(opts[:text]) |> IO.puts()
opts[:option] == options[:case_title] ->
opts[:text] |> to_string |> TextPlumber.titleCaseOf() |> IO.puts()
opts[:option] == options[:case_titleize] ->
String.downcase(opts[:text]) |> TextPlumber.titleCaseOf() |> IO.puts()
opts[:option] == options[:case_lower] ->
String.downcase(opts[:text]) |> IO.puts()
opts[:option] == options[:case_upper] ->
String.upcase(opts[:text]) |> IO.puts()
opts[:option] == options[:lorem_title] ->
TextPlumber.loremTitle()
opts[:option] == options[:lorem_paragraph] ->
TextPlumber.loremParagraph()
opts[:option] == options[:date8601] ->
TextPlumber.date8601()
opts[:option] == options[:kjv] ->
System.cmd(Settings.terminal(), [
"-e",
"sh",
"-c",
"kjv #{TextPlumber.singleLineOf(opts[:text])}"
])
TextPlumber.singleLineOf(opts[:text]) |> IO.puts()
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*/u, "\\1", global: true)
|> String.replace(~r/\s\s+/u, " ", 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 reverse(text) do
String.split(text) |> Enum.reverse |> Enum.join(" ")
end
def titleCaseOf(text) do
text |> String.split() |> Enum.map(fn word -> :string.titlecase(word) end) |> Enum.join(" ")
end
def loremParagraph() do
{string, _return} =
System.cmd("sh", [
"-c",
"perl -e 'use Text::Lorem; use Text::Wrap; $Text::Wrap::columns = 80; print wrap(\"\", \"\", Text::Lorem->new()->sentences(5));'"
])
IO.puts(string)
end
def loremTitle() do
{string, _return} =
System.cmd("sh", [
"-c",
"perl -e 'use Text::Lorem; print Text::Lorem->new()->words(4)';"
])
IO.puts(string)
end
def dictionOfFirstWordIn(text) do
System.cmd(Settings.terminal(), [
"-e",
"sh",
"-c",
"dict -h localhost #{TextPlumber.firstWordOf(text)} 2>&1 | vim -"
])
end
def jumble(list) do
if length(list) == 1 do
list
else
head = Enum.take(list, 1)
tail = Enum.take(list, -1)
list = list |> Enum.drop(-1) |> Enum.drop(1)
if length(list) == 2 do
head ++ (list |> Enum.reverse()) ++ tail
else
head ++ (list |> Enum.shuffle()) ++ tail
end
end
end
def date8601() do
{string, _return} =
System.cmd("date", [
"--universal",
"--iso-8601=seconds"
])
IO.puts(string)
end
end
ArgParser.parse()
|