xref: /openbsd-src/usr.bin/tmux/cmd-show-prompt-history.c (revision a51dead1c4d3ed85038c588391e643b117324e5a)
1*a51dead1Snicm /* $OpenBSD: cmd-show-prompt-history.c,v 1.2 2021/08/21 10:22:39 nicm Exp $ */
2bc5a8fc2Snicm 
3bc5a8fc2Snicm /*
4bc5a8fc2Snicm  * Copyright (c) 2021 Anindya Mukherjee <anindya49@hotmail.com>
5bc5a8fc2Snicm  *
6bc5a8fc2Snicm  * Permission to use, copy, modify, and distribute this software for any
7bc5a8fc2Snicm  * purpose with or without fee is hereby granted, provided that the above
8bc5a8fc2Snicm  * copyright notice and this permission notice appear in all copies.
9bc5a8fc2Snicm  *
10bc5a8fc2Snicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11bc5a8fc2Snicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12bc5a8fc2Snicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13bc5a8fc2Snicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14bc5a8fc2Snicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15bc5a8fc2Snicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16bc5a8fc2Snicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17bc5a8fc2Snicm  */
18bc5a8fc2Snicm 
19bc5a8fc2Snicm #include "tmux.h"
20bc5a8fc2Snicm 
21bc5a8fc2Snicm #include <stdlib.h>
22bc5a8fc2Snicm 
23bc5a8fc2Snicm /*
24bc5a8fc2Snicm  * Show or clear prompt history.
25bc5a8fc2Snicm  */
26bc5a8fc2Snicm 
27bc5a8fc2Snicm static enum cmd_retval	cmd_show_prompt_history_exec(struct cmd *,
28bc5a8fc2Snicm 			    struct cmdq_item *);
29bc5a8fc2Snicm 
30bc5a8fc2Snicm const struct cmd_entry cmd_show_prompt_history_entry = {
31bc5a8fc2Snicm 	.name = "show-prompt-history",
32bc5a8fc2Snicm 	.alias = "showphist",
33bc5a8fc2Snicm 
34*a51dead1Snicm 	.args = { "T:", 0, 0, NULL },
35bc5a8fc2Snicm 	.usage = "[-T type]",
36bc5a8fc2Snicm 
37bc5a8fc2Snicm 	.flags = CMD_AFTERHOOK,
38bc5a8fc2Snicm 	.exec = cmd_show_prompt_history_exec
39bc5a8fc2Snicm };
40bc5a8fc2Snicm 
41bc5a8fc2Snicm const struct cmd_entry cmd_clear_prompt_history_entry = {
42bc5a8fc2Snicm 	.name = "clear-prompt-history",
43bc5a8fc2Snicm 	.alias = "clearphist",
44bc5a8fc2Snicm 
45*a51dead1Snicm 	.args = { "T:", 0, 0, NULL },
46bc5a8fc2Snicm 	.usage = "[-T type]",
47bc5a8fc2Snicm 
48bc5a8fc2Snicm 	.flags = CMD_AFTERHOOK,
49bc5a8fc2Snicm 	.exec = cmd_show_prompt_history_exec
50bc5a8fc2Snicm };
51bc5a8fc2Snicm 
52bc5a8fc2Snicm static enum cmd_retval
cmd_show_prompt_history_exec(struct cmd * self,struct cmdq_item * item)53bc5a8fc2Snicm cmd_show_prompt_history_exec(struct cmd *self, struct cmdq_item *item)
54bc5a8fc2Snicm {
55bc5a8fc2Snicm 	struct args		*args = cmd_get_args(self);
56bc5a8fc2Snicm 	const char		*typestr = args_get(args, 'T');
57bc5a8fc2Snicm 	enum prompt_type	 type;
58bc5a8fc2Snicm 	u_int			 tidx, hidx;
59bc5a8fc2Snicm 
60bc5a8fc2Snicm 	if (cmd_get_entry(self) == &cmd_clear_prompt_history_entry) {
61bc5a8fc2Snicm 		if (typestr == NULL) {
62bc5a8fc2Snicm 			for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
63bc5a8fc2Snicm 				free(status_prompt_hlist[tidx]);
64bc5a8fc2Snicm 				status_prompt_hlist[tidx] = NULL;
65bc5a8fc2Snicm 				status_prompt_hsize[tidx] = 0;
66bc5a8fc2Snicm 			}
67bc5a8fc2Snicm 		} else {
68bc5a8fc2Snicm 			type = status_prompt_type(typestr);
69bc5a8fc2Snicm 			if (type == PROMPT_TYPE_INVALID) {
70bc5a8fc2Snicm 				cmdq_error(item, "invalid type: %s", typestr);
71bc5a8fc2Snicm 				return (CMD_RETURN_ERROR);
72bc5a8fc2Snicm 			}
73bc5a8fc2Snicm 			free(status_prompt_hlist[type]);
74bc5a8fc2Snicm 			status_prompt_hlist[type] = NULL;
75bc5a8fc2Snicm 			status_prompt_hsize[type] = 0;
76bc5a8fc2Snicm 		}
77bc5a8fc2Snicm 
78bc5a8fc2Snicm 		return (CMD_RETURN_NORMAL);
79bc5a8fc2Snicm 	}
80bc5a8fc2Snicm 
81bc5a8fc2Snicm 	if (typestr == NULL) {
82bc5a8fc2Snicm 		for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
83bc5a8fc2Snicm 			cmdq_print(item, "History for %s:\n",
84bc5a8fc2Snicm 			    status_prompt_type_string(tidx));
85bc5a8fc2Snicm 			for (hidx = 0; hidx < status_prompt_hsize[tidx];
86bc5a8fc2Snicm 			    hidx++) {
87bc5a8fc2Snicm 				cmdq_print(item, "%d: %s", hidx + 1,
88bc5a8fc2Snicm 				    status_prompt_hlist[tidx][hidx]);
89bc5a8fc2Snicm 			}
90bc5a8fc2Snicm 			cmdq_print(item, "%s", "");
91bc5a8fc2Snicm 		}
92bc5a8fc2Snicm 	} else {
93bc5a8fc2Snicm 		type = status_prompt_type(typestr);
94bc5a8fc2Snicm 		if (type == PROMPT_TYPE_INVALID) {
95bc5a8fc2Snicm 			cmdq_error(item, "invalid type: %s", typestr);
96bc5a8fc2Snicm 			return (CMD_RETURN_ERROR);
97bc5a8fc2Snicm 		}
98bc5a8fc2Snicm 		cmdq_print(item, "History for %s:\n",
99bc5a8fc2Snicm 		    status_prompt_type_string(type));
100bc5a8fc2Snicm 		for (hidx = 0; hidx < status_prompt_hsize[type]; hidx++) {
101bc5a8fc2Snicm 			cmdq_print(item, "%d: %s", hidx + 1,
102bc5a8fc2Snicm 			    status_prompt_hlist[type][hidx]);
103bc5a8fc2Snicm 		}
104bc5a8fc2Snicm 		cmdq_print(item, "%s", "");
105bc5a8fc2Snicm 	}
106bc5a8fc2Snicm 
107bc5a8fc2Snicm 	return (CMD_RETURN_NORMAL);
108bc5a8fc2Snicm }
109