Skip to content

Commit

Permalink
Fixed renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanyunusov committed Feb 17, 2022
1 parent 4b2012f commit f19fabe
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,6 @@ function getStateSymbol(task) {
}
}

function createLogUpdate(stream) {
let lines = 0

function clear() {
for (let i = 0; i < lines; i++) {
i > 0 && readline.moveCursor(stream, 0, -1)
readline.cursorTo(stream, 0)
readline.clearLine(stream, 0)
}
lines = 0
}

function render(output) {
clear()

stream.write(output)
lines = getLines(output, stream.columns)
}

render.clear = () => {
clear()
}

return render
}

function getTitles(task) {
const titles = [task.title]
let current = task
Expand Down Expand Up @@ -120,51 +94,69 @@ function renderCI(tasks) {
}

export function createRenderer(stream) {
let log = createLogUpdate(stream)
let tasks = []
let timer
let lines = 0

function update() {
if (isTTY) {
log(renderTree(tasks))
} else {
stream.write(renderCI(tasks))
}
}
return {
clear() {
for (let i = 0; i < lines; i++) {
i > 0 && readline.moveCursor(stream, 0, -1)
readline.cursorTo(stream, 0)
readline.clearLine(stream, 0)
}
lines = 0
},

function loop() {
timer = setTimeout(() => loop(), 130)
update()
}
write(str, clear = false) {
if (clear) {
this.clear()
}

stream.write(str)
},

render() {
const output = isTTY ? renderTree(tasks) : renderCI(tasks)

if (isTTY) {
this.write(output, true)
lines = getLines(output, stream.columns)
} else {
this.write(output)
}

return this
},

loop() {
timer = setTimeout(() => this.loop(), 130)
return this.render()
},

return {
start() {
if (timer) return this
if (isTTY) stream.write(`\x1b[?25l`)

loop()
return this
return this.loop()
},

stop() {
if (timer) timer = clearTimeout(timer)

if (isTTY) {
log.clear()
stream.write(`${renderTree(tasks)}\n`)
stream.write(`\x1b[?25h`)
this.write(`${renderTree(tasks)}\n`, true)
this.write(`\x1b[?25h`)
} else {
stream.write(renderCI(tasks))
this.write(renderCI(tasks))
}

return this
},

update(task) {
tasks.push(task)
update()

return this
return this.render()
},
}
}

0 comments on commit f19fabe

Please sign in to comment.