diff options
author | Guilherme Carreiro <[email protected]> | 2024-10-18 01:12:21 +0200 |
---|---|---|
committer | git <[email protected]> | 2024-10-23 00:58:50 +0000 |
commit | e9ba6c2ea447f1325b6ec58c0f2a31bf81fa924e (patch) | |
tree | b7a4c9118a71286b47f4bebff7585c5e0d3db385 /lib/error_highlight | |
parent | e7c9dfb3e913162b7803195f77d1cb9c0cc2b9df (diff) |
[ruby/error_highlight] Adjust truncation, add opt-out mechanism, rename methods, and prepare error highlighting to render on extremely small screens
https://github.com/ruby/error_highlight/commit/c565340958
Diffstat (limited to 'lib/error_highlight')
-rw-r--r-- | lib/error_highlight/formatter.rb | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/lib/error_highlight/formatter.rb b/lib/error_highlight/formatter.rb index 4d5c82f460..0656a88ef2 100644 --- a/lib/error_highlight/formatter.rb +++ b/lib/error_highlight/formatter.rb @@ -1,5 +1,7 @@ module ErrorHighlight class DefaultFormatter + MIN_SNIPPET_WIDTH = 20 + def self.message_for(spot) # currently only a one-line code snippet is supported return "" unless spot[:first_lineno] == spot[:last_lineno] @@ -7,22 +9,24 @@ module ErrorHighlight snippet = spot[:snippet] first_column = spot[:first_column] last_column = spot[:last_column] + ellipsis = "..." # truncate snippet to fit in the viewport - if snippet.size > viewport_size - visible_start = [first_column - viewport_size / 2, 0].max - visible_end = visible_start + viewport_size + if snippet_max_width && snippet.size > snippet_max_width + available_width = snippet_max_width - ellipsis.size + center = first_column - snippet_max_width / 2 - # avoid centering the snippet when the error is at the end of the line - visible_start = snippet.size - viewport_size if visible_end > snippet.size + visible_start = last_column < available_width ? 0 : [center, 0].max + visible_end = visible_start + snippet_max_width + visible_start = snippet.size - snippet_max_width if visible_end > snippet.size - prefix = visible_start.positive? ? "..." : "" - suffix = visible_end < snippet.size ? "..." : "" + prefix = visible_start.positive? ? ellipsis : "" + suffix = visible_end < snippet.size ? ellipsis : "" snippet = prefix + snippet[(visible_start + prefix.size)...(visible_end - suffix.size)] + suffix snippet << "\n" unless snippet.end_with?("\n") - first_column = first_column - visible_start + first_column -= visible_start last_column = [last_column - visible_start, snippet.size - 1].min end @@ -32,18 +36,31 @@ module ErrorHighlight "\n\n#{ snippet }#{ marker }" end - def self.viewport_size - Ractor.current[:__error_highlight_viewport_size__] ||= terminal_columns + def self.snippet_max_width + return if Ractor.current[:__error_highlight_max_snippet_width__] == :disabled + + Ractor.current[:__error_highlight_max_snippet_width__] ||= terminal_width end - def self.viewport_size=(viewport_size) - Ractor.current[:__error_highlight_viewport_size__] = viewport_size + def self.snippet_max_width=(width) + return Ractor.current[:__error_highlight_max_snippet_width__] = :disabled if width.nil? + + width = width.to_i + + if width < MIN_SNIPPET_WIDTH + warn "'snippet_max_width' adjusted to minimum value of #{MIN_SNIPPET_WIDTH}." + width = MIN_SNIPPET_WIDTH + end + + Ractor.current[:__error_highlight_max_snippet_width__] = width end - def self.terminal_columns - # lazy load io/console, so it's not loaded when viewport_size is set + def self.terminal_width + # lazy load io/console, so it's not loaded when snippet_max_width is set require "io/console" - IO.console.winsize[1] + STDERR.winsize[1] if STDERR.tty? + rescue LoadError, NoMethodError, SystemCallError + # do not truncate when window size is not available end end |