File 1572-Add-parameter-to-specify-documentation-width-in-the-.patch of Package erlang
From f8a6528f4bbef25b5d74cb39eb047933236ecd33 Mon Sep 17 00:00:00 2001
From: Dmitri Vereshchagin <dmitri.vereshchagin@gmail.com>
Date: Sun, 7 Jul 2024 14:12:28 +0300
Subject: [PATCH 2/2] Add parameter to specify documentation width in the shell
It may be hard to read the documentation rendered over the whole width
of the screen. The stdlib configuration parameter is added to override
this default behavior.
---
lib/stdlib/doc/stdlib_app.md | 5 ++++
lib/stdlib/src/shell_docs.erl | 24 ++++++++++++-----
lib/stdlib/test/shell_docs_SUITE.erl | 40 +++++++++++++++++++++++++---
3 files changed, 59 insertions(+), 10 deletions(-)
diff --git a/lib/stdlib/doc/stdlib_app.md b/lib/stdlib/doc/stdlib_app.md
index 01184b36cb..aa863dba3f 100644
--- a/lib/stdlib/doc/stdlib_app.md
+++ b/lib/stdlib/doc/stdlib_app.md
@@ -40,6 +40,11 @@ For more information about configuration parameters, see the
- **`shell_catch_exception = boolean()`{: #shell_catch_exception }** - Can be
used to set the exception handling of the evaluator process of Erlang shell.
+- **`shell_docs_columns = pos_integer()`{: #shell_docs_columns }** -
+ Configures how wide the documentation should be rendered in the shell.
+
+ See also `t:shell_docs:config/0`.
+
- **`shell_expand_location = above | below`{: #shell_expand_location }** - Sets
where the tab expansion text should appear in the shell. The default is
`below`. This will open a pager below the cursor that is scrollable one line
diff --git a/lib/stdlib/src/shell_docs.erl b/lib/stdlib/src/shell_docs.erl
index 99d372b8b3..97f0171fb0 100644
--- a/lib/stdlib/src/shell_docs.erl
+++ b/lib/stdlib/src/shell_docs.erl
@@ -116,7 +116,9 @@ The configuration of how the documentation should be rendered.
- **columns** - Configure how wide the target documentation should be rendered.
By default `shell_docs` used the value returned by
- [`io:columns()`](`io:columns/0`).
+ [`io:columns()`](`io:columns/0`). It is possible to override this default
+ by setting the stdlib configuration parameter `shell_docs_columns`
+ to a `t:pos_integer/0` value.
""".
-doc #{ since => ~"OTP 23.2" }.
-type config() :: #{ encoding => unicode | latin1,
@@ -919,12 +921,7 @@ init_config(D, Config) when is_map(Config) ->
Columns =
case maps:find(columns, Config) of
error ->
- case io:columns() of
- {ok, C} ->
- C;
- _ ->
- 80
- end;
+ get_columns();
{ok, C} ->
C
end,
@@ -936,6 +933,19 @@ init_config(D, Config) when is_map(Config) ->
init_config(D, Config) ->
Config#config{ docs = D }.
+get_columns() ->
+ case application:get_env(stdlib, shell_docs_columns) of
+ {ok, C} when is_integer(C), C > 0 ->
+ C;
+ _ ->
+ case io:columns() of
+ {ok, C} ->
+ C;
+ _ ->
+ 80
+ end
+ end.
+
render_docs(Elems,State,Pos,Ind,D) when is_list(Elems) ->
lists:mapfoldl(fun(Elem,P) ->
render_docs(Elem,State,P,Ind,D)
diff --git a/lib/stdlib/test/shell_docs_SUITE.erl b/lib/stdlib/test/shell_docs_SUITE.erl
index 5fab9753b5..8b6fe16f87 100644
--- a/lib/stdlib/test/shell_docs_SUITE.erl
+++ b/lib/stdlib/test/shell_docs_SUITE.erl
@@ -24,7 +24,8 @@
-export([all/0, suite/0, groups/0, init_per_suite/1, end_per_suite/1,
init_per_group/2, end_per_group/2, init_per_testcase/2, end_per_testcase/2]).
--export([render/1, links/1, normalize/1, render_prop/1, render_non_native/1, ansi/1]).
+-export([render/1, links/1, normalize/1, render_prop/1,
+ render_non_native/1, ansi/1, columns/1]).
-export([render_function/1, render_type/1, render_callback/1]).
-export([render_all/1, update_render/0, update_render/1]).
@@ -41,7 +42,7 @@ all() ->
[ {group, render},
{group, prop},
{group, render_smoke},
- ansi
+ ansi, columns
].
groups() ->
@@ -61,7 +61,8 @@ end_per_group(_GroupName, _Config) ->
init_per_testcase(_TestCase, Config) ->
Env = [{App, Key, application:get_env(App, Key)}
- || {App, Key} <- [{kernel, shell_docs_ansi}]],
+ || {App, Key} <- [{kernel, shell_docs_ansi},
+ {stdlib, shell_docs_columns}]],
[{env, Env} | Config].
end_per_testcase(_TestCase, Config) ->
@@ -439,3 +440,36 @@ ansi(_Config) ->
?assert(HasESC(#{ansi => true})),
ok.
+
+-doc """
+Doc doc doc doc doc doc doc doc doc doc doc doc doc doc doc.
+""".
+columns(_Config) ->
+ {ok, Docs} = code:get_doc(?MODULE),
+
+ MaxColumns =
+ fun(Config0) ->
+ Config = maps:merge(#{ansi => false}, Config0),
+ Doc = shell_docs:render(?MODULE, ?FUNCTION_NAME, Docs, Config),
+ Lines = string:split(Doc, "\n", all),
+ lists:max(lists:map(fun string:length/1, Lines))
+ end,
+
+ application:set_env(stdlib, shell_docs_columns, 30),
+ ?assert(MaxColumns(#{}) =< 30),
+ ?assert(MaxColumns(#{columns => 20}) =< 20),
+
+ application:set_env(stdlib, shell_docs_columns, not_an_integer),
+ ?assert(MaxColumns(#{}) > 30),
+
+ application:set_env(stdlib, shell_docs_columns, 0),
+ ?assert(MaxColumns(#{}) > 30),
+
+ application:set_env(stdlib, shell_docs_columns, -30),
+ ?assert(MaxColumns(#{}) > 30),
+
+ application:unset_env(stdlib, shell_docs_columns),
+ ?assert(MaxColumns(#{}) > 30),
+ ?assert(MaxColumns(#{columns => 20}) =< 20),
+
+ ok.
--
2.43.0