From 6f4c99ef5b6f78b3ce8fc2c6d2d916508bddd5d7 Mon Sep 17 00:00:00 2001 From: darwincereska Date: Tue, 11 Nov 2025 10:39:17 -0500 Subject: [PATCH] feat(no-color): added --no-color flag --- README.md | 21 +++++++++++++++ build.gradle.kts | 2 +- src/main/kotlin/io/notevc/NoteVC.kt | 27 +++++++++++++------ src/main/kotlin/io/notevc/utils/ColorUtils.kt | 18 ++++++++++--- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4ff169b..c786b4b 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,27 @@ The executable will be in `build/install/notevc/bin/notevc` --- +## Global Options + +These options work with any command and should be placed before the command name: + +- `--no-color`: Disable colored output + +**Examples:** +```bash +notevc --no-color status +notevc --no-color diff +notevc --no-color log --oneline +notevc --no-color show a1b2c3d4 +``` + +**Alternative methods to disable colors:** +- Set `NO_COLOR=1` environment variable: `NO_COLOR=1 notevc status` +- Pipe output: `notevc status | less` (colors auto-disabled) +- CI environments automatically disable colors + +--- + ## Command Reference ### Repository Management diff --git a/build.gradle.kts b/build.gradle.kts index 849d022..5b145ed 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { } group = "io.notevc" -version = "1.0.3" +version = "1.0.4" buildConfig { buildConfigField("String", "VERSION", "\"${project.version}\"") diff --git a/src/main/kotlin/io/notevc/NoteVC.kt b/src/main/kotlin/io/notevc/NoteVC.kt index 038fdcb..8976392 100644 --- a/src/main/kotlin/io/notevc/NoteVC.kt +++ b/src/main/kotlin/io/notevc/NoteVC.kt @@ -5,11 +5,22 @@ import io.notevc.commands.* import io.notevc.utils.ColorUtils fun main(args: Array) { + // Parse global flags first (like --no-color) + val filteredArgs = args.filter { arg -> + when (arg) { + "--no-color" -> { + ColorUtils.disableColors() + false // Remove this arg from the list + } + else -> true // Keep this arg + } + }.toTypedArray() + // Args logic - when (args.firstOrNull()) { + when (filteredArgs.firstOrNull()) { "init", "i" -> { val initCommand = InitCommand() - val result = initCommand.execute(args.getOrNull(1)) + val result = initCommand.execute(filteredArgs.getOrNull(1)) result.fold( onSuccess = { message -> println(message) }, @@ -18,7 +29,7 @@ fun main(args: Array) { } "log" -> { - val logArgs = args.drop(1) + val logArgs = filteredArgs.drop(1) val logCommand = LogCommand() val result = logCommand.execute(logArgs) @@ -29,7 +40,7 @@ fun main(args: Array) { } "commit" -> { - val commitArgs = args.drop(1) + val commitArgs = filteredArgs.drop(1) val commitCommand = CommitCommand() val result = commitCommand.execute(commitArgs) @@ -54,7 +65,7 @@ fun main(args: Array) { } "restore" -> { - val restoreArgs = args.drop(1) + val restoreArgs = filteredArgs.drop(1) val restoreCommand = RestoreCommand() val result = restoreCommand.execute(restoreArgs) @@ -65,7 +76,7 @@ fun main(args: Array) { } "diff" -> { - val diffArgs = args.drop(1) + val diffArgs = filteredArgs.drop(1) val diffCommand = DiffCommand() val result = diffCommand.execute(diffArgs) @@ -76,7 +87,7 @@ fun main(args: Array) { } "show" -> { - val showArgs = args.drop(1) + val showArgs = filteredArgs.drop(1) val showCommand = ShowCommand() val result = showCommand.execute(showArgs) @@ -87,7 +98,7 @@ fun main(args: Array) { } else -> { - println("Usage: notevc init|commit|status|log|restore|diff|show|version") + println("Usage: notevc [--no-color] init|commit|status|log|restore|diff|show|version") } } } diff --git a/src/main/kotlin/io/notevc/utils/ColorUtils.kt b/src/main/kotlin/io/notevc/utils/ColorUtils.kt index 9f49c7f..0a5e883 100644 --- a/src/main/kotlin/io/notevc/utils/ColorUtils.kt +++ b/src/main/kotlin/io/notevc/utils/ColorUtils.kt @@ -24,10 +24,20 @@ object ColorUtils { private const val BRIGHT_MAGENTA = "\u001B[95m" private const val BRIGHT_CYAN = "\u001B[96m" - // Check if colors should be enabled (disable in CI/pipes) - private val colorsEnabled = System.getenv("NO_COLOR") == null && - System.getenv("CI") == null && - System.console() != null + // Flag to force disable colors via --no-color flag + var forceDisableColors = false + + // Check if colors should be enabled (disable in CI/pipes or via flag) + private val colorsEnabled: Boolean + get() = !forceDisableColors && + System.getenv("NO_COLOR") == null && + System.getenv("CI") == null && + System.console() != null + + // Function to disable colors programmatically + fun disableColors() { + forceDisableColors = true + } // Public color functions fun red(text: String): String = if (colorsEnabled) "$RED$text$RESET" else text