Skip to content

Commit

Permalink
Merge pull request #256 from asciinema/original-theme
Browse files Browse the repository at this point in the history
Use original recording theme when available unless theme option used
  • Loading branch information
ku1ik committed Feb 15, 2024
2 parents e31ba14 + c491798 commit 43ea580
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/components/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default props => {
const [duration, setDuration] = createSignal(undefined);
const [markers, setMarkers] = createStore([]);
const [userActive, setUserActive] = createSignal(false);
const [originalTheme, setOriginalTheme] = createSignal(undefined);

const terminalCols = () =>
terminalSize().cols || 80;
Expand Down Expand Up @@ -92,9 +93,10 @@ export default props => {
}
}

core.addEventListener('init', ({ cols, rows, duration, poster, markers }) => {
core.addEventListener('init', ({ cols, rows, duration, theme, poster, markers }) => {
resize({ cols, rows });
setDuration(duration);
setOriginalTheme(theme);
setMarkers(markers);
setPoster(poster);
});
Expand Down Expand Up @@ -384,11 +386,22 @@ export default props => {
style['height'] = `${size.height}px`;
}

const theme = originalTheme();

if (theme !== undefined && (props.theme === undefined || props.theme === null)) {
style['--term-color-foreground'] = theme.foreground;
style['--term-color-background'] = theme.background;

theme.palette.forEach((color, i) => {
style[`--term-color-${i}`] = color;
});
}

return style;
}

const playerClass = () =>
`ap-player asciinema-theme-${props.theme ?? 'asciinema'}`;
`ap-player asciinema-player-theme-${props.theme ?? 'asciinema'}`;

const terminalScale = () =>
terminalElementSize()?.scale;
Expand Down
1 change: 1 addition & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class Core {
rows: this.rows,
duration: this.duration,
markers: this.markers,
theme: meta.theme,
poster
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/recording.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function recording(src, { feed, onInput, onMarker, now, setTimeout, setState, lo
.filter(e => e[1] === 'm')
.map(e => [e[0], e[2].label]);

return { cols, rows, duration, poster, markers };
return { cols, rows, duration, theme: recording.theme, poster, markers };
}

function doFetch({ url, data, fetchOpts = {} }) {
Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_asciinema.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.asciinema-theme-asciinema {
.asciinema-player-theme-asciinema {
@basehue: -17;
@saturation: 70%;
@lightness: 55%;
Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_dracula.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Based on Dracula: https://draculatheme.com
*/

.asciinema-theme-dracula {
.asciinema-player-theme-dracula {
--term-color-foreground: #f8f8f2;
--term-color-background: #282a36;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_monokai.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Based on Monokai from base16 collection - https://github.com/chriskempson/base16 */

.asciinema-theme-monokai {
.asciinema-player-theme-monokai {
--term-color-foreground: #f8f8f2;
--term-color-background: #272822;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_nord.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Via: https://github.com/neilotoole/asciinema-theme-nord
*/

.asciinema-theme-nord {
.asciinema-player-theme-nord {
--term-color-foreground: #eceff4;
--term-color-background: #2e3440;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_seti.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.asciinema-theme-seti {
.asciinema-player-theme-seti {
--term-color-foreground: #cacecd;
--term-color-background: #111213;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_solarized-dark.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Based on Solarized Dark: https://ethanschoonover.com/solarized/
*/

.asciinema-theme-solarized-dark {
.asciinema-player-theme-solarized-dark {
--term-color-foreground: #839496;
--term-color-background: #002b36;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_solarized-light.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Based on Solarized Light: https://ethanschoonover.com/solarized/
*/

.asciinema-theme-solarized-light {
.asciinema-player-theme-solarized-light {
--term-color-foreground: #657b83;
--term-color-background: #fdf6e3;

Expand Down
2 changes: 1 addition & 1 deletion src/less/themes/_tango.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Based on Tango: https://en.wikipedia.org/wiki/Tango_Desktop_Project
*/

.asciinema-theme-tango {
.asciinema-player-theme-tango {
--term-color-foreground: #cccccc;
--term-color-background: #121314;

Expand Down
17 changes: 17 additions & 0 deletions src/parser/asciicast.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,28 @@ function parseAsciicastV2(header, events) {
return {
cols: header.width,
rows: header.height,
theme: parseTheme(header.theme),
events,
idleTimeLimit: header.idle_time_limit
}
}

function parseTheme(theme) {
const colorRegex = /^#[0-9A-Fa-f]{6}$/;
const paletteRegex = /^(#[0-9A-Fa-f]{6}:){7,}#[0-9A-Fa-f]{6}$/;
const fg = theme?.fg;
const bg = theme?.bg;
const palette = theme?.palette;

if (colorRegex.test(fg) && colorRegex.test(bg) && paletteRegex.test(palette)) {
return {
foreground: fg,
background: bg,
palette: palette.split(':')
}
}
}

function unparseAsciicastV2(recording) {
const header = JSON.stringify({
version: 2,
Expand Down

0 comments on commit 43ea580

Please sign in to comment.