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