Skip to content

Commit

Permalink
Set virtual terminal size according to requested player cols/rows (cl…
Browse files Browse the repository at this point in the history
…oses #67) (closes #68)
  • Loading branch information
ku1ik committed Jul 18, 2017
1 parent 34b3d01 commit 430b7f8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion resources/public/element.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<body>
<p>Text</p>

<asciinema-player id="player" src="/asciicasts/frames-10386.json" loop></asciinema-player>
<asciinema-player id="player" src="/asciicasts/21195.json" cols="100" rows="30" loop></asciinema-player>

<script>
var player = document.getElementById('player');
Expand Down
12 changes: 5 additions & 7 deletions src/asciinema/player/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
[text :- s/Str
width :- s/Num
height :- s/Num]
(-> (vt/make-vt width height)
(-> (vt/make-vt (or width 80) (or height 24))
(vt/feed-str text)))

(defn parse-poster [poster width height]
Expand All @@ -49,14 +49,12 @@
[url {:keys [type width height start-at speed loop auto-play preload poster font-size theme]
:or {speed 1 loop false auto-play false preload false font-size "small" theme "asciinema"}
:as options}]
(let [vt-width (or width 80)
vt-height (or height 24)
start-at (or (parse-npt start-at) 0)
{poster-screen :screen poster-time :time} (parse-poster poster vt-width vt-height)
(let [start-at (or (parse-npt start-at) 0)
{poster-screen :screen poster-time :time} (parse-poster poster width height)
poster-time (or poster-time (when (and (not poster-screen) (pos? start-at)) start-at))
source (make-source url {:type type
:width vt-width
:height vt-height
:width width
:height height
:start-at start-at
:speed speed
:auto-play auto-play
Expand Down
17 changes: 11 additions & 6 deletions src/asciinema/player/format/asciicast_v1.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@
(defn- vt-cursor [vt]
(-> vt :screen screen/cursor))

(defn build-v1-frames [{:keys [stdout width height]}]
(defn build-v1-frames [stdout width height]
(let [vt (vt/make-vt width height)]
(->> stdout
frames/to-absolute-time
(frames/at-hz 30 #(.concat %1 %2))
(reductions reduce-vt [0 vt]))))

(s/defn initialize-asciicast
[asciicast :- AsciicastV1]
{:width (:width asciicast)
:height (:height asciicast)
:duration (reduce #(+ %1 (first %2)) 0 (:stdout asciicast))
:frames (build-v1-frames asciicast)})
[asciicast :- AsciicastV1
vt-width :- s/Num
vt-height :- s/Num]
(let [width (or vt-width (:width asciicast))
height (or vt-height (:height asciicast))
stdout (:stdout asciicast)]
{:width width
:height height
:duration (reduce #(+ %1 (first %2)) 0 stdout)
:frames (build-v1-frames stdout width height)}))

(extend-protocol ps/Screen
vt/VT
Expand Down
20 changes: 10 additions & 10 deletions src/asciinema/player/source.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@

(defmulti initialize-asciicast
"Given fetched asciicast extracts width, height and frames into a map."
(fn [asciicast]
(fn [asciicast _ _]
(if (vector? asciicast)
0
(:version asciicast))))

(defmethod initialize-asciicast 0 [asciicast]
(defmethod initialize-asciicast 0 [asciicast _ _]
(v0/initialize-asciicast asciicast))

(defmethod initialize-asciicast 1 [asciicast]
(v1/initialize-asciicast asciicast))
(defmethod initialize-asciicast 1 [asciicast vt-width vt-height]
(v1/initialize-asciicast asciicast vt-width vt-height))

(defmethod initialize-asciicast :default [asciicast]
(defmethod initialize-asciicast :default [asciicast _ _]
(throw (str "unsupported asciicast version: " (:version asciicast))))

(defn time-frames
Expand Down Expand Up @@ -84,17 +84,17 @@
js/JSON.parse
(js->clj :keywordize-keys true)))

(defn make-recording-ch-fn [thing]
(defn make-recording-ch-fn [thing vt-width vt-height]
(lazy-promise-chan
(fn [deliver]
(cond
(string? thing) (xhr/send thing (fn [event]
(let [res (-> event .-target .getResponseText)]
(deliver (-> res
parse-json
initialize-asciicast)))))
(initialize-asciicast vt-width vt-height))))))
(or (:stdout thing)
(sequential? thing)) (deliver (initialize-asciicast thing))))))
(sequential? thing)) (deliver (initialize-asciicast thing vt-width vt-height))))))

(defn report-metadata
"Reports recording dimensions and duration to the player."
Expand Down Expand Up @@ -259,8 +259,8 @@
(change-speed [this speed]
(put! command-ch [:change-speed speed])))

(defmethod make-source :asciicast [url {:keys [start-at speed auto-play loop preload poster-time]}]
(let [recording-ch-fn (make-recording-ch-fn url)
(defmethod make-source :asciicast [url {:keys [width height start-at speed auto-play loop preload poster-time]}]
(let [recording-ch-fn (make-recording-ch-fn url width height)
command-ch (chan 10)
force-load-ch (chan)]
(->Recording recording-ch-fn
Expand Down
6 changes: 3 additions & 3 deletions test/asciinema/player/source_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
(testing "pre v1 format"
(let [asciicast [[1.0 {:lines {:0 [["foo" {}] ["bar" {}]] :1 [["foobar" {}]]}}]
[2.0 {:lines {:0 [["baz" {}] ["qux" {}]] :1 [["quuuux" {}]]}}]]
asciicast (s/initialize-asciicast asciicast)]
asciicast (s/initialize-asciicast asciicast nil nil)]
(is (= (select-keys asciicast [:width :height :duration])
{:width 6 :height 2 :duration 3.0}))))

(testing "v1 format"
(let [asciicast {:version 1 :width 80 :height 24 :stdout [[1.0 "foo"] [2.0 "bar"]]}
asciicast (s/initialize-asciicast asciicast)]
asciicast (s/initialize-asciicast asciicast nil 30)]
(is (= (select-keys asciicast [:width :height :duration])
{:width 80 :height 24 :duration 3.0})))))
{:width 80 :height 30 :duration 3.0})))))

0 comments on commit 430b7f8

Please sign in to comment.