Skip to content
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

Deviate from ruby/pretty_print and enrich the API #57

Merged
merged 23 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4ce4648
wadler: accept indent kwarg use by default in nest and group
stackmystack Dec 31, 2024
fc325ce
wadler: accept base_indent kwarg
stackmystack Dec 31, 2024
f5ec8f2
wadler: use delim kwarg instead of positional args
stackmystack Dec 31, 2024
b2fa1d5
bin: repl: change ascii art
stackmystack Dec 31, 2024
7d3b32f
wadler: introduce consistent and inconsistent calls
stackmystack Dec 31, 2024
2dae1e7
wadler: remove unnecessary args for group_close
stackmystack Dec 31, 2024
361f4a3
wadler: make all printing methods return self
stackmystack Dec 31, 2024
d3e0ef3
wdaler: add do method to avoid breaking call chains
stackmystack Dec 31, 2024
2a8ffe8
wadler: normalize group_open and accept a symbol break_type
stackmystack Dec 31, 2024
51c1532
wadler: add {#separate}
stackmystack Dec 31, 2024
272da62
wadler: rename ctor param space to space_gen
stackmystack Dec 31, 2024
695244c
wadler: add {#space} to generate a literal space
stackmystack Dec 31, 2024
1629d5b
wadler: add {#surround} as a glorified group
stackmystack Jan 3, 2025
470a060
wadler: add {#lines} and {#concat} as wrappers for {#surround}
stackmystack Jan 6, 2025
ca62905
wadler: add some helpers based on surround
stackmystack Jan 6, 2025
8af37ae
wadler: allow {#separate}'s indent param to be an Integer
Jan 7, 2025
4f911d7
test: refactor surround and separate tests
Jan 7, 2025
42bc83c
wadler: always add parent group before starting print
Jan 7, 2025
30cf122
fix: wadler groups aliases
Jan 7, 2025
a10bec7
doc: fix examples results
Jan 7, 2025
9a48652
examples: make compliant to new api
Jan 7, 2025
be00905
ci: run examples to check if they break
Jan 7, 2025
60a44e3
examples: add entries for surround and separate
Jan 8, 2025
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
run: just lint
- name: test
run: just test
- name: examples
run: just examples
- name: doc
run: just doc
- name: package gem
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Style/Lambda:
Style/LambdaCall:
EnforcedStyle: braces

Style/MultilineBlockChain:
Enabled: false

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma

Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

## [unreleased]

- Deprarture from the `ruby/pretty_print` API with Wadler:
- accept `indent` which will apply to `nest` and `group` implicitly if no
`indent:` kwarg is passed.
- accept `base_indent` which sets the base global indentation level for
the whole printer.
- all printing methods return `self` for call chaining.
- Wadler:
- rename ctor param `space` to `space_gen`.
- add `consistent` and `inconsistent` as shorthands to
`group(:consistent)` and `group(:inconsistent)`, respectively.
- remove args of `group_close`.
- add `do` to avoid braking call chains.
- `group_open` accepts a symbol `break_type` instead of a boolean.
- add `separate` to separate a list of items.
- add `space` to generate a literal space.
- add `surround` as a glorified `group` that add breakables next to delimiters.

## v0.9.8 (30-12-2024)

- Oppen now supports Ruby 3.0+. It used to be restricted to Ruby 3.2+.
Expand Down
17 changes: 6 additions & 11 deletions bin/repl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
require 'colored'

# Fancy
WELCOME = <<-'YGM'
__ __ ___ __ __ ____ ____ ___ ___ ___ ____ ____ ___ ___ ___
| | | / \ | | || \ / | / _]| | || \ / || | | / _]
| | || || | || D )| __| / [_ | _ _ || _ || o || _ _ | / [_
| ~ || O || | || / | | || _]| \_/ || | || || \_/ || _]
|___, || || : || \ | |_ || [_ | | || | || _ || | || [_
| || || || . \| ______ ______ ______ __ || |
|____/ \___/ \__,_||__|\_||___ /\ == \ /\ ___\ /\ == \ /\ \ ___||_____|
\ \ __< \ \ __\ \ \ _-/ \ \ \____
\ \_\ \_\ \ \_____\ \ \_\ \ \_____\
\/_/ /_/ \/_____/ \/_/ \/_____/
WELCOME = <<~YGM
___ ___ ___ ___ ___
| . | . | . | -_| |
|___| _| _|___|_|_|
|_| |_|

YGM

puts WELCOME.green
2 changes: 1 addition & 1 deletion examples/configs/eager_print.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
title 'With eager printing:'
puts printer_with_config.output
# abc defghi
# jkl
# jkl

puts ''

Expand Down
6 changes: 4 additions & 2 deletions examples/configs/indent_anchor.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
printer_with_config =
Oppen::Wadler.new(
config: Oppen::Config.new(indent_anchor: :current_offset),
indent: 4,
width: 13,
)
printer_no_config =
Oppen::Wadler.new(
config: Oppen::Config.new(indent_anchor: :end_of_previous_line),
indent: 4,
width: 13,
)
test_block = ->(printer) {
printer.text 'And she said:'
printer.group(4) {
printer.group(4) {
printer.group {
printer.group {
printer.break
printer.text 'Hello, World!'
}
Expand Down
Empty file modified examples/configs/trim_trailing_whitespaces.rb
100644 → 100755
Empty file.
Empty file modified examples/configs/upsize_stack.rb
100644 → 100755
Empty file.
Empty file modified examples/oppen/oppen.rb
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions examples/oppen_and_wadler_customization/new_line.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# The new line String can be specified using the `new_line` parameter.
new_line = '<br>'

printer = Oppen::Wadler.new(new_line: new_line)
printer = Oppen::Wadler.new(indent: 2, new_line: new_line)
stackmystack marked this conversation as resolved.
Show resolved Hide resolved

printer.group(2) {
printer.group {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
Expand Down
6 changes: 3 additions & 3 deletions examples/oppen_and_wadler_customization/space.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
# By using a callable:
space = ->(n) { '---' * n }

printer = Oppen::Wadler.new(space: space)
printer = Oppen::Wadler.new(indent: 2, space_gen: space)

printer.group(2) {
printer.group {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
printer.group(2) {
printer.group {
printer.break
printer.text 'I am fine'
}
Expand Down
4 changes: 2 additions & 2 deletions examples/oppen_and_wadler_customization/whitespace.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# to prevent trailing whitespaces can be specified using the `whitespace` parameter.
whitespace = '**'

printer = Oppen::Wadler.new(whitespace: whitespace)
printer = Oppen::Wadler.new(indent: 2, whitespace: whitespace)
stackmystack marked this conversation as resolved.
Show resolved Hide resolved

printer.group(2) {
printer.group {
printer.text '******Hello, World!******'
printer.break
}
Expand Down
6 changes: 3 additions & 3 deletions examples/oppen_and_wadler_customization/width.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# The maximum width can be specified using the `width` parameter.
width = 5

printer_width_default = Oppen::Wadler.new
printer_width_narrow = Oppen::Wadler.new(width: width)
printer_width_default = Oppen::Wadler.new(indent: 2)
printer_width_narrow = Oppen::Wadler.new(indent: 2, width: width)
Comment on lines +8 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto.

test_block = ->(printer) {
printer.group(2) {
printer.group {
printer.text 'Hello, World!'
printer.breakable
printer.text 'How are you doing?'
Expand Down
16 changes: 9 additions & 7 deletions examples/show_print_commands/show_print_commands.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

printer = Oppen::Wadler.new

printer.group(2) {
printer.group(indent: 2) {
printer.text 'Hello, World!'
printer.nest(4) {
printer.nest(indent: 4) {
printer.break
printer.text 'GoodBye, World!'
}
}

puts printer.show_print_commands
# out.group(2, "", "", :consistent) {
# out.text("Hello, World!", width: 13)
# out.nest(4, "", "") {
# out.break(line_continuation: "")
# out.group(:consistent, indent: 0) {
# out.group(:consistent, indent: 2) {
# out.text("Hello, World!", width: 13)
# out.nest(indent: 4) {
# out.break(line_continuation: "")
# }
# out.text("GoodBye, World!", width: 15)
# }
# out.text("GoodBye, World!", width: 15)
# }
Empty file modified examples/wadler_break_and_breakable/break.rb
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/wadler_break_and_breakable/breakable.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
printer = Oppen::Wadler.new(width: 40)

# See `examples/wadler_group/inconsistent.rb` for more infos about `:inconsistent`
printer.group(0, '', '', :inconsistent) {
printer.group(:inconsistent) {
printer.text 'Hello, World!'
printer.breakable
printer.text '(still fits on the line)'
Expand Down
Empty file modified examples/wadler_break_and_breakable/line_continuation.rb
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion examples/wadler_group/consistent.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
printer = Oppen::Wadler.new(width: 999_999)

# Groups are consistent by default.
printer.group {
printer.consistent { # alias for printer.group(:consistent) and same as printer.group
printer.text 'Hello, World!'
printer.breakable
printer.text 'How are you doing?'
Expand Down
5 changes: 3 additions & 2 deletions examples/wadler_group/delimiters.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require_relative '../helper'

printer = Oppen::Wadler.new(width: 999_999)
printer = Oppen::Wadler.new(indent: 2, width: 999_999)

# Groups have no delimiters by default.
printer.group(2, '<<', '>>') {
printer.group(delim: %w[<< >>]) {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
Expand All @@ -16,6 +16,7 @@
}

puts printer.output
#
# <<Hello, World!
# How are you doing?
# I am fine, thanks.
Expand Down
2 changes: 1 addition & 1 deletion examples/wadler_group/inconsistent.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

printer = Oppen::Wadler.new(width: 999_999)

printer.group(0, '', '', :inconsistent) {
printer.inconsistent { # alias for printer.group(:inconsistent)
printer.text 'Hello, World!'
printer.breakable
printer.text 'How are you doing?'
Expand Down
4 changes: 2 additions & 2 deletions examples/wadler_group/indentation.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require_relative '../helper'

printer = Oppen::Wadler.new(width: 999_999)
printer = Oppen::Wadler.new(indent: 4, width: 999_999)

# Groups have an indentation of 0 by default.
printer.group(4) {
printer.group {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
Expand Down
4 changes: 2 additions & 2 deletions examples/wadler_nest/delimiters.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require_relative '../helper'

printer = Oppen::Wadler.new(width: 999_999)
printer = Oppen::Wadler.new(indent: 2, width: 999_999)

# Nests have no delimiters by default.
printer.nest(2, '<<', '>>') {
printer.nest(delim: %w[<< >>]) {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
Expand Down
4 changes: 2 additions & 2 deletions examples/wadler_nest/indentation.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

require_relative '../helper'

printer = Oppen::Wadler.new(width: 999_999)
printer = Oppen::Wadler.new(indent: 4, width: 999_999)

printer.nest(4) {
printer.nest {
printer.text 'Hello, World!'
printer.break
printer.text 'How are you doing?'
Expand Down
17 changes: 17 additions & 0 deletions examples/wadler_utils/separate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative '../helper'

# You might want to also take a look at the `lines` and `concat` methods.

printer = Oppen::Wadler.new(width: 10)

printer.separate((1..10).map(&:to_s), ',', break_type: :inconsistent, indent: 2) { |i|
printer.text i
}

puts printer.output
# 1, 2, 3,
# 4, 5, 6,
# 7, 8, 9,
# 10
16 changes: 16 additions & 0 deletions examples/wadler_utils/surround.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative '../helper'

# You might want to also take a look at the `parens`, `parens_break_both`, `angles`, ... methods.

printer = Oppen::Wadler.new(width: 10)

printer.surround('<<', '>>', indent: 2, lft_force_break: true, rgt_force_break: true) {
printer.text '42'
}

puts printer.output
# <<
# 42
# >>
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ doc:
doc-stats:
bundle exec yard stats --list-undoc

[group('test')]
examples:
find examples/ -mindepth 2 -type f -name "*.rb" | while IFS= read -r file; do \
bundle exec ruby "$file" > /dev/null || exit 1; \
done

[group('publish')]
gem:
mkdir -p {{PKG_OUT}}
Expand Down
10 changes: 5 additions & 5 deletions lib/oppen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Config
# config = Oppen::Config.new(indent_anchor: :end_of_previous_line)
# out = Oppen::Wadler.new config:, width: 13
# out.text 'And she said:'
# out.group(4) {
# out.group(4) {
# out.group(indent: 4) {
# out.group(indent: 4) {
# out.break
# out.text 'Hello, World!'
# }
Expand All @@ -80,8 +80,8 @@ class Config
# config = Oppen::Config.new(indent_anchor: :current_offset)
# out = Oppen::Wadler.new config:, width: 13
# out.text 'And she said:'
# out.group(4) {
# out.group(4) {
# out.group(indent: 4) {
# out.group(indent: 4) {
# out.break
# out.text 'Hello, World!'
# }
Expand Down Expand Up @@ -123,7 +123,7 @@ def initialize(eager_print: false, indent_anchor: :end_of_previous_line,
# #
# # eager_print: true =>
# # abc defghi
# # jkl
# # jkl
#
# @return [Boolean]
def eager_print? = @eager_print
Expand Down
Loading
Loading