Skip to content

refactor: simplify terminal.text.width using luasystem 0.7.0 (#204)#206

Open
Gitkbc wants to merge 4 commits intolunarmodules:mainfrom
Gitkbc:refactor-ambiguous-width
Open

refactor: simplify terminal.text.width using luasystem 0.7.0 (#204)#206
Gitkbc wants to merge 4 commits intolunarmodules:mainfrom
Gitkbc:refactor-ambiguous-width

Conversation

@Gitkbc
Copy link

@Gitkbc Gitkbc commented Feb 24, 2026

Implements the simplification proposed in #204.

  • Introduce a single ambiguous-width calibration during
    terminal.initialize() (and preload_widths).
  • Remove per-character width caching and runtime probing logic.
  • Delegate width calculations to LuaSystem.
  • Preserve test and test_write APIs for compatibility.
  • Bump dependency to luasystem >= 0.7.0.

Fixes #204.

Remove per-character width cache and probe a single ambiguous-width
character during initialization. Store the measured value globally
and delegate width calculation to LuaSystem.

Preserve test and test_write APIs for compatibility.
Update documentation and rockspec dependency.
Copy link
Member

@Tieske Tieske left a comment

Choose a reason for hiding this comment

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

@Gitkbc I assume you created this PR as an intro into GSoC.

Seems like you had an AI friend create this PR. My suggestion would be to try and understand the issue at hand first, and then try again by writing the code yourself.

-- re-run to get the total width, since all widths are known now,
-- but this time do not write the string, just return the width
return M.test(str)
M.initialize()
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there is any utility left for this function. It was an optimization of the test function, in case you would also want the text to actually show.

-- character not in the cache will be passed to `system.utf8cwidth` to determine the width.
--- Character display width helpers.
-- Uses LuaSystem width calculations with an optional calibrated
-- ambiguous-width value for terminal-specific behavior.
Copy link
Member

Choose a reason for hiding this comment

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

can we leave some more background in the doc comments? the cache part should go, that's clear, but the ambiguous explanation is still useful I think



-- returns a string with all box_fmt characters, to pre-load the character width cache
-- returns a string with all box_fmt characters
Copy link
Member

Choose a reason for hiding this comment

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

this has no utility anymore, it was only here to be able to test the default used characters

-- @within Initialization
function M.preload_widths(str)
text.width.test((str or "") .. M.progress._spinner_fmt_chars() .. M.draw._box_fmt_chars())
if str then
Copy link
Member

Choose a reason for hiding this comment

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

this function also has no utility anymore

sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) - sys.CIF_PROCESSED_INPUT)
end

text.width.initialize(true)
Copy link
Member

Choose a reason for hiding this comment

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

testing should be optional. The default can be to test, but the user should be able to skip it. In case of a pipe-out, instead of a terminal, write the test characters would break the output.

local ambiguous_char = "·"
local ambiguous_codepoint = utf8.codepoint(ambiguous_char)

M.ambiguous_width = nil
Copy link
Member

Choose a reason for hiding this comment

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

I think it is save to set a default here and document it. Default should be 1.



local function detect_ambiguous_width()
local row, col = t.cursor.position.get()
Copy link
Member

Choose a reason for hiding this comment

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

should we use istty() here to see if we have a terminal?

local query = ambiguous_char .. t.cursor.position.query_seq() .. setpos

t.text.stack.push({ brightness = 0 })
local result, err = t.input.query(query, "^\27%[(%d+);(%d+)R$")
Copy link
Member

Choose a reason for hiding this comment

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

don't hardcode the sequence, use this library's functions

@Gitkbc
Copy link
Author

Gitkbc commented Feb 24, 2026

Thanks for the detailed review and the clear pointers, @Tieske.

You're right — the initial submission didn’t fully align with the project’s design and conventions. I’ll rework the implementation to address each of your points and ensure it matches the existing architecture and standards.

I’ll push a revised version shortly.

Appreciate the thorough feedback.

@Gitkbc
Copy link
Author

Gitkbc commented Feb 26, 2026

Addressed the requested changes:
1)Simplified terminal.text.width to delegate to LuaSystem (>= 0.7.0)
2)Removed width caching and preload mechanisms
3)Introduced optional ambiguous-width calibration during initialize()
4)Restored terminal.utils export to preserve the public API
5)All specs passing locally (851 tests, 0 failures).

Please let me know if any further adjustments are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix: update display with handling

2 participants