Fossil SCM

Improvements to the "fossil sys ls" command.

drh 2025-11-01 23:15 system-cmd
Commit ab03e5067e307485c1b17e04a84aa6b0737cdba5f9246789262fcdba742320b4
1 file changed +61 -11
+61 -11
--- src/xsystem.c
+++ src/xsystem.c
@@ -82,20 +82,28 @@
8282
char *aList[2];
8383
char **azList;
8484
int nList;
8585
int i;
8686
const char *zPrefix;
87
- if( file_isdir(zName, ExtFILE)==1 ){
88
- azList = 0;
89
- nList = file_directory_list(zName, 0, (mFlags & 0x08)==0, 0, &azList);
90
- zPrefix = fossil_strcmp(zName,".") ? zName : 0;
91
- }else{
92
- aList[0] = (char*)zName;
93
- aList[1] = 0;
94
- azList = aList;
95
- nList = 1;
96
- zPrefix = 0;
87
+ switch( file_isdir(zName, ExtFILE) ){
88
+ case 1: { /* A directory */
89
+ azList = 0;
90
+ nList = file_directory_list(zName, 0, (mFlags & 0x08)==0, 0, &azList);
91
+ zPrefix = fossil_strcmp(zName,".") ? zName : 0;
92
+ break;
93
+ }
94
+ case 2: { /* A file */
95
+ aList[0] = (char*)zName;
96
+ aList[1] = 0;
97
+ azList = aList;
98
+ nList = 1;
99
+ zPrefix = 0;
100
+ break;
101
+ }
102
+ default: { /* Does not exist */
103
+ return;
104
+ }
97105
}
98106
for(i=0; i<nList; i++){
99107
char *zFile = zPrefix ? mprintf("%s/%s",zPrefix,azList[i]) : azList[i];
100108
int mode = file_mode(zFile, ExtFILE);
101109
sqlite3_int64 sz = file_size(zFile, ExtFILE);
@@ -121,11 +129,11 @@
121129
int mFlags
122130
){
123131
sqlite3_stmt *pStmt;
124132
int bDesc = (mFlags & 0x02)!=0;
125133
if( mFlags & 0x04 ) bDesc = !bDesc;
126
- if( 1 ){
134
+ if( (mFlags & 0x01)!=0 ){
127135
/* Long mode */
128136
char *zSql;
129137
zSql = mprintf(
130138
"SELECT mode, size, strftime('%%Y-%%m-%%d %%H:%%M',"
131139
"mtime,'unixepoch'), fn"
@@ -163,10 +171,52 @@
163171
sqlite3_column_int64(pStmt, 1),
164172
sqlite3_column_text(pStmt, 2),
165173
zName);
166174
}
167175
sqlite3_finalize(pStmt);
176
+ }else{
177
+ /* Column mode with just filenames */
178
+ int nCol, mxWidth, iRow, nSp, nRow;
179
+ char *zSql;
180
+ char *zOrderBy;
181
+ sqlite3_prepare_v2(db, "SELECT max(length(fn)),count(*) FROM ls",-1,
182
+ &pStmt,0);
183
+ if( sqlite3_step(pStmt)==SQLITE_ROW ){
184
+ mxWidth = sqlite3_column_int(pStmt,0);
185
+ nCol = (terminal_get_width(80)+1)/(mxWidth+2);
186
+ if( nCol<1 ) nCol = 1;
187
+ nRow = (sqlite3_column_int(pStmt,1)+nCol-1)/nCol;
188
+ }else{
189
+ nCol = 1;
190
+ mxWidth = 100;
191
+ nRow = 2000000;
192
+ }
193
+ sqlite3_finalize(pStmt);
194
+ zOrderBy = mprintf("%s %s", (mFlags & 0x04)!=0 ? "mtime": "fn",
195
+ bDesc ? "DESC" : "ASC");
196
+ zSql = mprintf("WITH sfn(ii,fn,mtime) AS "
197
+ "(SELECT row_number()OVER(ORDER BY %s)-1,fn,mtime FROM ls)"
198
+ "SELECT ii/%d,ii%%%d, fn FROM sfn ORDER BY 2,1",
199
+ zOrderBy, nRow, nRow);
200
+ fossil_free(zOrderBy);
201
+ sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
202
+ nSp = 0;
203
+ iRow = -1;
204
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
205
+ const char *zFN = (const char*)sqlite3_column_text(pStmt, 2);
206
+ int thisRow = sqlite3_column_int(pStmt,1);
207
+ if( iRow!=thisRow ){
208
+ if( iRow>=0 ) fossil_print("\n");
209
+ iRow = thisRow;
210
+ }else{
211
+ if( nSp ) fossil_print("%*s",nSp,"");
212
+ }
213
+ fossil_print("%s", zFN);
214
+ nSp = mxWidth - (int)strlen(zFN) + 2;
215
+ }
216
+ fossil_print("\n");
217
+ sqlite3_finalize(pStmt);
168218
}
169219
sqlite3_exec(db, "DELETE FROM ls;", 0, 0, 0);
170220
}
171221
172222
/* List files "ls"
173223
--- src/xsystem.c
+++ src/xsystem.c
@@ -82,20 +82,28 @@
82 char *aList[2];
83 char **azList;
84 int nList;
85 int i;
86 const char *zPrefix;
87 if( file_isdir(zName, ExtFILE)==1 ){
88 azList = 0;
89 nList = file_directory_list(zName, 0, (mFlags & 0x08)==0, 0, &azList);
90 zPrefix = fossil_strcmp(zName,".") ? zName : 0;
91 }else{
92 aList[0] = (char*)zName;
93 aList[1] = 0;
94 azList = aList;
95 nList = 1;
96 zPrefix = 0;
 
 
 
 
 
 
 
 
97 }
98 for(i=0; i<nList; i++){
99 char *zFile = zPrefix ? mprintf("%s/%s",zPrefix,azList[i]) : azList[i];
100 int mode = file_mode(zFile, ExtFILE);
101 sqlite3_int64 sz = file_size(zFile, ExtFILE);
@@ -121,11 +129,11 @@
121 int mFlags
122 ){
123 sqlite3_stmt *pStmt;
124 int bDesc = (mFlags & 0x02)!=0;
125 if( mFlags & 0x04 ) bDesc = !bDesc;
126 if( 1 ){
127 /* Long mode */
128 char *zSql;
129 zSql = mprintf(
130 "SELECT mode, size, strftime('%%Y-%%m-%%d %%H:%%M',"
131 "mtime,'unixepoch'), fn"
@@ -163,10 +171,52 @@
163 sqlite3_column_int64(pStmt, 1),
164 sqlite3_column_text(pStmt, 2),
165 zName);
166 }
167 sqlite3_finalize(pStmt);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168 }
169 sqlite3_exec(db, "DELETE FROM ls;", 0, 0, 0);
170 }
171
172 /* List files "ls"
173
--- src/xsystem.c
+++ src/xsystem.c
@@ -82,20 +82,28 @@
82 char *aList[2];
83 char **azList;
84 int nList;
85 int i;
86 const char *zPrefix;
87 switch( file_isdir(zName, ExtFILE) ){
88 case 1: { /* A directory */
89 azList = 0;
90 nList = file_directory_list(zName, 0, (mFlags & 0x08)==0, 0, &azList);
91 zPrefix = fossil_strcmp(zName,".") ? zName : 0;
92 break;
93 }
94 case 2: { /* A file */
95 aList[0] = (char*)zName;
96 aList[1] = 0;
97 azList = aList;
98 nList = 1;
99 zPrefix = 0;
100 break;
101 }
102 default: { /* Does not exist */
103 return;
104 }
105 }
106 for(i=0; i<nList; i++){
107 char *zFile = zPrefix ? mprintf("%s/%s",zPrefix,azList[i]) : azList[i];
108 int mode = file_mode(zFile, ExtFILE);
109 sqlite3_int64 sz = file_size(zFile, ExtFILE);
@@ -121,11 +129,11 @@
129 int mFlags
130 ){
131 sqlite3_stmt *pStmt;
132 int bDesc = (mFlags & 0x02)!=0;
133 if( mFlags & 0x04 ) bDesc = !bDesc;
134 if( (mFlags & 0x01)!=0 ){
135 /* Long mode */
136 char *zSql;
137 zSql = mprintf(
138 "SELECT mode, size, strftime('%%Y-%%m-%%d %%H:%%M',"
139 "mtime,'unixepoch'), fn"
@@ -163,10 +171,52 @@
171 sqlite3_column_int64(pStmt, 1),
172 sqlite3_column_text(pStmt, 2),
173 zName);
174 }
175 sqlite3_finalize(pStmt);
176 }else{
177 /* Column mode with just filenames */
178 int nCol, mxWidth, iRow, nSp, nRow;
179 char *zSql;
180 char *zOrderBy;
181 sqlite3_prepare_v2(db, "SELECT max(length(fn)),count(*) FROM ls",-1,
182 &pStmt,0);
183 if( sqlite3_step(pStmt)==SQLITE_ROW ){
184 mxWidth = sqlite3_column_int(pStmt,0);
185 nCol = (terminal_get_width(80)+1)/(mxWidth+2);
186 if( nCol<1 ) nCol = 1;
187 nRow = (sqlite3_column_int(pStmt,1)+nCol-1)/nCol;
188 }else{
189 nCol = 1;
190 mxWidth = 100;
191 nRow = 2000000;
192 }
193 sqlite3_finalize(pStmt);
194 zOrderBy = mprintf("%s %s", (mFlags & 0x04)!=0 ? "mtime": "fn",
195 bDesc ? "DESC" : "ASC");
196 zSql = mprintf("WITH sfn(ii,fn,mtime) AS "
197 "(SELECT row_number()OVER(ORDER BY %s)-1,fn,mtime FROM ls)"
198 "SELECT ii/%d,ii%%%d, fn FROM sfn ORDER BY 2,1",
199 zOrderBy, nRow, nRow);
200 fossil_free(zOrderBy);
201 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
202 nSp = 0;
203 iRow = -1;
204 while( sqlite3_step(pStmt)==SQLITE_ROW ){
205 const char *zFN = (const char*)sqlite3_column_text(pStmt, 2);
206 int thisRow = sqlite3_column_int(pStmt,1);
207 if( iRow!=thisRow ){
208 if( iRow>=0 ) fossil_print("\n");
209 iRow = thisRow;
210 }else{
211 if( nSp ) fossil_print("%*s",nSp,"");
212 }
213 fossil_print("%s", zFN);
214 nSp = mxWidth - (int)strlen(zFN) + 2;
215 }
216 fossil_print("\n");
217 sqlite3_finalize(pStmt);
218 }
219 sqlite3_exec(db, "DELETE FROM ls;", 0, 0, 0);
220 }
221
222 /* List files "ls"
223

Keyboard Shortcuts

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