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

Add IntRange type for ColorIndex #4618

Merged
merged 4 commits into from
Jul 30, 2023
Merged

Conversation

Tyriar
Copy link
Member

@Tyriar Tyriar commented Jul 28, 2023

Fixes #4614

@Tyriar Tyriar added this to the 5.3.0 milestone Jul 28, 2023
@Tyriar Tyriar requested a review from jerch July 28, 2023 20:52
@Tyriar Tyriar self-assigned this Jul 28, 2023
Comment on lines 3433 to 3435
function isValidColorIndex(value: number): value is ColorIndex {
return 0 <= value && value < 259;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@jerch should this be < 256 instead? If so what would be a good name for this color index sub-range? IsValidInputColorIndex or something?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, for colors the index is only 0..255, so its better to test for that range in during input. The fg/bg/cursor entries on top are an amalgamation, so the events and methods dont need re-declaration and/or branching.

: Enumerate<N, [...Acc, Acc['length']]>;
type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>;

type ColorIndex = IntRange<0, 259>; // number from 0 to 258
Copy link
Member

Choose a reason for hiding this comment

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

Would it be clearer to limit ColorIndex here to 0..255, and to merge into a super type with the fg/bg/cursor values in a second step? Also the test function above can then refer to that narrower ColorIndex type.

@Tyriar Tyriar requested a review from jerch July 28, 2023 23:34
Copy link
Member

@jerch jerch left a comment

Choose a reason for hiding this comment

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

LGTM, beside the left-over doubled change check below.

src/common/InputHandler.ts Outdated Show resolved Hide resolved
src/common/InputHandler.ts Outdated Show resolved Hide resolved
@Tyriar Tyriar enabled auto-merge July 30, 2023 13:56
@Tyriar Tyriar merged commit 25b266e into xtermjs:master Jul 30, 2023
8 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.

Avoid as ColorIndex in src/common/InputHandler.test.ts
2 participants