Skip to content

Conversation

memeplex
Copy link
Contributor

@memeplex memeplex commented Jul 22, 2018

Fixes #1331

Reviewer Note: This PR introduces breaking changes to the text module. It should be merged in the merge window where breaking changes can be introduced for the next major release (generally directly before the next major release).

refactor(text): Make content a label (#1342)

This adds the `format` key to the text module with `<content>` as its 
only (label) tag.

This change is not backwards compatible for all configs that make use of 
the `-margin`, `-spacing`, `-prefix` or `-suffix` properties of 
`content`, because `content` is converted from a format to a label.
It would be otherwise because by default `format = <content>` and if 
only `content` is set, the module will behave as before.
This will allow users to use all the label formatting options in the 
text module as well.

Sample configuration:
[module/text]
type = custom/text
format = <content>
content = Content of the text module

Fixes #1331

@codecov-io
Copy link

codecov-io commented Jul 22, 2018

Codecov Report

Merging #1342 into master will increase coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1342      +/-   ##
=========================================
+ Coverage    5.94%   5.94%   +<.01%     
=========================================
  Files         161     161              
  Lines        8929    8928       -1     
=========================================
  Hits          531     531              
+ Misses       8398    8397       -1
Flag Coverage Δ
#unittests 5.94% <0%> (ø) ⬆️
Impacted Files Coverage Δ
include/modules/text.hpp 0% <ø> (ø) ⬆️
src/modules/text.cpp 3.44% <0%> (-0.13%) ⬇️
include/modules/meta/static_module.hpp 0% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4cb0b18...6bc932f. Read the comment docs.

@memeplex
Copy link
Contributor Author

memeplex commented Jul 22, 2018

I have a couple of questions about this:

  1. I don't really know what would be m_label->replace_token(" ", BUILDER_SPACE_TOKEN); for, I just adapted it from the previous code, maybe it's superfluous.

  2. Should I raise an error if the format is not exactly <content>? Or, less strictly, if the format doesn't contain <content> although it might not be exactly <content>? Or never rise an error, despite the contents of format?

@memeplex
Copy link
Contributor Author

Well, I ended up implementing build since it seems the best way to avoid assuming too much about what will be in format. Nevertheless, the user is not intended to modify the default <content> but in case he/she does it everything should still work fine if:

  1. The label content is defined
  2. The format format includes <content>

Anything else will be reported as an error.

@memeplex memeplex changed the title Make text module a label [wip] Make text module a label Jul 23, 2018
Copy link
Member

@patrick96 patrick96 left a comment

Choose a reason for hiding this comment

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

Good work. This does make the text module more consistent with the others.

There seems to be some legacy code in the current text module, I have added a few comments about it.

The rest looks good and it shouldn't break any existing configs (unless someone has already defined the format key in their text module).


string text_module::get_format() const {
return "content";
format->value = string_util::replace_all(format->value, " ", BUILDER_SPACE_TOKEN);
Copy link
Member

Choose a reason for hiding this comment

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

I have wondered about this before. I think we can remove it because the builder also does this.

m_formatter->add("content", "", {});

if (m_formatter->get("content")->value.empty()) {
throw module_error(name() + ".content is empty or undefined");
Copy link
Member

Choose a reason for hiding this comment

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

This looks like very old code. No other module actually does that anymore. I don't think this needs to be adapted and can be removed. What all the other modules do, is to load the format, as you did, and if the label tag occurs in the format string, load the label. For example the script modules does it like this:

modules/script.cpp:
-------------------
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
if (m_formatter->has(TAG_LABEL)) {
  m_label = load_optional_label(m_conf, name(), "label", "%output%");
}

This avoids throwing unnecessary errors because one should be able to also define a format without including any tags (even if it's not that useful).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In your last example

if (m_formatter->has(TAG_LABEL))

I'm unsure about removing the DEFAULT_FORMAT argument, as in:

if (m_formatter->has(TAG_CONTENT, DEFAULT_FORMAT))

Because the formatter could contain more than one format and the only API I found to check if a tag is inside a format is to request the formatter for that tag in one of its formats. But maybe the dafault format is considered when no other is explicitly passed?

Copy link
Member

Choose a reason for hiding this comment

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

From looking at the code (base.cpp:143), if you pass only one argument to module_formatter::has, then it searches through all the defined formats. So in this case it shouldn't matter, because the text module only defines a single format format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've already updated and rebased everything according to your comments.

@patrick96
Copy link
Member

I just realised that this breaks compatibility for all configs that use content-prefix, -suffix, -spacing, or -margin because right now content is a format and afterwards it would be a label, which doesn't have all those properties. We will keep this PR open and will merge it for the 4.0.0 release. This may be a while though.

@patrick96 patrick96 added this to the 4.0.0 milestone Jul 29, 2018
@memeplex
Copy link
Contributor Author

memeplex commented Jul 29, 2018 via email

patrick96 added a commit to patrick96/polybar that referenced this pull request Apr 4, 2022
patrick96 added a commit to patrick96/polybar that referenced this pull request Apr 4, 2022
patrick96 added a commit to patrick96/polybar that referenced this pull request Apr 4, 2022
patrick96 added a commit that referenced this pull request Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make text module a label
3 participants