Skip to content

Commit 62f2f38

Browse files
[#48] Add color support
1 parent b3c10b7 commit 62f2f38

5 files changed

Lines changed: 161 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ high-resolution loss-less files (44.1kHz+/16bit+).
1111
* Digital Stream Digital (DSD) over Pulse Code Modulation (PCM) (DoP) 64/128/256 (Sony .dsf / Philips .dff)
1212
* FLAC (44.1kHz/16bit, 48kHz/16bit, 88.2kHz/16bit, 96kHz/16bit, 176.4kHz/16bit, 192kHz/16bit, 352.8kHz/16bit, 384kHz/16bit, 44.1kHz/24bit, 48kHz/24bit, 88.2kHz/24bit, 96kHz/24bit, 176.4kHz/24bit, 192kHz/24bit, 352.8kHz/24bit, 384kHz/24bit)
1313
* WAV (44.1kHz/16bit, 48kHz/16bit, 88.2kHz/16bit, 96kHz/16bit, 176.4kHz/16bit, 192kHz/16bit, 352.8kHz/16bit, 384kHz/16bit, 44.1kHz/24bit, 48kHz/24bit, 88.2kHz/24bit, 96kHz/24bit, 176.4kHz/24bit, 192kHz/24bit, 352.8kHz/24bit, 384kHz/24bit)
14-
* Customize console output
14+
* Customize console output with color support
1515
* By-pass PulseAudio for native bit-stream
1616

1717
See [Getting Started](./doc/GETTING_STARTED.md) on how to get started with `hrmp`.

doc/CONFIGURATION.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,53 @@ The `Bool` data type supports the following values: `on`, `1`, `true`, `off`, `0
3636
| device | | String | Yes | The device address |
3737
| description | | String | No | The description of the device |
3838
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
39+
40+
## Console output
41+
42+
The default console output format is
43+
44+
```
45+
[%n/%N] %d: %f [%i] (%t/%T) (%p)
46+
```
47+
48+
The expansions supported are
49+
50+
| Expansion | Description |
51+
| :-------- | :---------- |
52+
| `%n` | Current track number |
53+
| `%N` | Total number of tracks |
54+
| `%d` | Device name |
55+
| `%f` | File name |
56+
| `%F` | Full path of the file |
57+
| `%i` | File information |
58+
| `%t` | Current time |
59+
| `%T` | Total time |
60+
| `%p` | Percentage |
61+
62+
The console also supports color using the ANSI color codes
63+
64+
| Code | Description |
65+
| :-------- | :---------- |
66+
| `\e[0m` | No Color |
67+
| `\e[0;30m`| Black |
68+
| `\e[1;30m`| Gray |
69+
| `\e[0;31m`| Red |
70+
| `\e[1;31m`| Light red |
71+
| `\e[0;32m`| Green |
72+
| `\e[1;32m`| Light green |
73+
| `\e[0;33m`| Brown |
74+
| `\e[1;33m`| Yellow |
75+
| `\e[0;34m`| Blue |
76+
| `\e[1;34m`| Light blue |
77+
| `\e[0;35m`| Purple |
78+
| `\e[1;35m`| Light purple |
79+
| `\e[0;36m`| Cyan |
80+
| `\e[1;36m`| Light cyan |
81+
| `\e[0;37m`| Light gray |
82+
| `\e[1;37m`| White |
83+
84+
An example
85+
86+
```
87+
output=[\e[1;32m%t\e[0m/\e[0;32m%T\e[0m] \e[1;37m%f\e[0m
88+
```

doc/manual/01-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Digital Stream Digital (DSD) over Pulse Code Modulation (PCM) (DoP) 64/128/256 (Sony .dsf / Philips .dff)
1111
* FLAC (44.1kHz/16bit, 48kHz/16bit, 88.2kHz/16bit, 96kHz/16bit, 176.4kHz/16bit, 192kHz/16bit, 352.8kHz/16bit, 384kHz/16bit, 44.1kHz/24bit, 48kHz/24bit, 88.2kHz/24bit, 96kHz/24bit, 176.4kHz/24bit, 192kHz/24bit, 352.8kHz/24bit, 384kHz/24bit)
1212
* WAV (44.1kHz/16bit, 48kHz/16bit, 88.2kHz/16bit, 96kHz/16bit, 176.4kHz/16bit, 192kHz/16bit, 352.8kHz/16bit, 384kHz/16bit, 44.1kHz/24bit, 48kHz/24bit, 88.2kHz/24bit, 96kHz/24bit, 176.4kHz/24bit, 192kHz/24bit, 352.8kHz/24bit, 384kHz/24bit)
13-
* Customize console output
13+
* Customize console output with color support
1414
* By-pass PulseAudio for native bit-stream
1515

1616
### Best effort

doc/manual/03-configuration.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,53 @@ The `Bool` data type supports the following values: `on`, `yes`, `1`, `true`, `o
4141
| device | | String | Yes | The device address |
4242
| description | | String | No | The description of the device |
4343
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
44+
45+
## Console output
46+
47+
The default console output format is
48+
49+
```
50+
[%n/%N] %d: %f [%i] (%t/%T) (%p)
51+
```
52+
53+
The expansions supported are
54+
55+
| Expansion | Description |
56+
| :-------- | :---------- |
57+
| `%n` | Current track number |
58+
| `%N` | Total number of tracks |
59+
| `%d` | Device name |
60+
| `%f` | File name |
61+
| `%F` | Full path of the file |
62+
| `%i` | File information |
63+
| `%t` | Current time |
64+
| `%T` | Total time |
65+
| `%p` | Percentage |
66+
67+
The console also supports color using the ANSI color codes
68+
69+
| Code | Description |
70+
| :-------- | :---------- |
71+
| `\e[0m` | No Color |
72+
| `\e[0;30m`| Black |
73+
| `\e[1;30m`| Gray |
74+
| `\e[0;31m`| Red |
75+
| `\e[1;31m`| Light red |
76+
| `\e[0;32m`| Green |
77+
| `\e[1;32m`| Light green |
78+
| `\e[0;33m`| Brown |
79+
| `\e[1;33m`| Yellow |
80+
| `\e[0;34m`| Blue |
81+
| `\e[1;34m`| Light blue |
82+
| `\e[0;35m`| Purple |
83+
| `\e[1;35m`| Light purple |
84+
| `\e[0;36m`| Cyan |
85+
| `\e[1;36m`| Light cyan |
86+
| `\e[0;37m`| Light gray |
87+
| `\e[1;37m`| White |
88+
89+
An example
90+
91+
```
92+
output=[\e[1;32m%t\e[0m/\e[0;32m%T\e[0m] \e[1;37m%f\e[0m
93+
```

src/libhrmp/playback.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,16 @@ format_output(struct playback* pb)
10061006

10071007
out = hrmp_append(out, "\r");
10081008

1009-
for (const char* p = fmt; p && *p; ++p)
1009+
size_t len = strlen(fmt);
1010+
1011+
for (size_t i = 0; i < len; ++i)
10101012
{
1011-
if (*p == '%' && *(p + 1) != '\0')
1013+
char c = fmt[i];
1014+
1015+
if (c == '%' && (i + 1) < len)
10121016
{
1013-
++p;
1014-
switch (*p)
1017+
++i;
1018+
switch (fmt[i])
10151019
{
10161020
case 'n':
10171021
out = hrmp_append_int(out, pb->file_number);
@@ -1095,13 +1099,62 @@ format_output(struct playback* pb)
10951099
break;
10961100
default:
10971101
out = hrmp_append_char(out, '%');
1098-
out = hrmp_append_char(out, *p);
1102+
out = hrmp_append_char(out, fmt[i]);
10991103
break;
11001104
}
11011105
}
1106+
else if (c == '\\' && (i + 1) < len)
1107+
{
1108+
char n = fmt[i + 1];
1109+
1110+
if (n == '0' && (i + 3) < len && fmt[i + 2] == '3' && fmt[i + 3] == '3')
1111+
{
1112+
/* Octal escape for ESC: \033 */
1113+
out = hrmp_append_char(out, '\x1b');
1114+
i += 3;
1115+
}
1116+
else if ((n == 'x' || n == 'X') &&
1117+
(i + 3) < len && (fmt[i + 2] == '1' && (fmt[i + 3] == 'b' || fmt[i + 3] == 'B')))
1118+
{
1119+
/* Hex escape for ESC: \x1b or \x1B */
1120+
out = hrmp_append_char(out, '\x1b');
1121+
i += 3;
1122+
}
1123+
else if (n == 'e' || n == 'E')
1124+
{
1125+
/* GNU-style \e escape for ESC */
1126+
out = hrmp_append_char(out, '\x1b');
1127+
++i;
1128+
}
1129+
else if (n == 'n')
1130+
{
1131+
out = hrmp_append_char(out, '\n');
1132+
++i;
1133+
}
1134+
else if (n == 'r')
1135+
{
1136+
out = hrmp_append_char(out, '\r');
1137+
++i;
1138+
}
1139+
else if (n == 't')
1140+
{
1141+
out = hrmp_append_char(out, '\t');
1142+
++i;
1143+
}
1144+
else if (n == '\\')
1145+
{
1146+
out = hrmp_append_char(out, '\\');
1147+
++i;
1148+
}
1149+
else
1150+
{
1151+
/* Unknown escape, keep the backslash literal */
1152+
out = hrmp_append_char(out, c);
1153+
}
1154+
}
11021155
else
11031156
{
1104-
out = hrmp_append_char(out, *p);
1157+
out = hrmp_append_char(out, c);
11051158
}
11061159
}
11071160

0 commit comments

Comments
 (0)