Skip to content

Commit 43385c4

Browse files
committed
LOLWUT: wrap it into a proper command.
1 parent eac2a79 commit 43385c4

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ endif
144144

145145
REDIS_SERVER_NAME=redis-server
146146
REDIS_SENTINEL_NAME=redis-sentinel
147-
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o
147+
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o
148148
REDIS_CLI_NAME=redis-cli
149149
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o
150150
REDIS_BENCHMARK_NAME=redis-benchmark

src/lolwut.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,40 @@ sds lwRenderCanvas(lwCanvas *canvas) {
236236
return text;
237237
}
238238

239-
int main(void) {
240-
#if 0
241-
lwCanvas *c = lwCreateCanvas(80,80);
242-
for (int i = 0; i < 40; i++) {
243-
lwDrawPixel(c,i,i,1);
244-
}
245-
lwDrawLine(c,10,10,60,30,1);
246-
lwDrawSquare(c,40,40,40,0.5);
247-
lwDrawSquare(c,50,40,10,1);
248-
#endif
249-
lwCanvas *c = lwDrawSchotter(80,6,10);
250-
sds rendered = lwRenderCanvas(c);
251-
printf("%s\n", rendered);
239+
/* The LOLWUT command:
240+
*
241+
* LOLWUT [terminal columns] [squares-per-row] [squares-per-col]
242+
*
243+
* By default the command uses 80 columns, 6 squares per row, 10 squares
244+
* per column.
245+
*/
246+
void lolwutCommand(client *c) {
247+
long cols = 80;
248+
long squares_per_row = 6;
249+
long squares_per_col = 10;
250+
251+
/* Parse the optional arguments if any. */
252+
if (c->argc > 1 &&
253+
getLongFromObjectOrReply(c,c->argv[1],&cols,NULL) != C_OK)
254+
return;
255+
256+
if (c->argc > 2 &&
257+
getLongFromObjectOrReply(c,c->argv[2],&squares_per_row,NULL) != C_OK)
258+
return;
259+
260+
if (c->argc > 3 &&
261+
getLongFromObjectOrReply(c,c->argv[3],&squares_per_col,NULL) != C_OK)
262+
return;
263+
264+
if (cols < 1) cols = 1;
265+
if (squares_per_row < 1) squares_per_row = 1;
266+
if (squares_per_col < 1) squares_per_col = 1;
267+
268+
/* Generate some computer art and reply. */
269+
lwCanvas *canvas = lwDrawSchotter(cols,squares_per_row,squares_per_col);
270+
sds rendered = lwRenderCanvas(canvas);
271+
rendered = sdscat(rendered,
272+
"\nGeorg Ness - Schotter, Plotter on paper, 1968.\n");
273+
addReplyBulkSds(c,rendered);
274+
lwFreeCanvas(canvas);
252275
}

src/server.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ struct redisCommand redisCommandTable[] = {
322322
{"xtrim",xtrimCommand,-2,"wF",0,NULL,1,1,1,0,0},
323323
{"post",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0},
324324
{"host:",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0},
325-
{"latency",latencyCommand,-2,"aslt",0,NULL,0,0,0,0,0}
325+
{"latency",latencyCommand,-2,"aslt",0,NULL,0,0,0,0,0},
326+
{"lolwut",lolwutCommand,-1,"r",0,NULL,0,0,0,0,0}
326327
};
327328

328329
/*============================ Utility functions ============================ */

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,7 @@ void xclaimCommand(client *c);
21092109
void xinfoCommand(client *c);
21102110
void xdelCommand(client *c);
21112111
void xtrimCommand(client *c);
2112+
void lolwutCommand(client *c);
21122113

21132114
#if defined(__GNUC__)
21142115
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));

0 commit comments

Comments
 (0)