]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-statuslog.c
ui/ncurses: Remove help shortcut from nc-subset
[petitboot] / ui / ncurses / nc-statuslog.c
1 /*
2  *  Copyright (C) 2016 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <string.h>
23
24 #include <talloc/talloc.h>
25 #include <types/types.h>
26 #include <log/log.h>
27 #include <util/util.h>
28 #include <i18n/i18n.h>
29
30 #include "nc-cui.h"
31 #include "nc-textscreen.h"
32 #include "nc-statuslog.h"
33
34 static const int max_status_entry = 10000;
35
36 struct statuslog {
37         struct list             status;
38         int                     n_status;
39         bool                    truncated;
40 };
41
42 struct statuslog_screen {
43         struct text_screen text_scr;
44 };
45
46 struct statuslog *statuslog_init(struct cui *cui)
47 {
48         struct statuslog *sl;
49
50         sl = talloc(cui, struct statuslog);
51         sl->truncated = false;
52         sl->n_status = 0;
53         list_init(&sl->status);
54
55         return sl;
56 }
57
58 void statuslog_append_steal(struct cui *cui, struct statuslog *statuslog,
59                 struct status *status)
60 {
61         struct statuslog_entry *entry;
62
63         entry = talloc(statuslog, struct statuslog_entry);
64         entry->status = status;
65         talloc_steal(statuslog, status);
66
67         list_add_tail(&statuslog->status, &entry->list);
68
69         if (statuslog->n_status >= max_status_entry) {
70                 list_remove(&statuslog->status.head);
71                 statuslog->truncated = true;
72                 statuslog->n_status--;
73         }
74
75         statuslog->n_status++;
76
77         if (cui->statuslog_screen) {
78                 text_screen_append_line(&cui->statuslog_screen->text_scr,
79                                 "%s", status->message);
80                 text_screen_draw(&cui->statuslog_screen->text_scr);
81         }
82 }
83
84 struct statuslog_screen *statuslog_screen_init(struct cui *cui,
85                 void (*on_exit)(struct cui *))
86 {
87         struct statuslog_screen *screen;
88         struct statuslog_entry *entry;
89         const char *title;
90
91         screen = talloc_zero(cui, struct statuslog_screen);
92
93         title = _("Petitboot status log");
94
95         text_screen_init(&screen->text_scr, cui, title, on_exit);
96         list_for_each_entry(&cui->statuslog->status, entry, list) {
97                 text_screen_append_line(&screen->text_scr, "%s",
98                                 entry->status->message);
99         }
100         text_screen_draw(&screen->text_scr);
101
102         return screen;
103 }
104
105 struct nc_scr *statuslog_screen_scr(struct statuslog_screen *screen)
106 {
107         return text_screen_scr(&screen->text_scr);
108 }