Skip to content

Instantly share code, notes, and snippets.

@vKxni
Created December 11, 2022 12:43
Show Gist options
  • Save vKxni/440cca283c0f046a039afd1075d80d43 to your computer and use it in GitHub Desktop.
Save vKxni/440cca283c0f046a039afd1075d80d43 to your computer and use it in GitHub Desktop.
IO.println() in Elixir
defmodule Koni do
@moduledoc """
Create a file with the following content:
IO.println("Your-text-here")
Then run the following command:
Koni.println("path/to/file")
"""
def read_file(file_path) do
{:ok, contents} = File.read(file_path)
String.split(contents, "\n")
end
def println(file_path) do
file_lines = read_file(file_path)
regex = ~r/IO\.println\(["'](.*?)["']\)/
matches = Enum.filter(file_lines, fn line -> Regex.match?(regex, line) end)
if Enum.empty?(matches) do
{:error, "The file doesn't contain any \"IO.println\" function calls."}
else
Enum.each(matches, fn match ->
[_, text] = Regex.run(regex, match)
IO.puts(text)
end)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment