Colors everywhere
This commit is contained in:
23
hex.c
23
hex.c
@@ -1,11 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// TODO: Position printing
|
||||
// TODO: Stop appending garbage at file end
|
||||
// TODO: Skip repeating lines
|
||||
// TODO: Coloring printable ASCII characters in output
|
||||
// TODO: Stop scanning always for cmd & loc (for q and s commands)
|
||||
|
||||
#define BYTES 1024
|
||||
#define BYTES_PER_LINE 20
|
||||
@@ -14,7 +11,6 @@ void print_hex(unsigned char* buf, int byteno, int pos)
|
||||
{
|
||||
for (int i=0; i<byteno; i++)
|
||||
{
|
||||
// Print line number here
|
||||
if (i % BYTES_PER_LINE == 0)
|
||||
{
|
||||
//printf("%03d: ", lineno);
|
||||
@@ -23,18 +19,15 @@ void print_hex(unsigned char* buf, int byteno, int pos)
|
||||
printf(" ");
|
||||
for (int j=i-BYTES_PER_LINE; j<i; j++)
|
||||
{
|
||||
if (isprint(buf[j])) printf("%c", buf[j]);
|
||||
if (isprint(buf[j])) printf("\x1b[33m%c\x1b[0m", buf[j]);
|
||||
else printf(".");
|
||||
}
|
||||
}
|
||||
puts("");
|
||||
// Here, we could print positions in hex but I prefer using integers.
|
||||
//printf("%04X ", i);
|
||||
|
||||
if (pos == 0) printf("%06d: ", i);
|
||||
else printf("%06d: ", pos);
|
||||
// Here, we could print positions in hex but I prefer using integers. printf("%04X ", i);
|
||||
if (pos == 0) printf("\x1b[1;34m%06d:\x1b[0m ", i);
|
||||
else printf("\x1b[34m%06d:\x1b[0m ", pos);
|
||||
}
|
||||
|
||||
printf("%.2X ", buf[i]);
|
||||
}
|
||||
|
||||
@@ -45,17 +38,15 @@ void print_hex(unsigned char* buf, int byteno, int pos)
|
||||
for (int i=0; i<padding; i++) printf(" ");
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
int start = byteno - (byteno % BYTES_PER_LINE);
|
||||
for (int j = start; j < byteno; j++) {
|
||||
if (isprint(buf[j])) {
|
||||
printf("%c", buf[j]);
|
||||
printf("\x1b[33m%c\x1b[0m", buf[j]);
|
||||
}
|
||||
else {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
|
||||
puts("");
|
||||
}
|
||||
|
||||
@@ -77,7 +68,9 @@ int main(int argc, char** argv)
|
||||
char cmd;
|
||||
int loc;
|
||||
|
||||
scanf("%c%d", &cmd, &loc);
|
||||
scanf(" %c", &cmd);
|
||||
if (cmd == 'p' || cmd == 'P' || cmd == 'e' || cmd == 'E') scanf("%d", &loc);
|
||||
|
||||
switch(cmd)
|
||||
{
|
||||
case 'p':
|
||||
|
||||
Reference in New Issue
Block a user