#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <slang.h>
#include <stdlib.h>
#include <newt.h>
#include <sys/ttydefaults.h>
#include "cache.h"
#include "hist.h"
#include "pstack.h"
#include "session.h"
#include "sort.h"
#include "symbol.h"
struct ui_progress {
newtComponent form, scale;
};
struct ui_progress *ui_progress__new(const char *title, u64 total)
{
struct ui_progress *self = malloc(sizeof(*self));
if (self != NULL) {
int cols;
newtGetScreenSize(&cols, NULL);
cols -= 4;
newtCenteredWindow(cols, 1, title);
self->form = newtForm(NULL, NULL, 0);
if (self->form == NULL)
goto out_free_self;
self->scale = newtScale(0, 0, cols, total);
if (self->scale == NULL)
goto out_free_form;
newtFormAddComponent(self->form, self->scale);
newtRefresh();
}
return self;
out_free_form:
newtFormDestroy(self->form);
out_free_self:
free(self);
return NULL;
}
void ui_progress__update(struct ui_progress *self, u64 curr)
{
newtScaleSet(self->scale, curr);
newtRefresh();
}
void ui_progress__delete(struct ui_progress *self)
{
newtFormDestroy(self->form);
newtPopWindow();
free(self);
}
static void ui_helpline__pop(void)
{
newtPopHelpLine();
}
static void ui_helpline__push(const char *msg)
{
newtPushHelpLine(msg);
}
static void ui_helpline__vpush(const char *fmt, va_list ap)
{
char *s;
if (vasprintf(&s, fmt, ap) < 0)
vfprintf(stderr, fmt, ap);
else {
ui_helpline__push(s);
free(s);
}
}
static void ui_helpline__fpush(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
ui_helpline__vpush(fmt, ap);
va_end(ap);
}
static void ui_helpline__puts(const char *msg)
{
ui_helpline__pop();
ui_helpline__push(msg);
}
static char browser__last_msg[1024];
int browser__show_help(const char *format, va_list ap)
{
int ret;
static int backlog;
ret = vsnprintf(browser__last_msg + backlog,
sizeof(browser__last_msg) - backlog, format, ap);
backlog += ret;
if (browser__last_msg[backlog - 1] == '\n') {
ui_helpline__puts(browser__last_msg);
newtRefresh();
backlog = 0;
}
return ret;
}
static void newt_form__set_exit_keys(newtComponent self)
{
newtFormAddHotKey(self, NEWT_KEY_LEFT);
newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
newtFormAddHotKey(self, 'Q');
newtFormAddHotKey(self, 'q');
newtFormAddHotKey(self, CTRL('c'));
}
static newtComponent newt_form__new(void)
{
newtComponent self = newtForm(NULL, NULL, 0);
if (self)
newt_form__set_exit_keys(self);
return self;
}
static int popup_menu(int argc, char * const argv[])
{
struct newtExitStruct es;
int i, rc = -1, max_len = 5;
newtComponent listbox, form = newt_form__new();
if (form == NULL)
return -1;
listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
if (listbox == NULL)
goto out_destroy_form;
newtFormAddComponent(form, listbox);
for (i = 0; i < argc; ++i) {
int len = strlen(argv[i]);
if (len >