Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show fortune #128

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
21 changes: 17 additions & 4 deletions lib/nerves_motd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ defmodule NervesMOTD do
@typedoc """
MOTD options
"""
@type option() :: {:logo, IO.ANSI.ansidata()} | {:extra_rows, [row()]}
@type option() ::
{:logo, IO.ANSI.ansidata()}
| {:extra_rows, [row()]}
| {:show_fortune?, boolean()}

@typedoc """
One row of information
Expand All @@ -48,6 +51,7 @@ defmodule NervesMOTD do
an empty logo (`""`) to remove it completely.
* `:extra_rows` - a list of custom rows or a callback for returning rows.
The callback can be a 0-arity function reference or MFArgs tuple.
* `:show_fortune?` - a boolean flag to show fortune.
"""
@spec print([option()]) :: :ok
def print(opts \\ []) do
Expand All @@ -62,9 +66,7 @@ defmodule NervesMOTD do
"\n",
Enum.map(rows(apps, combined_opts), &format_row/1),
"\n",
"""
Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html
"""
fortune(opts)
]
|> IO.ANSI.format()
|> IO.puts()
Expand All @@ -82,6 +84,17 @@ defmodule NervesMOTD do
Keyword.get(opts, :logo, @logo)
end

@spec fortune([option()]) :: IO.ANSI.ansidata()
defp fortune(opts) do
case Code.ensure_compiled(Fortune) do
{:module, fortune_mod} ->
if opts[:show_fortune?], do: [fortune_mod.random!(), "\n"], else: []

_ ->
[]
end
end

@spec rows(map(), list()) :: [[cell()]]
defp rows(apps, opts) do
[
Expand Down
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ defmodule NervesMOTD.MixProject do
[
{:nerves_runtime, "~> 0.8"},
{:nerves_time, "~> 0.4", optional: true},
{:nerves_tips, github: "nerves-project/nerves_tips", only: [:dev, :test], optional: true},
{:fortune, "~> 0.1", only: [:dev, :test], optional: true},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: :dev, runtime: false},
{:ex_doc, "~> 0.25", only: :docs, runtime: false},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.30.9", "d691453495c47434c0f2052b08dd91cc32bc4e1a218f86884563448ee2502dd2", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d7aaaf21e95dc5cddabf89063327e96867d00013963eadf2c6ad135506a8bc10"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"fortune": {:hex, :fortune, "0.1.0", "cf607c436159ce03cd30a7dd832996d1cc673ad1538831d2415af7cd83d649aa", [:mix], [], "hexpm", "9bf386a8574a83e44e11b375512c5079470c8d281bbb51c108689ca5962e47eb"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
Expand All @@ -18,6 +19,7 @@
"nerves_runtime": {:hex, :nerves_runtime, "0.13.6", "aef9cb5e6232605d4e5421b297f41cde0535a53913c21e51c7ebe6c71c1b9cf6", [:mix], [{:nerves_logging, "~> 0.2.0", [hex: :nerves_logging, repo: "hexpm", optional: false]}, {:nerves_uevent, "~> 0.1.0", [hex: :nerves_uevent, repo: "hexpm", optional: false]}, {:uboot_env, "~> 1.0 or ~> 0.3.0", [hex: :uboot_env, repo: "hexpm", optional: false]}], "hexpm", "44b031f46542079d67d3ef0bd7b0c966e580909dd5cc61b7825a76e881e7cd5a"},
"nerves_time": {:hex, :nerves_time, "0.4.6", "f02e5e866149f3884a4b125104a5b677f7e61d271c1f7d87bc989197dc02dae5", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:muontrap, "~> 1.0 or ~> 0.5", [hex: :muontrap, repo: "hexpm", optional: false]}], "hexpm", "1fecb9e9dd098c0e93d35205a79e604831779f2d2313cfe7c31d181678e26ce6"},
"nerves_time_zones": {:hex, :nerves_time_zones, "0.3.3", "4d1c2e2d852efe239845b16d0bb99dcaa761b74a0da6e84f6a3c4c5006fbe994", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:zoneinfo, "~> 0.1.2", [hex: :zoneinfo, repo: "hexpm", optional: false]}], "hexpm", "46e260ebe0db65952a24d08fa8c2da6df5512ab22c567bde4f8cce60b7ff403a"},
"nerves_tips": {:git, "https://github.com/nerves-project/nerves_tips.git", "92e012d9885c7fa6406b8d6b29168cb459273ad1", []},
"nerves_uevent": {:hex, :nerves_uevent, "0.1.0", "651111a46be9a238560cbf7946989fc500e5f33d7035fd9ea7194d07a281bc19", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:property_table, "~> 0.2.0", [hex: :property_table, repo: "hexpm", optional: false]}], "hexpm", "cb0b1993c3ed3cefadbcdb534e910af0661f95c3445796ce8a7c8be3519a4e5f"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"property_table": {:hex, :property_table, "0.2.4", "c53e3684b6da8cb77ceee89f8c7a980d6c626429a6653473db29c210b9d53103", [:mix], [], "hexpm", "feae0e06fee2740452dd8375eb2c3407ece12317525491345f141dcbe1b9442c"},
Expand Down