Fossil SCM
Improvements to the "fossil sys ls" command.
Commit
ab03e5067e307485c1b17e04a84aa6b0737cdba5f9246789262fcdba742320b4
Parent
27baef45ca87c48…
1 file changed
+61
-11
+61
-11
| --- src/xsystem.c | ||
| +++ src/xsystem.c | ||
| @@ -82,20 +82,28 @@ | ||
| 82 | 82 | char *aList[2]; |
| 83 | 83 | char **azList; |
| 84 | 84 | int nList; |
| 85 | 85 | int i; |
| 86 | 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; | |
| 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 | + } | |
| 97 | 105 | } |
| 98 | 106 | for(i=0; i<nList; i++){ |
| 99 | 107 | char *zFile = zPrefix ? mprintf("%s/%s",zPrefix,azList[i]) : azList[i]; |
| 100 | 108 | int mode = file_mode(zFile, ExtFILE); |
| 101 | 109 | sqlite3_int64 sz = file_size(zFile, ExtFILE); |
| @@ -121,11 +129,11 @@ | ||
| 121 | 129 | int mFlags |
| 122 | 130 | ){ |
| 123 | 131 | sqlite3_stmt *pStmt; |
| 124 | 132 | int bDesc = (mFlags & 0x02)!=0; |
| 125 | 133 | if( mFlags & 0x04 ) bDesc = !bDesc; |
| 126 | - if( 1 ){ | |
| 134 | + if( (mFlags & 0x01)!=0 ){ | |
| 127 | 135 | /* Long mode */ |
| 128 | 136 | char *zSql; |
| 129 | 137 | zSql = mprintf( |
| 130 | 138 | "SELECT mode, size, strftime('%%Y-%%m-%%d %%H:%%M'," |
| 131 | 139 | "mtime,'unixepoch'), fn" |
| @@ -163,10 +171,52 @@ | ||
| 163 | 171 | sqlite3_column_int64(pStmt, 1), |
| 164 | 172 | sqlite3_column_text(pStmt, 2), |
| 165 | 173 | zName); |
| 166 | 174 | } |
| 167 | 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); | |
| 168 | 218 | } |
| 169 | 219 | sqlite3_exec(db, "DELETE FROM ls;", 0, 0, 0); |
| 170 | 220 | } |
| 171 | 221 | |
| 172 | 222 | /* List files "ls" |
| 173 | 223 |
| --- 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 |