This commit is contained in:
Zhi Wang 2022-01-18 13:06:25 -05:00
parent bb8eda56fb
commit 4b73e418e2
3 changed files with 61 additions and 20 deletions

View File

@ -80,12 +80,53 @@ function base64ToUint8array(base64) {
return array; return array;
} }
async function play_ctrl(term, session, start, total_dur, paused, shifted, prog) {
var cur = 0
var new_pos = -1
start = parseInt(total_dur * start / 100)
term.reset()
for (const item of session) {
new_pos = shifted()
if (new_pos != -1) {
return new_pos
}
// we will blast through the beginning of the session
if (cur >= start) {
// we are cheating a little bit here, we do not want to wait for too long
exit = await sleep(Math.min(item.Duration, 800), paused)
if (exit) {
return
}
}
term.write(base64ToUint8array(item.Data))
cur += item.Duration
if (cur > start) {
prog(parseInt(cur * 100 / total_dur))
}
}
return -1
}
// replay session // replay session
// term: xterm, path: session file to replay, // term: xterm, path: session file to replay,
// start: start position to replay in percentile, range 0-100 // start: start position to replay in percentile, range 0-100
// callback to update the progress bar // paused: callback whether to stop play
async function replay_session(term, path, start, paused, prog, end) { // prog: callback to update the progress bar
// shift: callback whether we should change position
// end: callback when playback is finished
async function replay_session(term, path, start, paused, shifted, prog, end) {
var session var session
var total_dur = 0
var cur = 0
var new_pos = 0
var ret = 0
// read file from server // read file from server
await fetch(path) await fetch(path)
@ -94,34 +135,24 @@ async function replay_session(term, path, start, paused, prog, end) {
session = out session = out
}) })
var total_dur = 0
var cur = 0
//calculate the total duration //calculate the total duration
for (const item of session) { for (const item of session) {
item.Duration = parseInt(item.Duration / 1000000) item.Duration = parseInt(item.Duration / 1000000)
total_dur += item.Duration total_dur += item.Duration
} }
start = parseInt(total_dur * start / 100) console.log("Total duration:", total_dur, "start replay on position", start)
console.log("Total duration:", total_dur, "start replay on", start)
term.reset() while (true) {
for (const item of session) { ret = await play_ctrl(term, session, start, total_dur, paused, shifted, prog)
cur += item.Duration if (ret == -1) {
break
// we will blast through the beginning of the session
if (cur >= start) {
// we are cheating a little bit here, we do not want to wait for too long
if (await sleep(Math.min(item.Duration, 1000), paused) == true) {
return
}
} }
prog(parseInt(cur * 100 / total_dur)) start = ret
term.write(base64ToUint8array(item.Data))
} }
term.reset()
end() end()
} }

View File

@ -41,7 +41,7 @@
<img src="/assets/img/play.svg" id="play-btn" height="18px"> <img src="/assets/img/play.svg" id="play-btn" height="18px">
</button> </button>
<input type="range" class="form-range" min="0" max="100" id="replay-control" <input type="range" class="form-range" min="0" max="100" id="replay-control"
onchange="console.log(this.value)"> onchange="clicked = this.value">
</div> </div>
</div> </div>
@ -54,6 +54,7 @@
var icon = document.getElementById("play-btn") var icon = document.getElementById("play-btn")
var path = "/records/{{.fname}}" var path = "/records/{{.fname}}"
var pause = false var pause = false
var clicked = -1
function playbtn() { function playbtn() {
if (icon.src.includes("play")) { if (icon.src.includes("play")) {
@ -63,6 +64,11 @@
function () { function () {
return pause return pause
}, },
function(){
var tmp = clicked
clicked = -1
return tmp
},
function (percent) { function (percent) {
rc.value = percent rc.value = percent
}, },

View File

@ -156,6 +156,10 @@ func main() {
}) })
}) })
rt.GET("/favicon.ico", func(c *gin.Context) {
c.File("./assets/img/favicon.ico")
})
// to update the tabs of current interactive and saved sessions // to update the tabs of current interactive and saved sessions
rt.GET("/update/:active", func(c *gin.Context) { rt.GET("/update/:active", func(c *gin.Context) {
var active0, active1 string var active0, active1 string