]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-statuslog.c
discover/paths: Announce download completion
[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_entry {
37         struct status           *status;
38         struct list_item        list;
39 };
40
41 struct statuslog {
42         struct list             status;
43         int                     n_status;
44         bool                    truncated;
45 };
46
47 struct statuslog_screen {
48         struct text_screen text_scr;
49 };
50
51 struct statuslog *statuslog_init(struct cui *cui)
52 {
53         struct statuslog *sl;
54
55         sl = talloc(cui, struct statuslog);
56         sl->truncated = false;
57         sl->n_status = 0;
58         list_init(&sl->status);
59
60         return sl;
61 }
62
63 void statuslog_append_steal(struct cui *cui, struct statuslog *statuslog,
64                 struct status *status)
65 {
66         struct statuslog_entry *entry;
67
68         entry = talloc(statuslog, struct statuslog_entry);
69         entry->status = status;
70         talloc_steal(statuslog, status);
71
72         list_add_tail(&statuslog->status, &entry->list);
73
74         if (statuslog->n_status >= max_status_entry) {
75                 list_remove(&statuslog->status.head);
76                 statuslog->truncated = true;
77                 statuslog->n_status--;
78         }
79
80         statuslog->n_status++;
81
82         if (cui->statuslog_screen) {
83                 text_screen_append_line(&cui->statuslog_screen->text_scr,
84                                 "%s", status->message);
85                 text_screen_draw(&cui->statuslog_screen->text_scr);
86         }
87 }
88
89 struct statuslog_screen *statuslog_screen_init(struct cui *cui,
90                 void (*on_exit)(struct cui *))
91 {
92         struct statuslog_screen *screen;
93         struct statuslog_entry *entry;
94         const char *title;
95
96         screen = talloc_zero(cui, struct statuslog_screen);
97
98         title = _("Petitboot status log");
99
100         text_screen_init(&screen->text_scr, cui, title, on_exit);
101         list_for_each_entry(&cui->statuslog->status, entry, list) {
102                 text_screen_append_line(&screen->text_scr, "%s",
103                                 entry->status->message);
104         }
105         text_screen_draw(&screen->text_scr);
106
107         return screen;
108 }
109
110 struct nc_scr *statuslog_screen_scr(struct statuslog_screen *screen)
111 {
112         return text_screen_scr(&screen->text_scr);
113 }