-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot.iex.exs
163 lines (130 loc) · 3.96 KB
/
dot.iex.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Do not forget that you can (and should) use .iex.exs files per each project
# to simplify your life. You can write something like this:
#
# File.exists?(Path.expand("~/.iex.exs")) && import_file("~/.iex.exs")
#
# alias Project.{Repo, User, Post}
#
# defmodule MyHelpers
# # ...
# end
defmodule H do
@moduledoc """
Defines a few helper methods to enchance visual appearance and available
functionality of the IEx.
### Extra Information
First time I've learned about customizations to `.iex.exs` here:
- https://www.youtube.com/watch?v=E0bmtcYrz9M
Here is a couple of good articles:
- https://samuelmullen.com/articles/customizing_elixirs_iex/
- https://www.adiiyengar.com/blog/20180709/my-iex-exs
"""
@tips_and_tricks [
":observer.start() - a GUI for BEAM",
"runtime_info <:memory|:applications|...> - sometimes useful data",
"IEx.configure(inspect: [limit: :infinity]) - show everything"
]
# Lookup an app in the started applications list
def is_app_started?(app) when is_atom(app) do
Application.started_applications()
|> Enum.any?(&(elem(&1, 0) == app))
end
# Message queue length for the IEx process: nice to see when playing with
# remote nodes (distributed Erlang)
def queue_length do
self()
|> Process.info()
|> Keyword.get(:message_queue_len)
end
# Wrap given text in the given ANSI color to prepare for output
def colorize(text, ansi_color),
do: ansi_color <> text <> IO.ANSI.reset()
def print_tips_n_tricks() do
"\n--- Tips & Tricks:" |> colorize(IO.ANSI.bright()) |> IO.puts()
Enum.map(@tips_and_tricks, &IO.puts/1)
end
@doc """
Copy from the IEx session into macOS clipboard (using pbcopy). Original idea:
https://shyr.io/blog/iex-copy-to-clipboard.
## Usage
iex> User |> Repo.get!(user_id) |> H.copy
"""
def copy(term) do
text =
if is_binary(term) do
term
else
inspect(term, limit: :infinity, pretty: true)
end
port = Port.open({:spawn, "pbcopy"}, [])
true = Port.command(port, text)
true = Port.close(port)
:ok
end
end
# We will be using `ANSI`
Application.put_env(:elixir, :ansi_enabled, true)
# Letting user know some hints
H.print_tips_n_tricks()
###
## IEx Settings
###
prefix = H.colorize("%prefix", IO.ANSI.green())
counter = H.colorize("-%node-(%counter)", IO.ANSI.green())
info = H.colorize("#{H.queue_length()}", IO.ANSI.light_blue())
last = H.colorize(">", IO.ANSI.yellow())
alive = H.colorize("⚡", IO.ANSI.bright() <> IO.ANSI.yellow())
default_prompt = prefix <> counter <> " " <> info <> " " <> last
alive_prompt = prefix <> counter <> " " <> info <> " " <> alive <> last
IEx.configure(
alive_prompt: alive_prompt,
auto_reload: true,
colors: [
eval_result: [:green, :bright],
eval_error: [:red, :bright],
eval_info: [:blue, :bright]
],
default_prompt: default_prompt,
history_size: 100,
inspect: [limit: 5_000, pretty: true]
)
###
## Phoenix & Ecto Helpers
###
"\n--- Phoenix & Ecto:" |> H.colorize(IO.ANSI.bright()) |> IO.puts()
phoenix_started? = H.is_app_started?(:phoenix)
ecto_started? = H.is_app_started?(:ecto)
phoenix_info =
if phoenix_started? do
H.colorize("running", IO.ANSI.green())
else
H.colorize("not detected", IO.ANSI.yellow())
end
IO.puts("Phoenix: #{phoenix_info}")
ecto_info =
if ecto_started? do
H.colorize("running", IO.ANSI.green())
else
H.colorize("not detected", IO.ANSI.yellow())
end
repo_module_name =
if ecto_started? do
Mix.Project.get().project()[:app]
|> Application.get_env(:ecto_repos)
|> then(fn
nil ->
""
[repo_mod | _] ->
repo_alias = Atom.to_string(repo_mod) |> String.replace(~r/^Elixir\./, "")
H.colorize("(`alias #{repo_alias}, as: Repo`)", IO.ANSI.faint())
end)
else
""
end
if ecto_started? do
import_if_available(Ecto.Query)
import_if_available(Ecto.Changeset)
end
IO.puts("Ecto: #{ecto_info} #{repo_module_name}")
# One extra empty line before command line
IO.puts("")