diff --git a/source/dub/commandline.d b/source/dub/commandline.d index ccdd8af..e7eaf35 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -166,13 +166,17 @@ options.root_path = options.root_path.expandTilde.absolutePath.buildNormalizedPath; } - if (options.colors_mode == "" || options.colors_mode == "auto") { - // we already detected whether to enable colors or not with initLogging() above - // this if case is here just to make the else below work correctly - } else if (options.colors_mode == "on") { - setLoggingColorsEnabled(true); // enable colors, no matter what - } else if (options.colors_mode == "off") { - setLoggingColorsEnabled(false); // disable colors, no matter what + final switch (options.colors_mode) with (options.colors) + { + case automatic: + // Use default determined in internal.logging.initLogging(). + break; + case on: + setLoggingColorsEnabled(true); // enable colors, no matter what + break; + case off: + setLoggingColorsEnabled(false); // disable colors, no matter what + break; } } @@ -549,7 +553,8 @@ bool help, annotate, bare; string[] registry_urls; string root_path; - string colors_mode; + enum colors { automatic, on, off } // Style violation in support of invalid option error formatting. + colors colors_mode = colors.automatic; SkipPackageSuppliers skipRegistry = SkipPackageSuppliers.none; PlacementLocation placementLocation = PlacementLocation.user; @@ -578,10 +583,11 @@ args.getopt("verror", &verror, ["Only print errors"]); args.getopt("vquiet", &vquiet, ["Print no messages"]); args.getopt("colors", &colors_mode, [ - "Confiugre color output", - " auto: Automatically turn on/off colors (default)", - " on: Force colors enabled", - " off: Force colors disabled" + "Configure colored output. Accepted values:", + " automatic: Colored output on console/terminal,", + " unless NO_COLOR is defined (default)", + " on: Force colors enabled", + " off: Force colors disabled" ]); args.getopt("cache", &placementLocation, ["Puts any fetched packages in the specified location [local|system|user]."]); @@ -1782,7 +1788,7 @@ enforceUsage(app_args.length == 0, "Unexpected application arguments."); enforceUsage(!m_verify, "--verify is not yet implemented."); enforce(loadCwdPackage(dub, true), "Failed to load package."); - logInfo("Upgrading", Color.yellow, "project in %s", dub.projectPath.toNativeString()); + logInfo("Upgrading", Color.cyan, "project in %s", dub.projectPath.toNativeString().color(Mode.bold)); auto options = UpgradeOptions.upgrade|UpgradeOptions.select; if (m_missingOnly) options &= ~UpgradeOptions.upgrade; if (m_prerelease) options |= UpgradeOptions.preRelease; diff --git a/source/dub/dub.d b/source/dub/dub.d index 108f124..c3f8727 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -549,7 +549,7 @@ if (basename == rootbasename) continue; if (!m_project.selections.hasSelectedVersion(basename)) { - logInfo("Upgrade", Color.yellow, + logInfo("Upgrade", Color.cyan, "Package %s would be selected with version %s", basename, ver); any = true; continue; @@ -557,7 +557,7 @@ auto sver = m_project.selections.getSelectedVersion(basename); if (!sver.path.empty || !sver.repository.empty) continue; if (ver.version_ <= sver.version_) continue; - logInfo("Upgrade", Color.yellow, + logInfo("Upgrade", Color.cyan, "%s would be upgraded %s to %s.", basename.color(Mode.bold), sver, ver); any = true; @@ -728,7 +728,7 @@ /// Cleans intermediate/cache files of the given package void cleanPackage(NativePath path) { - logInfo("Cleaning", Color.green, "package at %s", path.toNativeString()); + logInfo("Cleaning", Color.green, "package at %s", path.toNativeString().color(Mode.bold)); enforce(!Package.findPackageFile(path).empty, "No package found.", path.toNativeString()); // TODO: clear target files and copy files @@ -1141,7 +1141,7 @@ } //Act smug to the user. - logInfo("Success", Color.green, "created empty project in %s", path.toNativeString()); + logInfo("Success", Color.green, "created empty project in %s", path.toNativeString().color(Mode.bold)); } private void runCustomInitialization(NativePath path, string type, string[] runArgs) diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index c87511b..09ef0fa 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -82,7 +82,7 @@ enforce(!(settings.rdmd && root_ti.buildSettings.targetType == TargetType.none), "Building package with target type \"none\" with rdmd is not supported yet."); - logInfo("Starting", Color.green, + logInfo("Starting", Color.light_green, "Performing \"%s\" build using %s for %-(%s, %).", settings.buildType.color(Color.magenta), settings.platform.compilerBinary, settings.platform.architecture); @@ -207,7 +207,7 @@ // run post-build commands if (!cached && buildsettings.postBuildCommands.length) { - logInfo("Post-build", Color.green, "Running commands"); + logInfo("Post-build", Color.light_green, "Running commands"); runBuildCommands(CommandType.postBuild, buildsettings.postBuildCommands, pack, m_project, settings, buildsettings, [["DUB_BUILD_PATH" : target_path.parentPath.toNativeString.absolutePath]]); } @@ -244,10 +244,10 @@ return false; } - logInfo("Building", Color.green, "%s %s: building configuration [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); + logInfo("Building", Color.light_green, "%s %s: building configuration [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); if( buildsettings.preBuildCommands.length ){ - logInfo("Pre-build", Color.green, "Running commands"); + logInfo("Pre-build", Color.light_green, "Running commands"); runBuildCommands(CommandType.preBuild, buildsettings.preBuildCommands, pack, m_project, settings, buildsettings); } @@ -309,11 +309,11 @@ flags ~= mainsrc.relativeTo(cwd).toNativeString(); if (buildsettings.preBuildCommands.length){ - logInfo("Pre-build", Color.green, "Running commands"); + logInfo("Pre-build", Color.light_green, "Running commands"); runCommands(buildsettings.preBuildCommands); } - logInfo("Building", Color.green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); + logInfo("Building", Color.light_green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); logInfo("Running rdmd..."); logDiagnostic("rdmd %s", join(flags, " ")); @@ -340,7 +340,7 @@ f = fp.toNativeString(); } - logInfo("Building", Color.green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); + logInfo("Building", Color.light_green, "%s %s [%s]", pack.name.color(Mode.bold), pack.version_, config.color(Color.blue)); // make all target/import paths relative string makeRelative(string path) { @@ -367,7 +367,7 @@ } if( buildsettings.preBuildCommands.length ){ - logInfo("Pre-build", Color.green, "Running commands"); + logInfo("Pre-build", Color.light_green, "Running commands"); runBuildCommands(CommandType.preBuild, buildsettings.preBuildCommands, pack, m_project, settings, buildsettings); } @@ -530,7 +530,7 @@ auto objs = new string[](srcs.walkLength); void compileSource(size_t i, string src) { - logInfo("Compiling", Color.green, "%s", src); + logInfo("Compiling", Color.light_green, "%s", src); const objPath = pathToObjName(settings.platform, src); objs[i] = compileUnit(src, objPath, buildsettings, settings); } @@ -541,7 +541,7 @@ foreach (i, src; srcs.array) compileSource(i, src); } - logInfo("Linking", Color.green, "%s", buildsettings.targetName.color(Mode.bold)); + logInfo("Linking", Color.light_green, "%s", buildsettings.targetName.color(Mode.bold)); lbuildsettings.sourceFiles = is_static_library ? [] : lbuildsettings.sourceFiles.filter!(f => isLinkerFile(settings.platform, f)).array; settings.compiler.setTarget(lbuildsettings, settings.platform); settings.compiler.prepareBuildSettings(lbuildsettings, settings.platform, BuildSetting.commandLineSeparate|BuildSetting.sourceFiles); @@ -580,7 +580,7 @@ settings.compiler.invoke(buildsettings, settings.platform, settings.compileCallback); if (generate_binary) { - logInfo("Linking", Color.green, "%s", buildsettings.targetName.color(Mode.bold)); + logInfo("Linking", Color.light_green, "%s", buildsettings.targetName.color(Mode.bold)); settings.compiler.invokeLinker(lbuildsettings, settings.platform, [tempobj.toNativeString()], settings.linkCallback); } } diff --git a/source/dub/generators/generator.d b/source/dub/generators/generator.d index c428df4..ba14278 100644 --- a/source/dub/generators/generator.d +++ b/source/dub/generators/generator.d @@ -891,7 +891,7 @@ in BuildSettings buildsettings) { if (buildsettings.preGenerateCommands.length && !isRecursiveInvocation(pack.name)) { - logInfo("Pre-gen", Color.green, "Running commands for %s", pack.name); + logInfo("Pre-gen", Color.light_green, "Running commands for %s", pack.name); runBuildCommands(CommandType.preGenerate, buildsettings.preGenerateCommands, pack, proj, settings, buildsettings); } } @@ -903,7 +903,7 @@ in BuildSettings buildsettings, NativePath target_path, bool generate_binary) { if (buildsettings.postGenerateCommands.length && !isRecursiveInvocation(pack.name)) { - logInfo("Post-gen", Color.green, "Running commands for %s", pack.name); + logInfo("Post-gen", Color.light_green, "Running commands for %s", pack.name); runBuildCommands(CommandType.postGenerate, buildsettings.postGenerateCommands, pack, proj, settings, buildsettings); } diff --git a/source/dub/internal/colorize/colors.d b/source/dub/internal/colorize/colors.d index f62ab26..e0e1f51 100644 --- a/source/dub/internal/colorize/colors.d +++ b/source/dub/internal/colorize/colors.d @@ -10,7 +10,7 @@ private template color_type(int offset) { - static enum type : int + enum type : int { init = 39 + offset, @@ -38,7 +38,7 @@ alias color_type!10 .type bg; // Text modes -static enum mode : int +enum mode : int { init = 0, bold = 1, @@ -64,7 +64,6 @@ * ); * --- */ - string color( const string str, const fg c=fg.init, @@ -90,71 +89,6 @@ ret = "This is red on blue blinking".color(fg.red, bg.blue, mode.blink); assert(ret == "\033[5;31;44mThis is red on blue blinking\033[0m"); - - ret = color("This is magenta", "magenta"); - assert(ret == "\033[35mThis is magenta\033[0m"); -} - -string colorHelper(const string str, const string name) pure -{ - int code; - - switch(name) - { - case "init": code = 39; break; - - case "black" : code = 30; break; - case "red" : code = 31; break; - case "green" : code = 32; break; - case "yellow" : code = 33; break; - case "blue" : code = 34; break; - case "magenta": code = 35; break; - case "cyan" : code = 36; break; - case "white" : code = 37; break; - - case "light_black" : code = 90; break; - case "light_red" : code = 91; break; - case "light_green" : code = 92; break; - case "light_yellow" : code = 93; break; - case "light_blue" : code = 94; break; - case "light_magenta": code = 95; break; - case "light_cyan" : code = 96; break; - case "light_white" : code = 97; break; - - case "bg_init": code = 49; break; - - case "bg_black" : code = 40; break; - case "bg_red" : code = 41; break; - case "bg_green" : code = 42; break; - case "bg_yellow" : code = 43; break; - case "bg_blue" : code = 44; break; - case "bg_magenta": code = 45; break; - case "bg_cyan" : code = 46; break; - case "bg_white" : code = 47; break; - - case "bg_light_black" : code = 100; break; - case "bg_light_red" : code = 101; break; - case "bg_light_green" : code = 102; break; - case "bg_light_yellow" : code = 103; break; - case "bg_light_blue" : code = 104; break; - case "bg_light_magenta": code = 105; break; - case "bg_light_cyan" : code = 106; break; - case "bg_light_white" : code = 107; break; - - case "mode_init": code = 0; break; - case "mode_bold" : code = 1; break; - case "mode_underline": code = 4; break; - case "mode_blink" : code = 5; break; - case "mode_swap" : code = 7; break; - case "mode_hide" : code = 8; break; - - default: - throw new Exception( - "Unknown fg color, bg color or mode \"" ~ name ~ "\"" - ); - } - - return format("\033[%dm%s\033[0m", code, str); } string colorHelper(T)(const string str, const T t=T.init) pure @@ -163,14 +97,10 @@ return format("\033[%dm%s\033[0m", t, str); } -alias colorHelper!bg background; -alias colorHelper!fg foreground; -alias colorHelper!mode style; - -alias background color; -alias foreground color; -alias style color; -alias colorHelper color; +alias background = colorHelper!bg; +alias foreground = colorHelper!fg; +alias style = colorHelper!mode; +alias color = colorHelper; unittest { diff --git a/source/dub/internal/logging.d b/source/dub/internal/logging.d index cc068c0..ecada55 100644 --- a/source/dub/internal/logging.d +++ b/source/dub/internal/logging.d @@ -105,20 +105,18 @@ /** This function must be called at the beginning for the program, before any logging occurs. It will detect whether or not stdout/stderr are a console/TTY - and will consequently disable colored output if needed. + and will consequently disable colored output if needed. Also, if a NO_COLOR + environment variable is defined, colors are disabled (https://no-color.org/). Forgetting to call the function will result in ASCII escape sequences in the - piped output, probably an undesiderable thing. + piped output, probably an undesirable thing. */ void initLogging() { + import std.process : environment; import core.stdc.stdio; - // Initially enable colors, we'll disable them during this functions if we - // find any reason to - _printColors = true; - - // The following stuff depends on the platform + _printColors = environment.get("NO_COLOR") == ""; version (Windows) { version (CRuntime_DigitalMars)