<commit_before>package net.milosvasic.pussycat.core


import net.milosvasic.pussycat.color.Color
import net.milosvasic.pussycat.logger
import net.milosvasic.pussycat.utils.Text
import java.io.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Pussycat main class
 */
class Pussycat : PussycatActions {

    private var filter = ""
    private val TAG = Pussycat::class
    private var run = AtomicBoolean(true)
    private var color: String = Color.BLACK
    private val paused = AtomicBoolean(false)
    private var refreshing = AtomicBoolean(false)
    private val data = CopyOnWriteArrayList<String>()

    override fun live() {
        Thread(Runnable {
            Thread.currentThread().name = "Live adb reading thread"
            val process = Runtime.getRuntime().exec("adb logcat")
            val input = process.inputStream
            readLive(input)
            process.destroy()
        }).start()
    }

    override fun filesystem(file: File) {
        Thread(Runnable {
            Thread.currentThread().name = "Filesystem reading thread"
            readFilesystem(FileInputStream(file.absoluteFile))
        }).start()
    }

    override fun stop() {
        run.set(false)
    }

    override fun pause() {
        paused.set(true)
    }

    override fun resume() {
        applyFilter()
    }

    override fun filter() {
        filter = ""
        applyFilter()
    }

    override fun filter(filter: String) {
        this.filter = filter
        applyFilter()
    }

    override fun clear() {
        println(27.toChar() + "[2J")
    }

    private fun filterOk(line: String): Boolean {
        if (Text.isEmpty(filter)) {
            return true
        }
        if (!filter.contains("&&") && !filter.contains("||")) {
            if (filter.startsWith("!")) {
                val check = filter.replace("!", "")
                if (line.containsIgnoreCase(check)) {
                    return false
                }
            } else {
                if (!line.containsIgnoreCase(filter)) {
                    return false
                }
            }
            return true
        }
        if (filter.contains("&&")) {
            val params = filter.split("&&")
            for (item in params) {
                var check = item.trim()
                if (check.startsWith("!")) {
                    check = check.replace("!", "")
                    if (line.containsIgnoreCase(check)) {
                        return false
                    }
                } else {
                    if (!line.containsIgnoreCase(check)) {
                        return false
                    }
                }
            }
            return true
        }
        if (filter.contains("||")) {
            val params = filter.split("||")
            for (item in params) {
                var check = item.trim()
                if (check.startsWith("!")) {
                    check = check.replace("!", "")
                    if (!line.containsIgnoreCase(check)) {
                        return true
                    }
                } else {
                    if (line.containsIgnoreCase(check)) {
                        return true
                    }
                }
            }
            return false
        }
        return true
    }

    private fun applyFilter() {
        refreshing.set(true)
        paused.set(false)
        clear()
        if (data.isEmpty()) {
            logger.w(TAG, "No data available, filter [ $filter ]")
        } else {
            var x = 0
            for (line in data) {
                if (filterOk(line)) {
                    printLine(line)
                    x++
                }
            }
            if (x == 0) logger.w(TAG, "No data matching, filter [ $filter ]")
        }
        refreshing.set(false)
    }

