Skip to content

Commit 2c4c5ca

Browse files
Exireldgw
andcommitted
docs: do it with style: interact with the bot
Co-authored-by: dgw <[email protected]>
1 parent f4d4724 commit 2c4c5ca

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

docs/source/_static/custom.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
.section dl.py:not(:first-of-type) {
22
margin-top: 1.5em;
33
}
4+
5+
.underline {
6+
text-decoration: underline;
7+
}
8+
9+
.strike {
10+
text-decoration: line-through;
11+
}
12+
13+
.bold {
14+
font-weight: bold;
15+
}
16+
17+
.italic {
18+
font-style: italic;
19+
}
20+
21+
.red {
22+
color: #f00;
23+
}
24+
25+
.spoiler {
26+
color: #000;
27+
background-color: #000;
28+
}

docs/source/plugin/bot.rst

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,125 @@ the :meth:`~sopel.bot.SopelWrapper.action` method::
9696
bot.action('does something')
9797

9898

99+
Do it with style
100+
================
101+
102+
.. Custom role definitions to apply custom style to inline text
103+
104+
.. role:: red
105+
:class: red
106+
107+
.. role:: boldred
108+
:class: bold red
109+
110+
.. role:: underline
111+
:class: underline
112+
113+
.. role:: strike
114+
:class: strike
115+
116+
.. role:: bolditalic
117+
:class: bold italic
118+
119+
.. role:: spoiler
120+
:class: spoiler
121+
122+
123+
When the bot talks, replies, or acts, it can do so with style: colors,
124+
**bold**, *italic*, :underline:`underline`, :strike:`strikethrough`, or
125+
``monospace``. IRC formatting works with control codes, bytes you can use to
126+
tell IRC clients how to display some part of the text.
127+
128+
.. seealso::
129+
130+
If you want to know more about IRC formatting in general and some of its
131+
limitations, `the modern IRC documentation`__ may be of interest to you.
132+
133+
.. __: https://modern.ircdocs.horse/formatting.html
134+
135+
However, dealing with control codes yourself is not the most dev-friendly
136+
approach, hence the :mod:`sopel.formatting` module. It contains various
137+
functions to help you create styled text.
138+
139+
Text styles
140+
-----------
141+
142+
Let's dive into examples, starting with :func:`~sopel.formatting.bold` text::
143+
144+
from sopel import formatting
145+
146+
bot.say(formatting.bold('This is some bold text!'))
147+
148+
This will output a line like this:
149+
150+
<Sopel> **This is some bold text!**
151+
152+
You can use them with Python string formatting::
153+
154+
emphasis = formatting.bold('important')
155+
bot.say('And here is the %s part.' % emphasis)
156+
157+
To get that kind of output:
158+
159+
<Sopel> And here is the **important** part.
160+
161+
And you can use multiple style functions together, for example with the
162+
:func:`~sopel.formatting.italic` function::
163+
164+
word = formatting.italic('very')
165+
emphasis = formatting.bold('%s important' % word)
166+
bot.say('And here is the %s part.' % emphasis)
167+
168+
To get a result that looks like this:
169+
170+
<Sopel> And here is the :bolditalic:`very` **important** part.
171+
172+
Colored styles
173+
--------------
174+
175+
Colorized text is a bit more complicated, and Sopel tries to provide helpful
176+
functions and constants for that: the :func:`~sopel.formatting.color` function
177+
and the :class:`~sopel.formatting.colors` class.
178+
179+
The ``color`` function takes a line of text and a foreground color. It also
180+
accepts an optional background color that uses the same color codes. The color
181+
codes are listed by the ``colors`` class, and can be used like this::
182+
183+
bot.say(formatting.color('Red text.', formatting.colors.RED))
184+
185+
The above example should produce this output:
186+
187+
<Sopel> :red:`Red text.`
188+
189+
You can combine colors and styles, like this::
190+
191+
big = formatting.color(
192+
formatting.bold('WARNING'), formatting.colors.RED)
193+
small = formatting.italic('warning')
194+
bot.say('[%s] This is a %s.' % (big, small))
195+
196+
So you get a similar result as:
197+
198+
<Sopel> [:boldred:`WARNING`] This is a *warning*.
199+
200+
If you want to prevent spoilers, you could be tempted to take advantage of
201+
the background color::
202+
203+
spoiler = formatting.color(
204+
'He was the killer.',
205+
formatting.colors.BLACK,
206+
formatting.colors.BLACK,
207+
)
208+
bot.say(spoiler)
209+
210+
And expect this (you need to select the text to read it):
211+
212+
<Sopel> :spoiler:`He was the killer.`
213+
214+
Note that not all combinations of foreground and background colors are happy
215+
ones, and you should be mindful of using too many unnecessary colors.
216+
217+
99218
Channels & users
100219
================
101220

0 commit comments

Comments
 (0)