Skip to content

Commit e68abd0

Browse files
committed
Show line texts when found
1 parent 485d202 commit e68abd0

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

src/sys/xfs.c

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,43 +1172,79 @@ static int XFile_CheckCriteria(xfile_search_t *pSearch, const char *pPath, const
11721172
if (pSearch->bInsensitive) xstrcase((char*)pBuffer, XSTR_LOWER);
11731173
int nPosit = xstrsrc(pBuffer, pSearch->sText);
11741174

1175-
if (nPosit >= 0 && pSearch->bSearchLines)
1175+
if (nPosit >= 0)
11761176
{
1177-
xarray_t *pArr = xstrsplite(pBuffer, "\n");
1178-
if (pArr != NULL)
1177+
if (pSearch->bSearchLines)
11791178
{
1180-
size_t i = 0;
1181-
for (i = 0; i < pArr->nUsed; i++)
1179+
xarray_t *pArr = xstrsplite(pBuffer, "\n");
1180+
if (pArr != NULL)
11821181
{
1183-
char *pLine = (char*)XArray_GetData(pArr, i);
1184-
if (xstrsrc(pLine, pSearch->sText) >= 0)
1182+
size_t i = 0;
1183+
for (i = 0; i < pArr->nUsed; i++)
11851184
{
1186-
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1187-
if (pEntry == NULL)
1185+
char *pLine = (char*)XArray_GetData(pArr, i);
1186+
if (xstrsrc(pLine, pSearch->sText) >= 0)
11881187
{
1189-
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1190-
XByteBuffer_Clear(&buffer);
1191-
XArray_Destroy(pArr);
1192-
return XSTDERR;
1188+
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1189+
if (pEntry == NULL)
1190+
{
1191+
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1192+
XByteBuffer_Clear(&buffer);
1193+
XArray_Destroy(pArr);
1194+
return XSTDERR;
1195+
}
1196+
1197+
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1198+
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1199+
pEntry->nLineNum = i + 1;
1200+
1201+
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1202+
{
1203+
XByteBuffer_Clear(&buffer);
1204+
XArray_Destroy(pArr);
1205+
return XSTDERR;
1206+
}
11931207
}
1208+
}
1209+
}
1210+
1211+
XByteBuffer_Clear(&buffer);
1212+
XArray_Destroy(pArr);
1213+
return XSTDNON;
1214+
}
1215+
else
1216+
{
1217+
while (nPosit && pBuffer[nPosit] != '\n') nPosit--;
1218+
if (pBuffer[nPosit] == '\n') nPosit++;
11941219

1195-
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1196-
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1197-
pEntry->nLineNum = i + 1;
1220+
int nEnd = xstrsrc(&pBuffer[nPosit], "\n");
1221+
if (nEnd > 0)
1222+
{
1223+
char sLine[XSTR_MAX];
1224+
xstrncpys(sLine, sizeof(sLine), &pBuffer[nPosit], nEnd);
11981225

1199-
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1200-
{
1201-
XByteBuffer_Clear(&buffer);
1202-
XArray_Destroy(pArr);
1203-
return XSTDERR;
1204-
}
1226+
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1227+
if (pEntry == NULL)
1228+
{
1229+
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1230+
XByteBuffer_Clear(&buffer);
1231+
return XSTDERR;
12051232
}
1233+
1234+
char *pLine = sLine;
1235+
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1236+
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1237+
1238+
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1239+
{
1240+
XByteBuffer_Clear(&buffer);
1241+
return XSTDERR;
1242+
}
1243+
1244+
XByteBuffer_Clear(&buffer);
1245+
return XSTDNON;
12061246
}
12071247
}
1208-
1209-
XByteBuffer_Clear(&buffer);
1210-
XArray_Destroy(pArr);
1211-
return XSTDNON;
12121248
}
12131249

12141250
XByteBuffer_Clear(&buffer);

tools/xsrc.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static void XSearch_ColorizeSymlink(char *pSimlink, size_t nSize, xfile_entry_t
344344

345345
static void XSearch_ColorizeLine(char *pDst, size_t nSize, xfile_entry_t *pEntry, const char *pText)
346346
{
347-
if (!xstrused(pText) || !pEntry->nLineNum) return;
347+
if (!xstrused(pText) || !xstrused(pEntry->sLine)) return;
348348
xarray_t *pArr = xstrsplit(pEntry->sLine, pText);
349349
if (pArr == NULL) return;
350350

@@ -382,7 +382,8 @@ static void XSearch_DisplayEntry(xfile_search_t *pSearch, xfile_entry_t *pEntry)
382382
/* Do not display additional info if verbose is not set */
383383
if (!((xsearch_args_t*)pSearch->pUserCtx)->bVerbose)
384384
{
385-
if (!pEntry->nLineNum) xlog("%s%s%s", sEntry, pArrow, sLinkPath);
385+
if (!pEntry->nLineNum && !xstrused(sLine)) xlog("%s%s%s", sEntry, pArrow, sLinkPath);
386+
else if (xstrused(pEntry->sLine) && xstrused(sLine)) xlog("%s: %s", sEntry, sLine);
386387
else xlog("%s:%s%d%s %s", sEntry, XSTR_FMT_BOLD, pEntry->nLineNum, XSTR_FMT_RESET, sLine);
387388
return;
388389
}
@@ -402,12 +403,18 @@ static void XSearch_DisplayEntry(xfile_search_t *pSearch, xfile_entry_t *pEntry)
402403
XBytesToUnit(sSize, sizeof(sSize), pEntry->nSize, XTRUE);
403404
xstrnlcpyf(sRound, sizeof(sRound), 7, XSTR_SPACE_CHAR, "%s", sSize);
404405

405-
if (pEntry->nLineNum)
406+
if (pEntry->nLineNum && xstrused(sLine))
406407
{
407408
xlog("%c%s %lu %s %s %s [%s] %s:%s%d%s %s", XFile_GetTypeChar(pEntry->eType),
408409
pEntry->sPerm, pEntry->nLinkCount, pUname, pGname, sRound, sTime,
409410
sEntry, XSTR_FMT_BOLD, pEntry->nLineNum, XSTR_FMT_RESET, sLine);
410411
}
412+
else if (xstrused(sLine))
413+
{
414+
xlog("%c%s %lu %s %s %s [%s] %s: %s", XFile_GetTypeChar(pEntry->eType),
415+
pEntry->sPerm, pEntry->nLinkCount, pUname, pGname, sRound, sTime,
416+
sEntry, sLine);
417+
}
411418
else
412419
{
413420
xlog("%c%s %lu %s %s %s [%s] %s%s%s", XFile_GetTypeChar(pEntry->eType),

0 commit comments

Comments
 (0)