-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Library: Port 'Text Colors' to Python #735
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Pango is a text layout library. It can e.g. be used for formatting text | ||
# https://gjs-docs.gnome.org/pango10~1.0/ | ||
import gi | ||
|
||
gi.require_version("Gtk", "4.0") | ||
gi.require_version("Pango", "1.0") | ||
from gi.repository import Gtk, Pango | ||
import workbench | ||
|
||
label: Gtk.Label = workbench.builder.get_object("label") | ||
|
||
|
||
def update_attributes(): | ||
# A Pango Attribute List is used to style the label | ||
label.set_attributes(rainbow_attributes(label.get_label())) | ||
|
||
|
||
# Generates an Attribute List that styles the label in rainbow colors. | ||
# The `text` parameter is needed to detect string length + position of spaces | ||
def rainbow_attributes(text): | ||
rainbow_colors = ( | ||
"#D00", | ||
"#C50", | ||
"#E90", | ||
"#090", | ||
"#24E", | ||
"#55E", | ||
"#C3C", | ||
) | ||
|
||
# Create a color array with the length needed to color all the letters | ||
color_array = [] | ||
i = 0 | ||
while i < len(text): | ||
color_array += rainbow_colors | ||
i = len(color_array) | ||
|
||
# Independent variable from `i` in the following loop to avoid spaces "consuming" a color | ||
color_idx = 0 | ||
|
||
attr_list_string = "" | ||
for i in range(len(text)): | ||
# Skip space characters | ||
if text[i] != " ": | ||
start_idx = i | ||
end_idx = i + 1 | ||
|
||
color = color_array[color_idx] | ||
color_idx += 1 | ||
# See comment below | ||
attr_list_string += f"{start_idx} {end_idx} foreground {color}," | ||
|
||
# For more info about the syntax for this function, see: | ||
# https://docs.gtk.org/Pango/method.AttrList.to_string.html | ||
return Pango.attr_list_from_string(attr_list_string) | ||
|
||
|
||
label.connect("notify::label", lambda _, __: update_attributes()) | ||
update_attributes() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifying the version for Pango is not necessary, there is only one version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one so far 😄 - there is a pango2 pull request by Matthias Clasen. Is this a problem in reality, specifying a version even if there is only one or just a personal preference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And what about Glib and Gio?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I wasn't aware Pango 2 was a thing. In that case we should keep this 👍
In general my comment here: #732 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also #738 - I'll think about / do some research if specifying the Pango version makes sense, but in doubt we keep it.