package com.karumi.shot.android

import com.karumi.shot.domain.model.{AppId, Folder}

import scala.sys.process._

object Adb {
  var adbBinaryPath: String = ""
}

class Adb {

  private final val CR_ASCII_DECIMAL = 13

  def devices: List[String] = {
    executeAdbCommandWithResult("devices")
      .split('\n')
      .toList
      .drop(1)
      .map { line =>
        line.split('\t').toList.head
      }
      .filter(device => !isCarriageReturnASCII(device))
  }

  def pullScreenshots(device: String,
                      screenshotsFolder: Folder,
                      appId: AppId): Unit =
    executeAdbCommandWithResult(
      s"-s $device pull /sdcard/screenshots/$appId/screenshots-default/ $screenshotsFolder")

  def clearScreenshots(device: String, appId: AppId): Unit =
    executeAdbCommand(
      s"-s $device shell rm -r /sdcard/screenshots/$appId/screenshots-default/")

  private def executeAdbCommand(command: String): Int =
    s"${Adb.adbBinaryPath} $command".!

  private def executeAdbCommandWithResult(command: String): String =
    s"${Adb.adbBinaryPath} $command".!!

  private def isCarriageReturnASCII(device: String): Boolean =
    device.charAt(0) == CR_ASCII_DECIMAL
}

package com.karumi.shot.android

import com.karumi.shot.domain.model.{AppId, Folder}

import scala.sys.process._

object Adb {
  var adbBinaryPath: String = ""
}

class Adb {

  private final val CR_ASCII_DECIMAL = 13
  private val logger = ProcessLogger(
    o => println("Shot ADB output: " + o),
    e => println(Console.RED + "Shot ADB error: " + e + Console.RESET)
  )

  def devices: List[String] = {
    executeAdbCommandWithResult("devices")
      .split('\n')
      .toList
      .drop(1)
      .map { line =>
        line.split('\t').toList.head
      }
      .filter(device => !isCarriageReturnASCII(device))
  }

  def pullScreenshots(device: String,
                      screenshotsFolder: Folder,
                      appId: AppId): Unit =
    executeAdbCommandWithResult(
      s"-s $device pull /sdcard/screenshots/$appId/screenshots-default/ $screenshotsFolder")

  def clearScreenshots(device: String, appId: AppId): Unit =
    executeAdbCommand(
      s"-s $device shell rm -r /sdcard/screenshots/$appId/screenshots-default/")

  private def executeAdbCommand(command: String): Int =
    s"${Adb.adbBinaryPath} $command" ! logger

  private def executeAdbCommandWithResult(command: String): String =
    s"${Adb.adbBinaryPath} $command" !! logger

  private def isCarriageReturnASCII(device: String): Boolean =
    device.charAt(0) == CR_ASCII_DECIMAL
}

Add process logger to the usage of ADB
Add process logger to the usage of ADB
