From 446ed05e7700c9720a2c37f6c24e9bc44f75c5db Mon Sep 17 00:00:00 2001 From: Masatoshi N Date: Sun, 30 Oct 2022 19:06:07 -0400 Subject: [PATCH] Yellow text color + unsyncronized help text when clock is out of sync --- lib/nerves_motd.ex | 14 +++++++++++++- lib/nerves_motd/runtime.ex | 9 +++++++++ mix.exs | 1 + mix.lock | 4 +++- test/nerves_motd_test.exs | 11 +++++++++++ test/support/runtime.ex | 3 +++ 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/nerves_motd.ex b/lib/nerves_motd.ex index 6677790..edcaa35 100644 --- a/lib/nerves_motd.ex +++ b/lib/nerves_motd.ex @@ -84,7 +84,7 @@ defmodule NervesMOTD do [ [{"Serial", serial_number()}], [{"Uptime", uptime()}], - [{"Clock", Utils.formatted_local_time()}], + [clock_cell()], temperature_row(), [], [firmware_cell(), applications_cell(apps)], @@ -135,6 +135,18 @@ defmodule NervesMOTD do defp format_cell_value(value, 0, width), do: Utils.fit_ansidata(value, width) defp format_cell_value(value, 1, _width), do: value + @spec clock_cell() :: cell() + defp clock_cell() do + formatted_clock = + if runtime_mod().time_synchronized?() do + Utils.formatted_local_time() + else + [:yellow, Utils.formatted_local_time(), " (unsynchronized)", :reset] + end + + {"Clock", formatted_clock} + end + @spec firmware_cell() :: cell() defp firmware_cell() do fw_active = runtime_mod().active_partition() diff --git a/lib/nerves_motd/runtime.ex b/lib/nerves_motd/runtime.ex index 8c169f5..e10e58d 100644 --- a/lib/nerves_motd/runtime.ex +++ b/lib/nerves_motd/runtime.ex @@ -21,6 +21,7 @@ defmodule NervesMOTD.Runtime do used_percent: non_neg_integer() }} | :error + @callback time_synchronized?() :: boolean() end defmodule NervesMOTD.Runtime.Target do @@ -112,4 +113,12 @@ defmodule NervesMOTD.Runtime.Target do # In case the `df` command is not available or any of the out parses incorrectly _error -> :error end + + if Code.ensure_loaded?(NervesTime) do + @impl NervesMOTD.Runtime + def time_synchronized?(), do: apply(NervesTime, :synchronized?, []) + else + @impl NervesMOTD.Runtime + def time_synchronized?(), do: true + end end diff --git a/mix.exs b/mix.exs index 909809c..1b68afe 100644 --- a/mix.exs +++ b/mix.exs @@ -33,6 +33,7 @@ defmodule NervesMOTD.MixProject do defp deps do [ {:nerves_runtime, "~> 0.8"}, + {:nerves_time, "~> 0.4", optional: true}, {:credo, "~> 1.5", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false}, {:ex_doc, "~> 0.25", only: :docs, runtime: false}, diff --git a/mix.lock b/mix.lock index 241baef..487b222 100644 --- a/mix.lock +++ b/mix.lock @@ -13,8 +13,10 @@ "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, "mix_test_watch": {:hex, :mix_test_watch, "1.1.0", "330bb91c8ed271fe408c42d07e0773340a7938d8a0d281d57a14243eae9dc8c3", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "52b6b1c476cbb70fd899ca5394506482f12e5f6b0d6acff9df95c7f1e0812ec3"}, "mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"}, + "muontrap": {:hex, :muontrap, "1.0.0", "53a05c37f71cc5070aaa0858a774ae1f500160b7186a70565521a14ef7843c5a", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "0d3cd6e335986f9c2af1b61f583375b0f0d91cea95b7ec7bc720f330b4dc9b49"}, "nerves_logging": {:hex, :nerves_logging, "0.2.0", "4099b860f41a0171ff49fbc1e86ee0ce4576c24c1cf318a0fd0bf227355e8c12", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "07cfb9fe9d21b908da51b81a1ced858288c68519204aca7aa2c1fcd531d5e059"}, - "nerves_runtime": {:hex, :nerves_runtime, "0.13.3", "378d6dda56291c0144567f24eb2d4ade314012443ce5d03542adec93091ad9b7", [: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", "00b51d9675cae78cc82910e18508e5c8b8a032dad7a83d417c4d061d802e3107"}, + "nerves_runtime": {:hex, :nerves_runtime, "0.13.3", "378d6dda56291c0144567f24eb2d4ade314012443ce5d03542adec93091ad9b7", [: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, "~> 0.3.0 or ~> 1.0", [hex: :uboot_env, repo: "hexpm", optional: false]}], "hexpm", "00b51d9675cae78cc82910e18508e5c8b8a032dad7a83d417c4d061d802e3107"}, + "nerves_time": {:hex, :nerves_time, "0.4.5", "038d6754421b20f21eff8918f006d26a2c3e7440dc6954c4164030427e9228d8", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:muontrap, "~> 0.5 or ~> 1.0", [hex: :muontrap, repo: "hexpm", optional: false]}], "hexpm", "cbe4593bb63ba6fd66f09c321f558037ca9de6ad8b93bc0dc77c4209c4734a88"}, "nerves_time_zones": {:hex, :nerves_time_zones, "0.3.1", "0ccb71faf9db9de37ad40a2f75ea0d7f5e7a24ac6e07c19a369e485ba709205c", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:zoneinfo, "~> 0.1.2", [hex: :zoneinfo, repo: "hexpm", optional: false]}], "hexpm", "69d026322fb8062315fc8c28e24de4600fcee2b6d0b5abba83a355ff62290c53"}, "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.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, diff --git a/test/nerves_motd_test.exs b/test/nerves_motd_test.exs index 82d7b59..266c5d9 100644 --- a/test/nerves_motd_test.exs +++ b/test/nerves_motd_test.exs @@ -113,6 +113,17 @@ defmodule NervesMOTDTest do assert capture_motd() =~ ~r/Clock : \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \w{3}/ end + test "Clock gets highlighted when not synchronized" do + NervesMOTD.MockRuntime + |> Mox.expect(:applications, 1, default_applications_code()) + |> Mox.expect(:active_partition, 1, fn -> "A" end) + |> Mox.expect(:firmware_validity, 1, fn -> :valid end) + |> Mox.expect(:time_synchronized?, 1, fn -> false end) + + assert capture_motd() =~ + ~r/Clock : \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \w{3} \(unsynchronized\)/ + end + test "Temperature when available" do NervesMOTD.MockRuntime |> Mox.expect(:applications, 1, default_applications_code()) diff --git a/test/support/runtime.ex b/test/support/runtime.ex index 1a7fdfe..767a4a1 100644 --- a/test/support/runtime.ex +++ b/test/support/runtime.ex @@ -38,4 +38,7 @@ defmodule NervesMOTD.Runtime.Host do # Raise if the path isn't the expected one for the unit tests {:ok, %{size_mb: 14_619, used_mb: 37, used_percent: 0}} end + + @impl NervesMOTD.Runtime + def time_synchronized?(), do: true end