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