Skip to content

feat: log supports multiple colors #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path, { posix } from 'node:path';
import { type Options as FdirOptions, fdir } from 'fdir';
import picomatch from 'picomatch';
import { escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts';
import { coloredLog, escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts';

const PARENT_DIRECTORY = /^(\/?\.\.)+/;
const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g;
Expand Down Expand Up @@ -207,7 +207,7 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
const matches = matcher(path);

if (matches) {
log(`matched ${path}`);
coloredLog(`matched ${path}`, 'matched');
}

return matches;
Expand All @@ -220,9 +220,9 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
const skipped = (relativePath !== '.' && !partialMatcher(relativePath)) || ignore(relativePath);

if (skipped) {
log(`skipped ${p}`);
coloredLog(`skipped ${p}`, 'skipped');
} else {
log(`crawling ${p}`);
coloredLog(`crawling ${p}`, 'crawling');
}

return skipped;
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,17 @@ export function isDynamicPattern(pattern: string, options?: { caseSensitiveMatch
export function log(...tasks: unknown[]): void {
console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, ...tasks);
}
export function coloredLog(msg: string, type: 'matched' | 'skipped' | 'crawling'): void {
const colors = {
matched: '\x1b[32m',
skipped: '\x1b[90m',
crawling: '\x1b[34m'
};
const color = colors[type as keyof typeof colors];
if (color) {
console.log(`${color}%s\x1b[0m`, `[tinyglobby ${new Date().toLocaleTimeString('es')}]${msg}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardcoding it to spanish?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one isn't changed on this pr and comes from main, it's for hardcoding time to be in the hh:mm:ss format, couldn't find a "international" locale that did this

Copy link
Contributor

@benmccann benmccann May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense. Could be worth a comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swedish is quite often used for this kinda thing but that's a bit risky though, I've opened an issue in NodeJS a while ago for date string not respecting 2 digits with 0 padding in Node 22. Then a Node maintainer replied with this comment

Data changes constantly. Look at https://www.unicode.org/cldr/charts/44/delta/index.html and pick any locale. toLocaleString() is completely inappropriate as an API to expect a constant format. It should display what's correct for Swedish, which might change over time.

It's usually safer to use new Date(date).toISOString() even though it's UTC TZ

return;
}
console.log(`[tinyglobby ${new Date().toLocaleTimeString('es')}]`, msg);
}
// #endregion