Skip to content

improved readability of help messages #10304

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

Merged
merged 49 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b8f9bdb
improved readability of help messages
michelou Nov 13, 2020
5c29cfb
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 13, 2020
093e5ca
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 13, 2020
161b012
removed dependency on jline terminal
michelou Nov 13, 2020
80e673f
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 14, 2020
33d8b63
added defaultPageWidth into ScalaSettings
michelou Nov 14, 2020
5a257df
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 15, 2020
fa6cbeb
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 17, 2020
30fca32
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 19, 2020
686c0d1
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 19, 2020
99c3f04
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 21, 2020
3f97991
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Nov 23, 2020
4f825f6
sync with upstream
michelou Jan 30, 2021
27fecd7
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Jan 30, 2021
29c552d
improved readability of help messages
michelou Nov 13, 2020
73e482a
removed dependency on jline terminal
michelou Nov 13, 2020
c0e1d6c
rebased with upstream
michelou Feb 22, 2021
95229d4
Merge branch 'scala3-help' of https://github.com/michelou/dotty into …
michelou Feb 22, 2021
5c82e3e
Merge remote-tracking branch 'upstream/master' into HEAD
michelou Feb 23, 2021
0f7503c
sync with upstream
michelou Feb 25, 2021
5059475
Merge remote-tracking branch 'upstream/master' into HEAD
michelou Feb 26, 2021
5613181
Merge remote-tracking branch 'upstream/master' into HEAD
michelou Mar 1, 2021
8316dc4
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 2, 2021
00bd288
sync with upstream, added tput
michelou Mar 2, 2021
19d7024
added missing source file
michelou Mar 2, 2021
6997b46
improved readability of help messages
michelou Nov 13, 2020
d330207
2nd attempt
michelou Mar 2, 2021
3839dd2
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 2, 2021
3598d66
sync with upstream
michelou Mar 3, 2021
056f47a
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 3, 2021
a957a51
sync with upstream
michelou Mar 4, 2021
3495899
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 4, 2021
b9887ff
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 4, 2021
abf5a9f
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 4, 2021
9e5f044
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 4, 2021
9ae830a
improved readability of help messages
michelou Nov 13, 2020
f714e36
removed dependency on jline terminal
michelou Nov 13, 2020
40156d5
sync with upstream
michelou Mar 4, 2021
5b2970c
improved readability of help messages
michelou Nov 13, 2020
4ef48a9
removed dependency on jline terminal
michelou Nov 13, 2020
5148a33
added defaultPageWidth into ScalaSettings
michelou Nov 14, 2020
6c38e39
sync with upstream, added tput
michelou Mar 2, 2021
49306d3
improved readability of help messages
michelou Nov 13, 2020
c743126
2nd attempt
michelou Mar 2, 2021
8f73dae
sync with upstream
michelou Mar 4, 2021
270fe25
Bring community build submodules up to date
griggt Mar 4, 2021
1c03961
dist/bin/*.bat belong to another branch
michelou Mar 5, 2021
640ed97
Merge branch 'scala3-help' of https://github.com/michelou/dotty into …
michelou Mar 5, 2021
063e09a
Merge remote-tracking branch 'upstream/master' into scala3-help
michelou Mar 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions compiler/src/dotty/tools/dotc/config/CliCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,48 @@ trait CliCommand:

/** Creates a help message for a subset of options based on cond */
protected def availableOptionsMsg(cond: Setting[?] => Boolean)(using settings: ConcreteSettings)(using SettingsState): String =
val ss = (settings.allSettings filter cond).toList sortBy (_.name)
val width = (ss map (_.name.length)).max
def format(s: String) = ("%-" + width + "s") format s
val ss = (settings.allSettings filter cond).toList sortBy (_.name)
val maxNameWidth = 30
val nameWidths = ss.map(_.name.length).partition(_ < maxNameWidth)._1
val width = if nameWidths.nonEmpty then nameWidths.max else maxNameWidth
val terminalWidth = settings.pageWidth.value
val (nameWidth, descriptionWidth) = {
val w1 =
if width < maxNameWidth then width
else maxNameWidth
val w2 =
if terminalWidth < w1 + maxNameWidth then 0
else terminalWidth - w1 - 1
(w1, w2)
}
def formatName(name: String) =
if name.length <= nameWidth then ("%-" + nameWidth + "s") format name
else (name + "\n%-" + nameWidth + "s") format ""
def formatDescription(text: String): String =
if descriptionWidth == 0 then text
else if text.length < descriptionWidth then text
else {
val inx = text.substring(0, descriptionWidth).lastIndexOf(" ")
if inx < 0 then text
else
val str = text.substring(0, inx)
s"${str}\n${formatName("")} ${formatDescription(text.substring(inx + 1))}"
}
def formatSetting(name: String, value: String) =
if (value.nonEmpty)
// the format here is helping to make empty padding and put the additional information exactly under the description.
s"\n${formatName("")} $name: $value."
else
""
def helpStr(s: Setting[?]) =
def defaultValue = s.default match
case _: Int | _: String => s.default.toString
case _ =>
// For now, skip the default values that do not make sense for the end user.
// For example 'false' for the version command.
""