    private fun printLine(line: String) {
        if (paused.get()) {
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.V_LIVE) || line.containsIgnoreCase(LogcatTagType.V_FILESYSTEM)) {
            color = Color.WHITE
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.D_LIVE) || line.containsIgnoreCase(LogcatTagType.D_FILESYSTEM)) {
            color = Color.YELLOW
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.I_LIVE) || line.containsIgnoreCase(LogcatTagType.I_FILESYSTEM)) {
            color = Color.CYAN
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.W_LIVE) || line.containsIgnoreCase(LogcatTagType.W_FILESYSTEM)) {
            color = Color.PURPLE
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.E_LIVE) || line.containsIgnoreCase(LogcatTagType.E_FILESYSTEM)) {<commit_msg>Pussycat: Refactoring / Improving / Polishing.<commit_after>package net.milosvasic.pussycat.core


import net.milosvasic.pussycat.color.Color
import net.milosvasic.pussycat.logger
import net.milosvasic.pussycat.utils.Text
import java.io.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Pussycat main class
 */
class Pussycat : PussycatActions {

    private var filter = ""
    private val TAG = Pussycat::class
    private var run = AtomicBoolean(true)
    private var color: String = Color.BLACK
    private val paused = AtomicBoolean(false)
    private var refreshing = AtomicBoolean(false)
    private val data = CopyOnWriteArrayList<String>()

    override fun live() {
        Thread(Runnable {
            Thread.currentThread().name = "Live adb reading thread"
            val process = Runtime.getRuntime().exec("adb logcat")
            val input = process.inputStream
            readLive(input)
            process.destroy()
        }).start()
    }

    override fun filesystem(file: File) {
        Thread(Runnable {
            Thread.currentThread().name = "Filesystem reading thread"
            readFilesystem(FileInputStream(file.absoluteFile))
        }).start()
    }

    override fun stop() {
        run.set(false)
    }

    override fun pause() {
        paused.set(true)
    }

    override fun resume() {
        applyFilter()
    }

    override fun filter() {
        filter = ""
        applyFilter()
    }

    override fun filter(filter: String) {
        this.filter = filter
        applyFilter()
    }

    override fun clear() {
        println(27.toChar() + "[2J")
    }

    private fun applyFilter() {
        refreshing.set(true)
        paused.set(false)
        clear()
        if (data.isEmpty()) {
            logger.w(TAG, "No data available, filter [ $filter ]")
        } else {
            var x = 0
            for (line in data) {
                if (filterOk(line)) {
                    printLine(line)
                    x++
                }
            }
            if (x == 0) logger.w(TAG, "No data matching, filter [ $filter ]")
        }
        refreshing.set(false)
    }

    private fun filterOk(line: String): Boolean {
        if (Text.isEmpty(filter)) {
            return true
        }
        if (!filter.contains("&&") && !filter.contains("||")) {
            if (filter.startsWith("!")) {
                val check = filter.replace("!", "")
                if (line.containsIgnoreCase(check)) {
                    return false
                }
            } else {
                if (!line.containsIgnoreCase(filter)) {
                    return false
                }
            }
            return true
        }
        if (filter.contains("&&")) {
            val params = filter.split("&&")
            for (item in params) {
                var check = item.trim()
                if (check.startsWith("!")) {
                    check = check.replace("!", "")
                    if (line.containsIgnoreCase(check)) {
                        return false
                    }
                } else {
                    if (!line.containsIgnoreCase(check)) {
                        return false
                    }
                }
            }
            return true
        }
        if (filter.contains("||")) {
            val params = filter.split("||")
            for (item in params) {
                var check = item.trim()
                if (check.startsWith("!")) {
                    check = check.replace("!", "")
                    if (!line.containsIgnoreCase(check)) {
                        return true
                    }
                } else {
                    if (line.containsIgnoreCase(check)) {
                        return true
                    }
                }
            }
            return false
        }
        return true
    }

    private fun printLine(line: String) {
        if (paused.get()) {
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.V_LIVE) || line.containsIgnoreCase(LogcatTagType.V_FILESYSTEM)) {
            color = Color.WHITE
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.D_LIVE) || line.containsIgnoreCase(LogcatTagType.D_FILESYSTEM)) {
            color = Color.YELLOW
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.I_LIVE) || line.containsIgnoreCase(LogcatTagType.I_FILESYSTEM)) {
            color = Color.CYAN
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.W_LIVE) || line.containsIgnoreCase(LogcatTagType.W_FILESYSTEM)) {
            color = Color.PURPLE
            println("$color$line${Color.RESET}")
            return
        }
        if (line.containsIgnoreCase(LogcatTagType.E_LIVE) || line.containsIgnoreCase(LogcatTagType.E_FILESYSTEM)) {
{'fraction_non_alphanumeric': 0.11112213513245361, 'fraction_numerical': 0.0009921619208254787, 'mean_word_length': 1.5155977040179685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3391030', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2644, 'n_words': 533}