|
| 1 | +#include "config.h" // IWYU pragma: keep |
| 2 | + |
| 3 | +#include "CwdUtils.h" |
| 4 | + |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <wchar.h> |
| 8 | + |
| 9 | +#include "XUtils.h" |
| 10 | + |
| 11 | +typedef struct ShortenCwdContext { |
| 12 | + size_t maxLength; |
| 13 | + size_t len; |
| 14 | + wchar_t **parts; |
| 15 | + size_t partsLen; |
| 16 | + size_t *partLengths; |
| 17 | +} ShortenCwdContext; |
| 18 | + |
| 19 | +void ShortenCwdContext_init(ShortenCwdContext *ctx, wchar_t *cwd, size_t maxLength) { |
| 20 | + ctx->maxLength = maxLength; |
| 21 | + ctx->len = wcslen(cwd); |
| 22 | + ctx->parts = Wstring_split(cwd, L'/', &ctx->partsLen); |
| 23 | + ctx->partLengths = xCalloc(ctx->partsLen, sizeof(size_t)); |
| 24 | + for (size_t i = 0; i < ctx->partsLen; i++) |
| 25 | + ctx->partLengths[i] = wcslen(ctx->parts[i]); |
| 26 | +} |
| 27 | + |
| 28 | +void ShortenCwdContext_destroy(ShortenCwdContext *ctx) { |
| 29 | + for (size_t i = 0; i < ctx->partsLen; i++) |
| 30 | + free(ctx->parts[i]); |
| 31 | + free(ctx->parts); |
| 32 | + free(ctx->partLengths); |
| 33 | + *ctx = (ShortenCwdContext){0}; |
| 34 | +} |
| 35 | + |
| 36 | +static void shortenCwdParts(ShortenCwdContext *ctx) { |
| 37 | + for (size_t i = ctx->partsLen - 2; i != (size_t)-1 && ctx->len > ctx->maxLength; i--) { |
| 38 | + if (ctx->partLengths[i] < 3) |
| 39 | + continue; |
| 40 | + |
| 41 | + size_t extraChars = ctx->len - ctx->maxLength; |
| 42 | + size_t maxRemovableChars = ctx->partLengths[i] - 2; |
| 43 | + size_t charsToRemove = extraChars < maxRemovableChars ? extraChars : maxRemovableChars; |
| 44 | + |
| 45 | + ctx->partLengths[i] -= charsToRemove; |
| 46 | + ctx->len -= charsToRemove; |
| 47 | + |
| 48 | + Wstring_safeWcsncpy(ctx->parts[i] + (ctx->partLengths[i] - 1), L"~", 2); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static size_t collapseCwdParts(ShortenCwdContext *ctx, bool doActualWork) { |
| 53 | + if (ctx->len <= ctx->maxLength || ctx->partsLen <= 3) |
| 54 | + return 0; |
| 55 | + |
| 56 | + size_t len = ctx->len; |
| 57 | + |
| 58 | + size_t i; |
| 59 | + for (i = ctx->partsLen - 2; i > 1; i--) { |
| 60 | + if (len + (3 - ctx->partLengths[i]) <= ctx->maxLength) |
| 61 | + break; |
| 62 | + |
| 63 | + len -= ctx->partLengths[i] + 1; |
| 64 | + |
| 65 | + if (doActualWork) { |
| 66 | + ctx->partLengths[i] = 0; |
| 67 | + free(ctx->parts[i]); |
| 68 | + ctx->parts[i] = NULL; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + len += 3 - ctx->partLengths[i]; |
| 73 | + size_t diff = ctx->len - len; |
| 74 | + |
| 75 | + if (doActualWork) { |
| 76 | + wchar_t newPart[] = L"~~~"; |
| 77 | + newPart[0] = ctx->parts[i][0]; |
| 78 | + free(ctx->parts[i]); |
| 79 | + ctx->parts[i] = xWcsdup(newPart); |
| 80 | + ctx->partLengths[i] = 3; |
| 81 | + ctx->len = len; |
| 82 | + } |
| 83 | + |
| 84 | + return diff; |
| 85 | +} |
| 86 | + |
| 87 | +static size_t shortenCwdLastPart(ShortenCwdContext *ctx, bool doActualWork) { |
| 88 | + if (ctx->len <= ctx->maxLength) |
| 89 | + return 0; |
| 90 | + |
| 91 | + size_t lastPartLen = ctx->partLengths[ctx->partsLen - 1]; |
| 92 | + if (lastPartLen <= 3) |
| 93 | + return 0; |
| 94 | + |
| 95 | + wchar_t *lastPart = ctx->parts[ctx->partsLen - 1]; |
| 96 | + size_t extraChars = ctx->len - ctx->maxLength + 1; |
| 97 | + size_t maxRemovableChars = lastPartLen - 2; |
| 98 | + size_t charsToRemove = extraChars < maxRemovableChars ? extraChars : maxRemovableChars; |
| 99 | + |
| 100 | + if (doActualWork) { |
| 101 | + size_t charsAtBeginning = (lastPartLen - charsToRemove + 1) / 2; |
| 102 | + size_t charsAtEnd = lastPartLen - charsToRemove - charsAtBeginning; |
| 103 | + lastPart[charsAtBeginning] = '~'; |
| 104 | + wmemmove(lastPart + charsAtBeginning + 1, lastPart + lastPartLen - charsAtEnd, charsAtEnd); |
| 105 | + lastPart[charsAtBeginning + charsAtEnd + 1] = '\0'; |
| 106 | + ctx->partLengths[ctx->partsLen - 1] = lastPartLen - charsToRemove + 1; |
| 107 | + ctx->len -= charsToRemove - 1; |
| 108 | + } |
| 109 | + |
| 110 | + return charsToRemove - 1; |
| 111 | +} |
| 112 | + |
| 113 | +static wchar_t* buildCwdFromParts(ShortenCwdContext *ctx) { |
| 114 | + size_t len = ctx->partsLen - 1; |
| 115 | + for (size_t i = 0; i < ctx->partsLen; i++) |
| 116 | + len += ctx->partLengths[i]; |
| 117 | + |
| 118 | + wchar_t *newCwd = xCalloc(len + 1, sizeof(wchar_t)); |
| 119 | + |
| 120 | + newCwd[0] = '\0'; |
| 121 | + for (size_t i = 0, writeIndex = 0; i < ctx->partsLen; i++) { |
| 122 | + if (!ctx->parts[i]) |
| 123 | + continue; |
| 124 | + |
| 125 | + Wstring_safeWcsncpy(newCwd + writeIndex, ctx->parts[i], ctx->partLengths[i] + 1); |
| 126 | + writeIndex += ctx->partLengths[i]; |
| 127 | + if (i < ctx->partsLen - 1) |
| 128 | + newCwd[writeIndex++] = L'/'; |
| 129 | + } |
| 130 | + |
| 131 | + return newCwd; |
| 132 | +} |
| 133 | + |
| 134 | +char* CwdUtils_shortenCwd(const char *cwd, const size_t maxLength) { |
| 135 | + wchar_t *wcwd = xMbstowcs(cwd); |
| 136 | + size_t len = wcslen(wcwd); |
| 137 | + if (len <= maxLength) { |
| 138 | + free(wcwd); |
| 139 | + return xStrdup(cwd); |
| 140 | + } |
| 141 | + |
| 142 | + ShortenCwdContext ctx; |
| 143 | + ShortenCwdContext_init(&ctx, wcwd, maxLength); |
| 144 | + free(wcwd); |
| 145 | + wcwd = NULL; |
| 146 | + |
| 147 | + shortenCwdParts(&ctx); |
| 148 | + if (shortenCwdLastPart(&ctx, false) > collapseCwdParts(&ctx, false)) { |
| 149 | + shortenCwdLastPart(&ctx, true); |
| 150 | + collapseCwdParts(&ctx, true); |
| 151 | + } else { |
| 152 | + collapseCwdParts(&ctx, true); |
| 153 | + shortenCwdLastPart(&ctx, true); |
| 154 | + } |
| 155 | + |
| 156 | + wchar_t *newWcwd = buildCwdFromParts(&ctx); |
| 157 | + char *newCwd = xWcstombs(newWcwd); |
| 158 | + free(newWcwd); |
| 159 | + |
| 160 | + ShortenCwdContext_destroy(&ctx); |
| 161 | + |
| 162 | + return newCwd; |
| 163 | +} |
0 commit comments