def formatSetting(name: String, value: String) =
if (value.nonEmpty)
// the format here is helping to make empty padding and put the additional information exactly under the description.
s"\n${format("")} $name: $value."
else
""
s"${format(s.name)} ${s.description}${formatSetting("Default", defaultValue)}${formatSetting("Choices", s.legalChoices)}"

ss.map(helpStr).mkString("", "\n", s"\n${format("@<file>")} A text file containing compiler arguments (options and source files).\n")
s"${formatName(s.name)} ${formatDescription(s.description)}${formatSetting("Default", defaultValue)}${formatSetting("Choices", s.legalChoices)}"
ss.map(helpStr).mkString("", "\n", s"\n${formatName("@<file>")} ${formatDescription("A text file containing compiler arguments (options and source files).")}\n")

protected def shortUsage: String = s"Usage: $cmdName <options> <source files>"

Expand Down
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import Settings.Setting
trait CommonScalaSettings { self: Settings.SettingGroup =>
protected def defaultClasspath: String = sys.env.getOrElse("CLASSPATH", ".")

protected def defaultPageWidth: Int = {
val defaultWidth = 80
val columnsVar = System.getenv("COLUMNS")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux at least, this will not be set inside a shell script by default, the best portable way I've found to deal with that is to set it with:

if command -v tput >/dev/null 2>&1; then
    export COLUMNS="$(tput -Tdumb cols)"
fi

in https://github.com/lampepfl/dotty/blob/master/dist/bin/common (I checked and that doesn't impact performance).

Copy link
Contributor Author

@michelou michelou Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smarter I know that tput usage but I was hesitant to add it to the Scala 3 launch script. I'm fine if it's ok for you.

I suggest the following change:

if [ -z ${COLUMNS+x} ] && command -v tput >/dev/null 2>&1; then
    export COLUMNS="$(tput -Tdumb cols)"
fi

since it looks like running tput only once per session is enough to activate the automatic update of variable COLUMNS.

Can you please confirm the above behavior in your Unix environment ?!
For instance (on your command line, after executing the above command):

  1. $ set COLUMNS=
  2. $ echo $COLUMNS (should print the actual #columms in your terminal)
  3. Resize your terminal window.
  4. $ echo $COLUMNS (should print the updated #columms in your terminal)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will add your suggested code for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, looks like I forgot to answer here!

since it looks like running tput only once per session is enough to activate the automatic update of variable COLUMNS.

I think that the shell itself is responsible for updating $COLUMNS, if I'm in zsh or bash, $COLUMNS get updated automatically even if I reset it, but in dash for example it never gets updated.

if columnsVar != null then columnsVar.toInt
else if Properties.isWin then
val ansiconVar = System.getenv("ANSICON") // eg. "142x32766 (142x26)"
if ansiconVar != null && ansiconVar.matches("[0-9]+x.*") then
ansiconVar.substring(0, ansiconVar.indexOf("x")).toInt
else defaultWidth
else defaultWidth
}

/** Path related settings */
val bootclasspath: Setting[String] = PathSetting("-bootclasspath", "Override location of bootstrap class files.", Defaults.scalaBootClassPath, aliases = List("--boot-class-path"))
val extdirs: Setting[String] = PathSetting("-extdirs", "Override location of installed extensions.", Defaults.scalaExtDirs, aliases = List("--extension-directories"))
Expand All @@ -26,7 +38,7 @@ trait CommonScalaSettings { self: Settings.SettingGroup =>
val verbose: Setting[Boolean] = BooleanSetting("-verbose", "Output messages about what the compiler is doing.", aliases = List("--verbose"))
val version: Setting[Boolean] = BooleanSetting("-version", "Print product version and exit.", aliases = List("--version"))
val help: Setting[Boolean] = BooleanSetting("-help", "Print a synopsis of standard options.", aliases = List("--help"))
val pageWidth: Setting[Int] = IntSetting("-pagewidth", "Set page width", 80, aliases = List("--page-width"))
val pageWidth: Setting[Int] = IntSetting("-pagewidth", "Set page width", defaultPageWidth, aliases = List("--page-width"))
val silentWarnings: Setting[Boolean] = BooleanSetting("-nowarn", "Silence all warnings.", aliases = List("--no-warnings"))

/** Other settings */
Expand Down
6 changes: 6 additions & 0 deletions dist/bin/common
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ function onExit() {
trap onExit INT TERM EXIT

unset cygwin mingw msys darwin conemu

# COLUMNS is used together with command line option '-pageWidth'.
if command -v tput >/dev/null 2>&1; then
export COLUMNS="$(tput -Tdumb cols)"
fi

case "`uname`" in
CYGWIN*) cygwin=true
;;
Expand Down