import {dots} from 'cli-spinners'
import chalk from 'chalk'

let _isDriverLogging = false
let _lineCount = 0
let _spinnerInterval = null
let _spinnerIndex = 0
let _message = null

function _redraw(opts = {}) {
  let symbol = chalk.cyan(dots.frames[_spinnerIndex])

  if (opts.success === true) {
    symbol = chalk.green('✔')
  } else if (opts.success === false) {
    symbol = chalk.red('•')
  }

  process.stdout.clearLine()
  process.stdout.cursorTo(0)
  process.stdout.moveCursor(0, -_lineCount - 1)
  console.log(symbol, opts.success === false ? chalk.red(_message, 'FAILED') : _message)
  process.stdout.cursorTo(0)
  process.stdout.moveCursor(0, _lineCount)
}

export function startDriverLogging(message: string) {
  _isDriverLogging = true
  _message = message
  _lineCount = 0

  console.log()
  _redraw()

  _spinnerInterval = setInterval(() => {
    _spinnerIndex++

    if (_spinnerIndex >= dots.frames.length) {
      _spinnerIndex = 0
    }

    _redraw()
  }, dots.interval)
}

export function stopDriverLogging(opts = {}) {
  if (!isDriverLogging()) return

  _isDriverLogging = false
  clearInterval(_spinnerInterval)

  _redraw({ success: opts.success != null ? opts.success : true })
}

export function isDriverLogging(): boolean {
  return _isDriverLogging
}

export function log(message: string) {
  if (!isDriverLogging()) return

  _lineCount += message.split('\n').length - 1
  process.stdout.write(chalk.dim(message))
}

@@ -87,27 +87,8 @@
 lse%0A
-let _lineCount = 0%0A
 let 
@@ -393,119 +393,129 @@
 out.
-clearLine()%0A  process.stdout.cursorTo(0)%0A  process.stdout.moveCursor(0, -_lineCount - 1)%0A  console.log(
+write('                                                                        %5Cn')%0A  process.stdout.write(%60 $%7B
 symbol
-, 
+%7D $%7B
 opts
@@ -575,16 +575,20 @@
 _message
+%7D%5Cn%60
 )%0A  proc
@@ -641,26 +641,18 @@
 rsor(0, 
-_lineCount
+-2
 )%0A%7D%0A%0Aexp
@@ -753,41 +753,8 @@
 age%0A
-  _lineCount = 0%0A%0A  console.log()
 %0A  _
@@ -1150,16 +1150,38 @@
 true %7D)%0A
+  console.log('%5Cn%5Cn')%0A
 %7D%0A%0Aexpor
@@ -1326,55 +1326,8 @@
 rn%0A%0A
-  _lineCount += message.split('%5Cn').length - 1%0A
   pr

Improve driver logging