feat(no-color): added --no-color flag
This commit is contained in:
21
README.md
21
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
|
||||
|
||||
@@ -10,7 +10,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "io.notevc"
|
||||
version = "1.0.3"
|
||||
version = "1.0.4"
|
||||
|
||||
buildConfig {
|
||||
buildConfigField("String", "VERSION", "\"${project.version}\"")
|
||||
|
||||
@@ -5,11 +5,22 @@ import io.notevc.commands.*
|
||||
import io.notevc.utils.ColorUtils
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// 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<String>) {
|
||||
}
|
||||
|
||||
"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<String>) {
|
||||
}
|
||||
|
||||
"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<String>) {
|
||||
}
|
||||
|
||||
"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<String>) {
|
||||
}
|
||||
|
||||
"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<String>) {
|
||||
}
|
||||
|
||||
"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<String>) {
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,21 @@ 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 &&
|
||||
// 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
|
||||
fun green(text: String): String = if (colorsEnabled) "$GREEN$text$RESET" else text
|
||||
|
||||
Reference in New Issue
Block a user