From df4aed2a5443a5e906a3c6a50f7cdd3c961eea82 Mon Sep 17 00:00:00 2001 From: tdro Date: Wed, 6 Apr 2022 22:51:20 -0400 Subject: .local/bin/plumber: Add smart camel case conversion Make slug converter recognize camel case. Wrap generated lorem ipsum text. --- .local/bin/plumber | 85 ++++++++++++++++++++++++++++-------------------- .local/bin/plumber-dmenu | 19 ++++++----- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/.local/bin/plumber b/.local/bin/plumber index 3d4ebdc..4483708 100755 --- a/.local/bin/plumber +++ b/.local/bin/plumber @@ -13,46 +13,59 @@ defmodule ArgParser do |> OptionParser.parse!(strict: [option: :string, text: :string]) options = [ - first_letter: "first letter word", - single_line: "single line", - slug_text: "slug", - title_case: "title case", - lower_case: "lower case", - lorem_title: "lorem title", - lorem_paragraph: "lorem paragraph", - thesaurus: "thesaurus", - kjv_lookup: "kjv verse lookup" + camelize: "text->camelize", + dictionary: "word->dictionary", + first_letter: "text->first-letter-words", + kjv_lookup: "verse->kjv", + lorem_paragraph: "lorem->paragraph", + lorem_title: "lorem->title", + lower_case: "case->lower", + single_line: "text->single-line", + slugize: "text->slugize", + title_case: "case->title", + title_case_strict: "case->title-strict" ] cond do opts[:option] == options[:first_letter] -> TextPlumber.firstLetterOfWordsIn(opts[:text]) |> IO.binwrite() + 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.binwrite() + + 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.binwrite() + opts[:option] == options[:single_line] -> TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite() - opts[:option] == options[:thesaurus] -> - TextPlumber.synonymOfFirstWordIn(opts[:text]) + opts[:option] == options[:dictionary] -> + TextPlumber.dictionOfFirstWordIn(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.lowerCaseOf(opts[:text]) |> TextPlumber.titleCaseOf() |> IO.binwrite() + opts[:text] |> to_string |> TextPlumber.titleCaseOf() |> IO.binwrite() - opts[:option] == options[:slug_text] -> - TextPlumber.lowerCaseOf(opts[:text]) |> TextPlumber.slugOf() |> IO.binwrite() + opts[:option] == options[:title_case_strict] -> + String.downcase(opts[:text]) |> TextPlumber.titleCaseOf() |> IO.binwrite() opts[:option] == options[:lower_case] -> - TextPlumber.lowerCaseOf(opts[:text]) |> IO.binwrite() + String.downcase(opts[:text]) |> IO.binwrite() opts[:option] == options[:lorem_title] -> TextPlumber.loremTitle() @@ -60,6 +73,16 @@ defmodule ArgParser do opts[:option] == options[:lorem_paragraph] -> TextPlumber.loremParagraph() + opts[:option] == options[:kjv_lookup] -> + System.cmd(Settings.terminal(), [ + "-e", + "sh", + "-c", + "kjv #{TextPlumber.singleLineOf(opts[:text])}" + ]) + + TextPlumber.singleLineOf(opts[:text]) |> IO.binwrite() + true -> Enum.each(options, fn {_, value} -> IO.puts(value) end) end @@ -85,19 +108,11 @@ defmodule TextPlumber do text |> String.split() |> Enum.map(fn word -> :string.titlecase(word) end) |> Enum.join(" ") end - def slugOf(text) do - String.replace(text, " ", "-") - end - - def lowerCaseOf(text) do - String.downcase(text) - end - def loremParagraph() do {string, _return} = System.cmd("sh", [ "-c", - "perl -e 'use Text::Lorem; print Text::Lorem->new()->sentences(5)';" + "perl -e 'use Text::Lorem; use Text::Wrap; $Text::Wrap::columns = 80; print wrap(\"\", \"\", Text::Lorem->new()->sentences(5));'" ]) IO.puts(string) @@ -113,7 +128,7 @@ defmodule TextPlumber do IO.puts(string) end - def synonymOfFirstWordIn(text) do + def dictionOfFirstWordIn(text) do System.cmd(Settings.terminal(), [ "-e", "sh", diff --git a/.local/bin/plumber-dmenu b/.local/bin/plumber-dmenu index 9779223..07e0488 100755 --- a/.local/bin/plumber-dmenu +++ b/.local/bin/plumber-dmenu @@ -1,14 +1,15 @@ #!/bin/sh -eu options="\ -first letter word -single line -slug -title case -lower case -lorem title -lorem paragraph -thesaurus -kjv verse lookup +text->camelize +word->dictionary +text->first-letter-words +verse->kjv +lorem->paragraph +lorem->title +case->lower +text->single-line +text->slugize +case->title " printf "%s" "$options" \ | dmenu -i -b -p ⠀:::⠀plumber⠀::: \ -- cgit v1.2.3