All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.ajalt.clikt.output.Editor.kt Maven / Gradle / Ivy

package com.github.ajalt.clikt.output

import com.github.ajalt.clikt.mpp.isWindowsMpp
import com.github.ajalt.clikt.mpp.readEnvvar

internal expect class Editor(
        editorPath: String?,
        env: Map,
        requireSave: Boolean,
        extension: String
) {
    fun editFile(filename: String)
    fun edit(text: String): String?
}

internal fun inferEditorPath(commandExists: (String) -> Boolean): String {
    for (key in arrayOf("VISUAL", "EDITOR")) {
        return readEnvvar(key) ?: continue
    }

    val editors = when {
        TermUi.isWindows -> arrayOf("vim", "nano", "notepad")
        else -> arrayOf("vim", "nano")
    }

    for (editor in editors) {
        if (commandExists(editor)) return editor
    }

    return if (TermUi.isWindows) "notepad" else "vi"
}

internal fun normalizeEditorText(editor: String, text: String): String {
    return when (editor) {
        "notepad" -> text.replace(Regex("(? text.replace("\r\n", "\n")
    }
}

internal fun getWhichCommand(): String = when {
    isWindowsMpp() -> "where"
    else -> "which"
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy