Fossil SCM

Colorize the output of the "fossil sys ls" command.

drh 2025-11-25 16:42 UTC trunk
Commit b91ec5ad0e2b227da9fe5f5a3aa98132832b7e8b553fd295403a84fec7810997
1 file changed +39 -3
+39 -3
--- src/xsystem.c
+++ src/xsystem.c
@@ -171,19 +171,53 @@
171171
if( mFlags & LS_MTIME ) i = 1;
172172
if( mFlags & LS_SIZE ) i = 2;
173173
if( mFlags & LS_REVERSE ) i += 3;
174174
return zSortTypes[i];
175175
}
176
+
177
+/*
178
+** colorize_fn(fn,mode)
179
+**
180
+** SQL function to colorize a filename based on its mode.
181
+*/
182
+static void colorNameFunc(
183
+ sqlite3_context *context,
184
+ int argc,
185
+ sqlite3_value **argv
186
+){
187
+ const char *zName = (const char*)sqlite3_value_text(argv[0]);
188
+ int iMode = sqlite3_value_int(argv[1]);
189
+ sqlite3_str *pOut;
190
+ if( zName==0 ) return;
191
+ pOut = sqlite3_str_new(0);
192
+#ifdef _WIN32
193
+ if( sqlite3_strlike("%.exe",zName,0)==0 ) iMode |= 0111;
194
+#endif
195
+ if( iMode & 040000 ){
196
+ /* A directory */
197
+ sqlite3_str_appendall(pOut, "\033[1;34m");
198
+ }else if( iMode & 0100 ){
199
+ /* Executable */
200
+ sqlite3_str_appendall(pOut, "\033[1;32m");
201
+ }
202
+ sqlite3_str_appendall(pOut, zName);
203
+ if( (iMode & 040100)!=0 ){
204
+ sqlite3_str_appendall(pOut, "\033[0m");
205
+ }
206
+ sqlite3_result_text(context, sqlite3_str_finish(pOut), -1, sqlite3_free);
207
+}
208
+
176209
177210
/*
178211
** Show ls output information for content in the LS table
179212
*/
180213
static void xsystem_ls_render(
181214
sqlite3 *db,
182215
int mFlags
183216
){
184217
sqlite3_stmt *pStmt;
218
+ sqlite3_create_function(db, "color", 2, SQLITE_UTF8, 0, colorNameFunc,0,0);
185219
if( (mFlags & LS_LONG)!=0 ){
186220
/* Long mode */
187221
char *zSql;
188222
int szSz = 8;
189223
sqlite3_prepare_v2(db, "SELECT length(max(size)) FROM ls", -1, &pStmt, 0);
@@ -191,11 +225,11 @@
191225
szSz = sqlite3_column_int(pStmt, 0);
192226
}
193227
sqlite3_finalize(pStmt);
194228
pStmt = 0;
195229
zSql = mprintf(
196
- "SELECT mode, size, datetime(mtime,'unixepoch'), fn"
230
+ "SELECT mode, size, datetime(mtime,'unixepoch'), color(fn,mode)"
197231
" FROM ls ORDER BY %s",
198232
xsystem_ls_orderby(mFlags));
199233
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
200234
while( sqlite3_step(pStmt)==SQLITE_ROW ){
201235
char zMode[12];
@@ -233,11 +267,11 @@
233267
}else if( (mFlags & LS_COMMA)!=0 ){
234268
/* Comma-separate list */
235269
int mx = terminal_get_width(80);
236270
int sumW = 0;
237271
char *zSql;
238
- zSql = mprintf("SELECT fn, dlen FROM ls ORDER BY %s",
272
+ zSql = mprintf("SELECT color(fn,mode), dlen FROM ls ORDER BY %s",
239273
xsystem_ls_orderby(mFlags));
240274
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
241275
while( sqlite3_step(pStmt)==SQLITE_ROW ){
242276
const char *z = (const char*)sqlite3_column_text(pStmt, 0);
243277
int w = sqlite3_column_int(pStmt, 1);
@@ -263,12 +297,14 @@
263297
spec.iVersion = 1;
264298
spec.xWrite = xsystem_write;
265299
spec.eStyle = QRF_STYLE_Column;
266300
spec.bSplitColumn = QRF_Yes;
267301
spec.bTitles = QRF_No;
302
+ spec.eEsc = QRF_No;
268303
spec.nScreenWidth = terminal_get_width(80);
269
- zSql = mprintf("SELECT fn FROM ls ORDER BY %s", xsystem_ls_orderby(mFlags));
304
+ zSql = mprintf("SELECT color(fn,mode) FROM ls ORDER BY %s",
305
+ xsystem_ls_orderby(mFlags));
270306
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
271307
fossil_free(zSql);
272308
sqlite3_format_query_result(pStmt, &spec, 0);
273309
sqlite3_finalize(pStmt);
274310
}
275311
--- src/xsystem.c
+++ src/xsystem.c
@@ -171,19 +171,53 @@
171 if( mFlags & LS_MTIME ) i = 1;
172 if( mFlags & LS_SIZE ) i = 2;
173 if( mFlags & LS_REVERSE ) i += 3;
174 return zSortTypes[i];
175 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177 /*
178 ** Show ls output information for content in the LS table
179 */
180 static void xsystem_ls_render(
181 sqlite3 *db,
182 int mFlags
183 ){
184 sqlite3_stmt *pStmt;
 
185 if( (mFlags & LS_LONG)!=0 ){
186 /* Long mode */
187 char *zSql;
188 int szSz = 8;
189 sqlite3_prepare_v2(db, "SELECT length(max(size)) FROM ls", -1, &pStmt, 0);
@@ -191,11 +225,11 @@
191 szSz = sqlite3_column_int(pStmt, 0);
192 }
193 sqlite3_finalize(pStmt);
194 pStmt = 0;
195 zSql = mprintf(
196 "SELECT mode, size, datetime(mtime,'unixepoch'), fn"
197 " FROM ls ORDER BY %s",
198 xsystem_ls_orderby(mFlags));
199 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
200 while( sqlite3_step(pStmt)==SQLITE_ROW ){
201 char zMode[12];
@@ -233,11 +267,11 @@
233 }else if( (mFlags & LS_COMMA)!=0 ){
234 /* Comma-separate list */
235 int mx = terminal_get_width(80);
236 int sumW = 0;
237 char *zSql;
238 zSql = mprintf("SELECT fn, dlen FROM ls ORDER BY %s",
239 xsystem_ls_orderby(mFlags));
240 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
241 while( sqlite3_step(pStmt)==SQLITE_ROW ){
242 const char *z = (const char*)sqlite3_column_text(pStmt, 0);
243 int w = sqlite3_column_int(pStmt, 1);
@@ -263,12 +297,14 @@
263 spec.iVersion = 1;
264 spec.xWrite = xsystem_write;
265 spec.eStyle = QRF_STYLE_Column;
266 spec.bSplitColumn = QRF_Yes;
267 spec.bTitles = QRF_No;
 
268 spec.nScreenWidth = terminal_get_width(80);
269 zSql = mprintf("SELECT fn FROM ls ORDER BY %s", xsystem_ls_orderby(mFlags));
 
270 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
271 fossil_free(zSql);
272 sqlite3_format_query_result(pStmt, &spec, 0);
273 sqlite3_finalize(pStmt);
274 }
275
--- src/xsystem.c
+++ src/xsystem.c
@@ -171,19 +171,53 @@
171 if( mFlags & LS_MTIME ) i = 1;
172 if( mFlags & LS_SIZE ) i = 2;
173 if( mFlags & LS_REVERSE ) i += 3;
174 return zSortTypes[i];
175 }
176
177 /*
178 ** colorize_fn(fn,mode)
179 **
180 ** SQL function to colorize a filename based on its mode.
181 */
182 static void colorNameFunc(
183 sqlite3_context *context,
184 int argc,
185 sqlite3_value **argv
186 ){
187 const char *zName = (const char*)sqlite3_value_text(argv[0]);
188 int iMode = sqlite3_value_int(argv[1]);
189 sqlite3_str *pOut;
190 if( zName==0 ) return;
191 pOut = sqlite3_str_new(0);
192 #ifdef _WIN32
193 if( sqlite3_strlike("%.exe",zName,0)==0 ) iMode |= 0111;
194 #endif
195 if( iMode & 040000 ){
196 /* A directory */
197 sqlite3_str_appendall(pOut, "\033[1;34m");
198 }else if( iMode & 0100 ){
199 /* Executable */
200 sqlite3_str_appendall(pOut, "\033[1;32m");
201 }
202 sqlite3_str_appendall(pOut, zName);
203 if( (iMode & 040100)!=0 ){
204 sqlite3_str_appendall(pOut, "\033[0m");
205 }
206 sqlite3_result_text(context, sqlite3_str_finish(pOut), -1, sqlite3_free);
207 }
208
209
210 /*
211 ** Show ls output information for content in the LS table
212 */
213 static void xsystem_ls_render(
214 sqlite3 *db,
215 int mFlags
216 ){
217 sqlite3_stmt *pStmt;
218 sqlite3_create_function(db, "color", 2, SQLITE_UTF8, 0, colorNameFunc,0,0);
219 if( (mFlags & LS_LONG)!=0 ){
220 /* Long mode */
221 char *zSql;
222 int szSz = 8;
223 sqlite3_prepare_v2(db, "SELECT length(max(size)) FROM ls", -1, &pStmt, 0);
@@ -191,11 +225,11 @@
225 szSz = sqlite3_column_int(pStmt, 0);
226 }
227 sqlite3_finalize(pStmt);
228 pStmt = 0;
229 zSql = mprintf(
230 "SELECT mode, size, datetime(mtime,'unixepoch'), color(fn,mode)"
231 " FROM ls ORDER BY %s",
232 xsystem_ls_orderby(mFlags));
233 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
234 while( sqlite3_step(pStmt)==SQLITE_ROW ){
235 char zMode[12];
@@ -233,11 +267,11 @@
267 }else if( (mFlags & LS_COMMA)!=0 ){
268 /* Comma-separate list */
269 int mx = terminal_get_width(80);
270 int sumW = 0;
271 char *zSql;
272 zSql = mprintf("SELECT color(fn,mode), dlen FROM ls ORDER BY %s",
273 xsystem_ls_orderby(mFlags));
274 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
275 while( sqlite3_step(pStmt)==SQLITE_ROW ){
276 const char *z = (const char*)sqlite3_column_text(pStmt, 0);
277 int w = sqlite3_column_int(pStmt, 1);
@@ -263,12 +297,14 @@
297 spec.iVersion = 1;
298 spec.xWrite = xsystem_write;
299 spec.eStyle = QRF_STYLE_Column;
300 spec.bSplitColumn = QRF_Yes;
301 spec.bTitles = QRF_No;
302 spec.eEsc = QRF_No;
303 spec.nScreenWidth = terminal_get_width(80);
304 zSql = mprintf("SELECT color(fn,mode) FROM ls ORDER BY %s",
305 xsystem_ls_orderby(mFlags));
306 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
307 fossil_free(zSql);
308 sqlite3_format_query_result(pStmt, &spec, 0);
309 sqlite3_finalize(pStmt);
310 }
311

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button