Created
December 11, 2022 12:43
-
-
Save vKxni/440cca283c0f046a039afd1075d80d43 to your computer and use it in GitHub Desktop.
IO.println() in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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