1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?xml version="1.0"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<refentry id="cockpit-util">
<refnamediv>
<refname>cockpit.js: Utilities</refname>
<refpurpose>Various utility functions</refpurpose>
</refnamediv>
<refsection id="cockpit-format">
<title>cockpit.format()</title>
<programlisting>
string = cockpit.format(template, args)
string = cockpit.format(template, [arg, ...])
</programlisting>
<para>Format a string interpolating <code>args</code> into <code>template</code> using
shell like syntax. The <code>args</code> may be either an array or javascript object.
The <code>template</code> can contain fields that look like <code>$name</code> or
<code>${name}</code> or <code>$0</code>. Numeric fields are used with array
<code>args</code> and start at zero.</para>
<para>In the second form, multiple <code>arg</code> arguments may be passed directly,
and interpolated as as numeric fields in the <code>template</code>.</para>
<para>All falsy arguments except the numbers <code>0</code> and <code>0.0</code>are
replaced by an empty string.</para>
</refsection>
<refsection id="cockpit-format-number">
<title>cockpit.format_number()</title>
<programlisting>
string = cockpit.format_number(number, [precision])
</programlisting>
<para>Formats <code>number</code> into a displayable <code>string</code>. If the number is not
an integer, it is rounded to the given number of decimal places, defaulting to 3. If the number
is near zero, but not quite zero it is rounded to the smallest non-zero value of the given precision;
i.e. ±0.001 for default precision 3.</para>
<para>If <code>number</code> is <code>null</code> or <code>undefined</code> an empty string
will be returned.</para>
</refsection>
<refsection id="cockpit-format-bytes">
<title>cockpit.format_bytes()</title>
<programlisting>
string = cockpit.format_bytes(number, [options])
</programlisting>
<para>Formats <code>number</code> into a displayable <code>string</code> with a suffix, such as
<emphasis>kB</emphasis> or <emphasis>MB</emphasis>.</para>
<para>By default, SI units are used. IEC units (1024-based) can be requested by including
<code>base2: true</code> in <code>options</code>.</para>
<para>By default, non-integer numbers will be formatted with 3 digits of precision. This can be changed
with <code>options.precision</code>.</para>
<para>If <code>number</code> is <code>null</code> or <code>undefined</code> an empty string will be
returned.</para>
</refsection>
<refsection id="cockpit-format-bytes-per-sec">
<title>cockpit.format_bytes_per_sec()</title>
<programlisting>
string = cockpit.format_bytes_per_sec(number, [options])
</programlisting>
<para>Format <code>number</code> of bytes into a displayable speed <code>string</code>.</para>
<para>This function is mostly equivalent to <code>cockpit.format_bytes()</code> but the returned
value contains a unit like <emphasis>kB/s</emphasis> or <emphasis>MB/s</emphasis>.</para>
</refsection>
<refsection id="cockpit-format-bits-per-sec">
<title>cockpit.format_bits_per_sec()</title>
<programlisting>
string = cockpit.format_bits_per_sec(number, [options])
</programlisting>
<para>Format <code>number</code> of bits into a displayable speed <code>string</code>.</para>
<para>This function is mostly equivalent to <code>cockpit.format_bytes()</code> but the returned
value contains a unit like <emphasis>kbps</emphasis> or <emphasis>Mbps</emphasis>.</para>
<para>This function does not support IEC units. <code>base2</code> may not be passed as part of
<code>options</code>.</para>
</refsection>
<refsection id="cockpit-init">
<title>cockpit.init()</title>
<programlisting>
await cockpit.init();
cockpit.init().then(() => { ... });
</programlisting>
<para>Requests initialization of the Cockpit client library. This will ensure that the transport is
connected and we are ready to create channels. It also populates the <code>cockpit.info</code>
field.</para>
<para>This function returns a promise. Initialization isn't complete until the promise has resolved. You
can either <code>await</code> it or call <code>.then()</code> on it.</para>
</refsection>
<refsection id="cockpit-info">
<title>cockpit.info</title>
<programlisting>
cockpit.info.channels[payload]
cockpit.info.os_release[field]
cockpit.info.user
cockpit.info.ws.version
</programlisting>
<para>This object contains information about Cockpit itself. It is only available after cockpit.init() has
been called and awaited.</para>
<variablelist>
<varlistentry>
<term><code>channels</code></term>
<listitem><para>This is a mapping of channel payload types (keys, strings) supported by the bridge to
capabilities advertised by those channels (values, lists of strings). Channels are listed even if they
don't advertise any capabilities, making this useful as a way to determine which channel types are
supported by the bridge.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>os_release</code></term>
<listitem><para>This is the data from the <filename>/etc/os-release</filename> or
<filename>/usr/lib/os-release</filename> on the system that the bridge is running on. It is a mapping
from the key names to their values. See the <ulink
url="https://www.freedesktop.org/software/systemd/man/latest/os-release.html">os-release
Specification</ulink> for information about the available keys.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>user</code></term>
<listitem>
<para>Contains information about the user we're logged in as.</para>
<variablelist>
<varlistentry>
<term><code>uid</code></term>
<listitem><para>This is unix user id as an integer.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>gid</code></term>
<listitem><para>This is unix user group id as an integer.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>name</code></term>
<listitem><para>This is the unix user name like <code>"root"</code>.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>fullname</code></term>
<listitem><para>This is a readable name for the user, from the GECOS field.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>group</code></term>
<listitem><para>This is the primary group name of the user.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>groups</code></term>
<listitem><para>This is an array of group names to which the user belongs. The first item in this
list is the primary group.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>home</code></term>
<listitem><para>This is user's home directory.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>shell</code></term>
<listitem><para>This is unix user shell.</para></listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term><code>ws</code></term>
<listitem>
<para>Contains information about the webserver Cockpit is being served with.</para>
<variablelist>
<varlistentry>
<term><code>version</code></term>
<listitem><para>The version of the webserver.</para></listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
</variablelist>
</refsection>
<refsection id="cockpit-event-target">
<title>cockpit.event_target</title>
<programlisting>
cockpit.event_target(object, [handlers])
</programlisting>
<para>Adds an
<ulink url="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget">EventTarget</ulink>
implementation to the <code>object</code>. Optionally store the handlers in <code>handlers</code>
if its specified.</para>
</refsection>
</refentry>
|