Skip to content

Fix column annotation alignment for CJK and fullwidth characters#315

Merged
drwl merged 1 commit intodrwl:mainfrom
SergeyGildenshtern:fix/double-width-alignment
Feb 19, 2026
Merged

Fix column annotation alignment for CJK and fullwidth characters#315
drwl merged 1 commit intodrwl:mainfrom
SergeyGildenshtern:fix/double-width-alignment

Conversation

@SergeyGildenshtern
Copy link
Contributor

Summary

This PR fixes incorrect column alignment in generated annotations when column names or comments contain fullwidth or CJK characters.

Previously, the width calculation relied on:

elem.bytesize == 3

This approach is incorrect because UTF-8 byte size does not correspond to terminal display width. As a result, annotations could become misaligned when using certain Unicode characters.

This PR replaces the byte-size heuristic with a simplified Unicode range-based implementation that accounts for characters typically rendered as double-width in monospace terminals (CJK and fullwidth forms).

Problem

When using certain Unicode characters in column comments (e.g. currency symbols), annotation formatting could shift and break alignment.

Example case:

# frozen_string_literal: true

class TestMigration < ActiveRecord::Migration[8.0]
  def up
    create_table :payments do |t|
      t.decimal :value, null: false, comment: 'The amount(€) of payment'
      t.datetime :created_at, null: false
    end
  end
end

Before this change, alignment could be inconsistent depending on the characters used.

Before (broken alignment)

Снимок экрана 2026-02-18 в 19 48 57

After (correct alignment)

Снимок экрана 2026-02-18 в 19 50 15

Limitations

This remains a simplified wcwidth implementation and does not support:

  • Emoji
  • Grapheme clusters (e.g. family emoji sequences)
  • Combining marks
  • Zero-width joiners
  • East Asian ambiguous-width characters

If full Unicode terminal-width compliance is ever required, the implementation could be replaced with a dedicated library such as unicode-display_width.

Copy link
Owner

@drwl drwl left a comment

Choose a reason for hiding this comment

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

Thanks for adding this + the write up. As a person that uses mostly ascii characters, having other people help with unicode is greatly appreciated 👍

@drwl drwl merged commit c8925f9 into drwl:main Feb 19, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants