Compare commits
No commits in common. "609ddfab22f7cae83ba467878c6ff516a8830ad9" and "e957bba771ea200da16f4d3a02aaec7784c65168" have entirely different histories.
609ddfab22
...
e957bba771
288
line-editor.c
288
line-editor.c
@ -1,288 +0,0 @@
|
|||||||
/*
|
|
||||||
* line-editor.c: a line editor component for the TUI part of liberty
|
|
||||||
*
|
|
||||||
* Copyright (c) 2017 - 2018, Přemysl Janouch <p@janouch.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
||||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
||||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
// This is here just for IDE code model reasons
|
|
||||||
#ifndef HAVE_LIBERTY
|
|
||||||
#include "liberty/liberty.c"
|
|
||||||
#include "liberty/liberty-tui.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
|
||||||
row_buffer_append_c (struct row_buffer *self, ucs4_t c, chtype attrs)
|
|
||||||
{
|
|
||||||
struct row_char current = { .attrs = attrs, .c = c };
|
|
||||||
struct row_char invalid = { .attrs = attrs, .c = '?', .width = 1 };
|
|
||||||
|
|
||||||
current.width = uc_width (current.c, locale_charset ());
|
|
||||||
if (current.width < 0 || !app_is_character_in_locale (current.c))
|
|
||||||
current = invalid;
|
|
||||||
|
|
||||||
ARRAY_RESERVE (self->chars, 1);
|
|
||||||
self->chars[self->chars_len++] = current;
|
|
||||||
self->total_width += current.width;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Line editor -------------------------------------------------------------
|
|
||||||
|
|
||||||
enum line_editor_action
|
|
||||||
{
|
|
||||||
LINE_EDITOR_B_CHAR, ///< Go back a character
|
|
||||||
LINE_EDITOR_F_CHAR, ///< Go forward a character
|
|
||||||
LINE_EDITOR_B_WORD, ///< Go back a word
|
|
||||||
LINE_EDITOR_F_WORD, ///< Go forward a word
|
|
||||||
LINE_EDITOR_HOME, ///< Go to start of line
|
|
||||||
LINE_EDITOR_END, ///< Go to end of line
|
|
||||||
|
|
||||||
LINE_EDITOR_B_DELETE, ///< Delete last character
|
|
||||||
LINE_EDITOR_F_DELETE, ///< Delete next character
|
|
||||||
LINE_EDITOR_B_KILL_WORD, ///< Delete last word
|
|
||||||
LINE_EDITOR_B_KILL_LINE, ///< Delete everything up to BOL
|
|
||||||
LINE_EDITOR_F_KILL_LINE, ///< Delete everything up to EOL
|
|
||||||
};
|
|
||||||
|
|
||||||
struct line_editor
|
|
||||||
{
|
|
||||||
int point; ///< Caret index into line data
|
|
||||||
ucs4_t *line; ///< Line data, 0-terminated
|
|
||||||
int *w; ///< Codepoint widths, 0-terminated
|
|
||||||
size_t len; ///< Editor length
|
|
||||||
size_t alloc; ///< Editor allocated
|
|
||||||
char prompt; ///< Prompt character
|
|
||||||
|
|
||||||
void (*on_changed) (void); ///< Callback on text change
|
|
||||||
void (*on_end) (bool); ///< Callback on abort
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
|
||||||
line_editor_free (struct line_editor *self)
|
|
||||||
{
|
|
||||||
free (self->line);
|
|
||||||
free (self->w);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Notify whomever invoked the editor that it's been either confirmed or
|
|
||||||
/// cancelled and clean up editor state
|
|
||||||
static void
|
|
||||||
line_editor_abort (struct line_editor *self, bool status)
|
|
||||||
{
|
|
||||||
self->on_end (status);
|
|
||||||
self->on_changed = NULL;
|
|
||||||
|
|
||||||
free (self->line);
|
|
||||||
self->line = NULL;
|
|
||||||
free (self->w);
|
|
||||||
self->w = NULL;
|
|
||||||
self->alloc = 0;
|
|
||||||
self->len = 0;
|
|
||||||
self->point = 0;
|
|
||||||
self->prompt = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Start the line editor; remember to fill in "change" and "end" callbacks
|
|
||||||
static void
|
|
||||||
line_editor_start (struct line_editor *self, char prompt)
|
|
||||||
{
|
|
||||||
self->alloc = 16;
|
|
||||||
self->line = xcalloc (sizeof *self->line, self->alloc);
|
|
||||||
self->w = xcalloc (sizeof *self->w, self->alloc);
|
|
||||||
self->len = 0;
|
|
||||||
self->point = 0;
|
|
||||||
self->prompt = prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
line_editor_changed (struct line_editor *self)
|
|
||||||
{
|
|
||||||
self->line[self->len] = 0;
|
|
||||||
self->w[self->len] = 0;
|
|
||||||
|
|
||||||
if (self->on_changed)
|
|
||||||
self->on_changed ();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
line_editor_move (struct line_editor *self, int to, int from, int len)
|
|
||||||
{
|
|
||||||
memmove (self->line + to, self->line + from,
|
|
||||||
sizeof *self->line * len);
|
|
||||||
memmove (self->w + to, self->w + from,
|
|
||||||
sizeof *self->w * len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
line_editor_insert (struct line_editor *self, ucs4_t codepoint)
|
|
||||||
{
|
|
||||||
while (self->alloc - self->len < 2 /* inserted + sentinel */)
|
|
||||||
{
|
|
||||||
self->alloc <<= 1;
|
|
||||||
self->line = xreallocarray
|
|
||||||
(self->line, sizeof *self->line, self->alloc);
|
|
||||||
self->w = xreallocarray
|
|
||||||
(self->w, sizeof *self->w, self->alloc);
|
|
||||||
}
|
|
||||||
|
|
||||||
line_editor_move (self, self->point + 1, self->point,
|
|
||||||
self->len - self->point);
|
|
||||||
self->line[self->point] = codepoint;
|
|
||||||
self->w[self->point] = app_is_character_in_locale (codepoint)
|
|
||||||
? uc_width (codepoint, locale_charset ())
|
|
||||||
: 1 /* the replacement question mark */;
|
|
||||||
|
|
||||||
self->point++;
|
|
||||||
self->len++;
|
|
||||||
line_editor_changed (self);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
line_editor_action (struct line_editor *self, enum line_editor_action action)
|
|
||||||
{
|
|
||||||
switch (action)
|
|
||||||
{
|
|
||||||
default:
|
|
||||||
return soft_assert (!"unknown line editor action");
|
|
||||||
|
|
||||||
case LINE_EDITOR_B_CHAR:
|
|
||||||
if (self->point < 1)
|
|
||||||
return false;
|
|
||||||
do self->point--;
|
|
||||||
while (self->point > 0
|
|
||||||
&& !self->w[self->point]);
|
|
||||||
return true;
|
|
||||||
case LINE_EDITOR_F_CHAR:
|
|
||||||
if (self->point + 1 > (int) self->len)
|
|
||||||
return false;
|
|
||||||
do self->point++;
|
|
||||||
while (self->point < (int) self->len
|
|
||||||
&& !self->w[self->point]);
|
|
||||||
return true;
|
|
||||||
case LINE_EDITOR_B_WORD:
|
|
||||||
{
|
|
||||||
if (self->point < 1)
|
|
||||||
return false;
|
|
||||||
int i = self->point;
|
|
||||||
while (i && self->line[--i] == ' ');
|
|
||||||
while (i-- && self->line[i] != ' ');
|
|
||||||
self->point = ++i;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case LINE_EDITOR_F_WORD:
|
|
||||||
{
|
|
||||||
if (self->point + 1 > (int) self->len)
|
|
||||||
return false;
|
|
||||||
int i = self->point;
|
|
||||||
while (i < (int) self->len && self->line[i] != ' ') i++;
|
|
||||||
while (i < (int) self->len && self->line[i] == ' ') i++;
|
|
||||||
self->point = i;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case LINE_EDITOR_HOME:
|
|
||||||
self->point = 0;
|
|
||||||
return true;
|
|
||||||
case LINE_EDITOR_END:
|
|
||||||
self->point = self->len;
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case LINE_EDITOR_B_DELETE:
|
|
||||||
{
|
|
||||||
if (self->point < 1)
|
|
||||||
return false;
|
|
||||||
int len = 1;
|
|
||||||
while (self->point - len > 0
|
|
||||||
&& !self->w[self->point - len])
|
|
||||||
len++;
|
|
||||||
line_editor_move (self, self->point - len, self->point,
|
|
||||||
self->len - self->point);
|
|
||||||
self->len -= len;
|
|
||||||
self->point -= len;
|
|
||||||
line_editor_changed (self);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case LINE_EDITOR_F_DELETE:
|
|
||||||
{
|
|
||||||
if (self->point + 1 > (int) self->len)
|
|
||||||
return false;
|
|
||||||
int len = 1;
|
|
||||||
while (self->point + len < (int) self->len
|
|
||||||
&& !self->w[self->point + len])
|
|
||||||
len++;
|
|
||||||
self->len -= len;
|
|
||||||
line_editor_move (self, self->point, self->point + len,
|
|
||||||
self->len - self->point);
|
|
||||||
line_editor_changed (self);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case LINE_EDITOR_B_KILL_WORD:
|
|
||||||
{
|
|
||||||
if (self->point < 1)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int i = self->point;
|
|
||||||
while (i && self->line[--i] == ' ');
|
|
||||||
while (i-- && self->line[i] != ' ');
|
|
||||||
i++;
|
|
||||||
|
|
||||||
line_editor_move (self, i, self->point, (self->len - self->point));
|
|
||||||
self->len -= self->point - i;
|
|
||||||
self->point = i;
|
|
||||||
line_editor_changed (self);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case LINE_EDITOR_B_KILL_LINE:
|
|
||||||
self->len -= self->point;
|
|
||||||
line_editor_move (self, 0, self->point, self->len);
|
|
||||||
self->point = 0;
|
|
||||||
line_editor_changed (self);
|
|
||||||
return true;
|
|
||||||
case LINE_EDITOR_F_KILL_LINE:
|
|
||||||
self->len = self->point;
|
|
||||||
line_editor_changed (self);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
line_editor_write (const struct line_editor *self, struct row_buffer *row,
|
|
||||||
int width, chtype attrs)
|
|
||||||
{
|
|
||||||
if (self->prompt)
|
|
||||||
{
|
|
||||||
hard_assert (self->prompt < 127);
|
|
||||||
row_buffer_append_c (row, self->prompt, attrs);
|
|
||||||
width--;
|
|
||||||
}
|
|
||||||
|
|
||||||
int following = 0;
|
|
||||||
for (size_t i = self->point; i < self->len; i++)
|
|
||||||
following += self->w[i];
|
|
||||||
|
|
||||||
int preceding = 0;
|
|
||||||
size_t start = self->point;
|
|
||||||
while (start && preceding < width / 2)
|
|
||||||
preceding += self->w[--start];
|
|
||||||
|
|
||||||
// There can be one extra space at the end of the line but this way we
|
|
||||||
// don't need to care about non-spacing marks following full-width chars
|
|
||||||
while (start && width - preceding - following > 2 /* widest char */)
|
|
||||||
preceding += self->w[--start];
|
|
||||||
|
|
||||||
// XXX: we should also show < > indicators for overflow but it'd probably
|
|
||||||
// considerably complicate this algorithm
|
|
||||||
for (; start < self->len; start++)
|
|
||||||
row_buffer_append_c (row, self->line[start], attrs);
|
|
||||||
return !!self->prompt + preceding;
|
|
||||||
}
|
|
485
nncmpp.c
485
nncmpp.c
@ -20,31 +20,27 @@
|
|||||||
|
|
||||||
// We "need" to have an enum for attributes before including liberty.
|
// We "need" to have an enum for attributes before including liberty.
|
||||||
// Avoiding colours in the defaults here in order to support dumb terminals.
|
// Avoiding colours in the defaults here in order to support dumb terminals.
|
||||||
#define ATTRIBUTE_TABLE(XX) \
|
#define ATTRIBUTE_TABLE(XX) \
|
||||||
XX( NORMAL, "normal", -1, -1, 0 ) \
|
XX( NORMAL, "normal", -1, -1, 0 ) \
|
||||||
XX( HIGHLIGHT, "highlight", -1, -1, A_BOLD ) \
|
XX( HIGHLIGHT, "highlight", -1, -1, A_BOLD ) \
|
||||||
/* Gauge */ \
|
/* Gauge */ \
|
||||||
XX( ELAPSED, "elapsed", -1, -1, A_REVERSE ) \
|
XX( ELAPSED, "elapsed", -1, -1, A_REVERSE ) \
|
||||||
XX( REMAINS, "remains", -1, -1, A_UNDERLINE ) \
|
XX( REMAINS, "remains", -1, -1, A_UNDERLINE ) \
|
||||||
/* Tab bar */ \
|
/* Tab bar */ \
|
||||||
XX( TAB_BAR, "tab_bar", -1, -1, A_REVERSE ) \
|
XX( TAB_BAR, "tab_bar", -1, -1, A_REVERSE ) \
|
||||||
XX( TAB_ACTIVE, "tab_active", -1, -1, A_UNDERLINE ) \
|
XX( TAB_ACTIVE, "tab_active", -1, -1, A_UNDERLINE ) \
|
||||||
/* Listview */ \
|
/* Listview */ \
|
||||||
XX( HEADER, "header", -1, -1, A_UNDERLINE ) \
|
XX( HEADER, "header", -1, -1, A_UNDERLINE ) \
|
||||||
XX( EVEN, "even", -1, -1, 0 ) \
|
XX( EVEN, "even", -1, -1, 0 ) \
|
||||||
XX( ODD, "odd", -1, -1, 0 ) \
|
XX( ODD, "odd", -1, -1, 0 ) \
|
||||||
XX( DIRECTORY, "directory", -1, -1, 0 ) \
|
XX( DIRECTORY, "directory", -1, -1, 0 ) \
|
||||||
XX( SELECTION, "selection", -1, -1, A_REVERSE ) \
|
XX( SELECTION, "selection", -1, -1, A_REVERSE ) \
|
||||||
/* Cyan is good with both black and white.
|
XX( SCROLLBAR, "scrollbar", -1, -1, 0 ) \
|
||||||
* Can't use A_REVERSE because bold'd be bright.
|
/* These are for debugging only */ \
|
||||||
* Unfortunately ran out of B&W attributes. */ \
|
XX( WARNING, "warning", 3, -1, 0 ) \
|
||||||
XX( MULTISELECT, "multiselect",-1, 6, 0 ) \
|
XX( ERROR, "error", 1, -1, 0 ) \
|
||||||
XX( SCROLLBAR, "scrollbar", -1, -1, 0 ) \
|
XX( INCOMING, "incoming", 2, -1, 0 ) \
|
||||||
/* These are for debugging only */ \
|
XX( OUTGOING, "outgoing", 4, -1, 0 )
|
||||||
XX( WARNING, "warning", 3, -1, 0 ) \
|
|
||||||
XX( ERROR, "error", 1, -1, 0 ) \
|
|
||||||
XX( INCOMING, "incoming", 2, -1, 0 ) \
|
|
||||||
XX( OUTGOING, "outgoing", 4, -1, 0 )
|
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
@ -70,9 +66,6 @@ enum
|
|||||||
#include "liberty/liberty.c"
|
#include "liberty/liberty.c"
|
||||||
#include "liberty/liberty-tui.c"
|
#include "liberty/liberty-tui.c"
|
||||||
|
|
||||||
#define HAVE_LIBERTY
|
|
||||||
#include "line-editor.c"
|
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
@ -540,6 +533,7 @@ item_list_resize (struct item_list *self, size_t len)
|
|||||||
// For simplicity, the listview can only work with items that are one row high.
|
// For simplicity, the listview can only work with items that are one row high.
|
||||||
|
|
||||||
struct tab;
|
struct tab;
|
||||||
|
struct row_buffer;
|
||||||
enum action;
|
enum action;
|
||||||
|
|
||||||
/// Try to handle an action in the tab
|
/// Try to handle an action in the tab
|
||||||
@ -572,7 +566,6 @@ struct tab
|
|||||||
|
|
||||||
int item_top; ///< Index of the topmost item
|
int item_top; ///< Index of the topmost item
|
||||||
int item_selected; ///< Index of the selected item
|
int item_selected; ///< Index of the selected item
|
||||||
int item_mark; ///< Multiselect second point index
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
@ -637,9 +630,19 @@ static struct app_context
|
|||||||
int gauge_offset; ///< Offset to the gauge or -1
|
int gauge_offset; ///< Offset to the gauge or -1
|
||||||
int gauge_width; ///< Width of the gauge, if present
|
int gauge_width; ///< Width of the gauge, if present
|
||||||
|
|
||||||
struct line_editor editor; ///< Line editor
|
|
||||||
struct poller_idle refresh_event; ///< Refresh the screen
|
struct poller_idle refresh_event; ///< Refresh the screen
|
||||||
|
|
||||||
|
// Line editor:
|
||||||
|
|
||||||
|
int editor_point; ///< Caret index into line data
|
||||||
|
ucs4_t *editor_line; ///< Line data, 0-terminated
|
||||||
|
int *editor_w; ///< Codepoint widths, 0-terminated
|
||||||
|
size_t editor_len; ///< Editor length
|
||||||
|
size_t editor_alloc; ///< Editor allocated
|
||||||
|
char editor_prompt; ///< Prompt character
|
||||||
|
void (*on_editor_changed) (void); ///< Callback on text change
|
||||||
|
void (*on_editor_end) (bool); ///< Callback on abort
|
||||||
|
|
||||||
// Terminal:
|
// Terminal:
|
||||||
|
|
||||||
termo_t *tk; ///< termo handle
|
termo_t *tk; ///< termo handle
|
||||||
@ -667,7 +670,6 @@ tab_init (struct tab *self, const char *name)
|
|||||||
// and we'd need to filter it first to replace invalid chars with '?'
|
// and we'd need to filter it first to replace invalid chars with '?'
|
||||||
self->name_width = u8_strwidth ((uint8_t *) self->name, locale_charset ());
|
self->name_width = u8_strwidth ((uint8_t *) self->name, locale_charset ());
|
||||||
self->item_selected = 0;
|
self->item_selected = 0;
|
||||||
self->item_mark = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -676,17 +678,6 @@ tab_free (struct tab *self)
|
|||||||
free (self->name);
|
free (self->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct tab_range { int from, upto; }
|
|
||||||
tab_selection_range (struct tab *self)
|
|
||||||
{
|
|
||||||
if (self->item_selected < 0 || !self->item_count)
|
|
||||||
return (struct tab_range) { -1, -1 };
|
|
||||||
if (self->item_mark < 0)
|
|
||||||
return (struct tab_range) { self->item_selected, self->item_selected };
|
|
||||||
return (struct tab_range) { MIN (self->item_selected, self->item_mark),
|
|
||||||
MAX (self->item_selected, self->item_mark) };
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Configuration -----------------------------------------------------------
|
// --- Configuration -----------------------------------------------------------
|
||||||
|
|
||||||
static struct config_schema g_config_settings[] =
|
static struct config_schema g_config_settings[] =
|
||||||
@ -895,7 +886,8 @@ app_free_context (void)
|
|||||||
strv_free (&g.streams);
|
strv_free (&g.streams);
|
||||||
item_list_free (&g.playlist);
|
item_list_free (&g.playlist);
|
||||||
|
|
||||||
line_editor_free (&g.editor);
|
free (g.editor_line);
|
||||||
|
free (g.editor_w);
|
||||||
|
|
||||||
config_free (&g.config);
|
config_free (&g.config);
|
||||||
poller_free (&g.poller);
|
poller_free (&g.poller);
|
||||||
@ -1279,16 +1271,8 @@ app_draw_view (void)
|
|||||||
{
|
{
|
||||||
int item_index = tab->item_top + row;
|
int item_index = tab->item_top + row;
|
||||||
int row_attrs = (item_index & 1) ? APP_ATTR (ODD) : APP_ATTR (EVEN);
|
int row_attrs = (item_index & 1) ? APP_ATTR (ODD) : APP_ATTR (EVEN);
|
||||||
|
|
||||||
bool override_colors = true;
|
|
||||||
if (item_index == tab->item_selected)
|
if (item_index == tab->item_selected)
|
||||||
row_attrs = APP_ATTR (SELECTION);
|
row_attrs = APP_ATTR (SELECTION);
|
||||||
else if (tab->item_mark > -1 &&
|
|
||||||
((item_index >= tab->item_mark && item_index <= tab->item_selected)
|
|
||||||
|| (item_index >= tab->item_selected && item_index <= tab->item_mark)))
|
|
||||||
row_attrs = APP_ATTR (MULTISELECT);
|
|
||||||
else
|
|
||||||
override_colors = false;
|
|
||||||
|
|
||||||
struct row_buffer buf = row_buffer_make ();
|
struct row_buffer buf = row_buffer_make ();
|
||||||
tab->on_item_draw (item_index, &buf, view_width);
|
tab->on_item_draw (item_index, &buf, view_width);
|
||||||
@ -1298,7 +1282,7 @@ app_draw_view (void)
|
|||||||
for (size_t i = 0; i < buf.chars_len; i++)
|
for (size_t i = 0; i < buf.chars_len; i++)
|
||||||
{
|
{
|
||||||
chtype *attrs = &buf.chars[i].attrs;
|
chtype *attrs = &buf.chars[i].attrs;
|
||||||
if (override_colors)
|
if (item_index == tab->item_selected)
|
||||||
*attrs = (*attrs & ~(A_COLOR | A_REVERSE)) | row_attrs;
|
*attrs = (*attrs & ~(A_COLOR | A_REVERSE)) | row_attrs;
|
||||||
else if ((*attrs & A_COLOR) && (row_attrs & A_COLOR))
|
else if ((*attrs & A_COLOR) && (row_attrs & A_COLOR))
|
||||||
*attrs |= (row_attrs & ~A_COLOR);
|
*attrs |= (row_attrs & ~A_COLOR);
|
||||||
@ -1347,15 +1331,7 @@ static void
|
|||||||
app_write_mpd_status (struct row_buffer *buf)
|
app_write_mpd_status (struct row_buffer *buf)
|
||||||
{
|
{
|
||||||
struct str_map *map = &g.playback_info;
|
struct str_map *map = &g.playback_info;
|
||||||
if (g.active_tab->item_mark > -1)
|
if (str_map_find (map, "updating_db"))
|
||||||
{
|
|
||||||
struct tab_range r = tab_selection_range (g.active_tab);
|
|
||||||
char *msg = xstrdup_printf (r.from == r.upto
|
|
||||||
? "Selected %d item" : "Selected %d items", r.upto - r.from + 1);
|
|
||||||
row_buffer_append (buf, msg, APP_ATTR (HIGHLIGHT));
|
|
||||||
free (msg);
|
|
||||||
}
|
|
||||||
else if (str_map_find (map, "updating_db"))
|
|
||||||
row_buffer_append (buf, "Updating database...", APP_ATTR (NORMAL));
|
row_buffer_append (buf, "Updating database...", APP_ATTR (NORMAL));
|
||||||
else
|
else
|
||||||
app_write_mpd_status_playlist (buf);
|
app_write_mpd_status_playlist (buf);
|
||||||
@ -1385,6 +1361,53 @@ app_write_mpd_status (struct row_buffer *buf)
|
|||||||
row_buffer_free (&right);
|
row_buffer_free (&right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
row_buffer_append_c (struct row_buffer *self, ucs4_t c, chtype attrs)
|
||||||
|
{
|
||||||
|
struct row_char current = { .attrs = attrs, .c = c };
|
||||||
|
struct row_char invalid = { .attrs = attrs, .c = '?', .width = 1 };
|
||||||
|
|
||||||
|
current.width = uc_width (current.c, locale_charset ());
|
||||||
|
if (current.width < 0 || !app_is_character_in_locale (current.c))
|
||||||
|
current = invalid;
|
||||||
|
|
||||||
|
ARRAY_RESERVE (self->chars, 1);
|
||||||
|
self->chars[self->chars_len++] = current;
|
||||||
|
self->total_width += current.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
app_write_editor (struct row_buffer *row)
|
||||||
|
{
|
||||||
|
int limit = COLS;
|
||||||
|
if (g.editor_prompt)
|
||||||
|
{
|
||||||
|
hard_assert (g.editor_prompt < 127);
|
||||||
|
row_buffer_append_c (row, g.editor_prompt, APP_ATTR (HIGHLIGHT));
|
||||||
|
limit--;
|
||||||
|
}
|
||||||
|
|
||||||
|
int following = 0;
|
||||||
|
for (size_t i = g.editor_point; i < g.editor_len; i++)
|
||||||
|
following += g.editor_w[i];
|
||||||
|
|
||||||
|
int preceding = 0;
|
||||||
|
size_t start = g.editor_point;
|
||||||
|
while (start && preceding < limit / 2)
|
||||||
|
preceding += g.editor_w[--start];
|
||||||
|
|
||||||
|
// There can be one extra space at the end of the line but this way we
|
||||||
|
// don't need to care about non-spacing marks following full-width chars
|
||||||
|
while (start && limit - preceding - following > 2 /* widest char */)
|
||||||
|
preceding += g.editor_w[--start];
|
||||||
|
|
||||||
|
// XXX: we should also show < > indicators for overflow but it'd probably
|
||||||
|
// considerably complicate this algorithm
|
||||||
|
for (; start < g.editor_len; start++)
|
||||||
|
row_buffer_append_c (row, g.editor_line[start], APP_ATTR (HIGHLIGHT));
|
||||||
|
return !!g.editor_prompt + preceding;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
app_draw_statusbar (void)
|
app_draw_statusbar (void)
|
||||||
{
|
{
|
||||||
@ -1393,8 +1416,8 @@ app_draw_statusbar (void)
|
|||||||
struct row_buffer buf = row_buffer_make ();
|
struct row_buffer buf = row_buffer_make ();
|
||||||
if (g.message)
|
if (g.message)
|
||||||
row_buffer_append (&buf, g.message, APP_ATTR (HIGHLIGHT));
|
row_buffer_append (&buf, g.message, APP_ATTR (HIGHLIGHT));
|
||||||
else if (g.editor.line)
|
else if (g.editor_line)
|
||||||
caret = line_editor_write (&g.editor, &buf, COLS, APP_ATTR (HIGHLIGHT));
|
caret = app_write_editor (&buf);
|
||||||
else if (g.client.state == MPD_CONNECTED)
|
else if (g.client.state == MPD_CONNECTED)
|
||||||
app_write_mpd_status (&buf);
|
app_write_mpd_status (&buf);
|
||||||
|
|
||||||
@ -1553,7 +1576,6 @@ app_goto_tab (int tab_index)
|
|||||||
XX( CHOOSE, "Choose item" ) \
|
XX( CHOOSE, "Choose item" ) \
|
||||||
XX( DELETE, "Delete item" ) \
|
XX( DELETE, "Delete item" ) \
|
||||||
XX( UP, "Go up a level" ) \
|
XX( UP, "Go up a level" ) \
|
||||||
XX( MULTISELECT, "Toggle multiselect" ) \
|
|
||||||
\
|
\
|
||||||
XX( SCROLL_UP, "Scroll up" ) \
|
XX( SCROLL_UP, "Scroll up" ) \
|
||||||
XX( SCROLL_DOWN, "Scroll down" ) \
|
XX( SCROLL_DOWN, "Scroll down" ) \
|
||||||
@ -1623,6 +1645,199 @@ action_resolve (const char *name)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Line editor -------------------------------------------------------------
|
||||||
|
|
||||||
|
// TODO: move the editor out as a component to liberty-tui.c
|
||||||
|
|
||||||
|
/// Notify whomever invoked the editor that it's been either confirmed or
|
||||||
|
/// cancelled and clean up editor state
|
||||||
|
static void
|
||||||
|
app_editor_abort (bool status)
|
||||||
|
{
|
||||||
|
g.on_editor_end (status);
|
||||||
|
g.on_editor_changed = NULL;
|
||||||
|
g.on_editor_end = NULL;
|
||||||
|
|
||||||
|
free (g.editor_line);
|
||||||
|
g.editor_line = NULL;
|
||||||
|
free (g.editor_w);
|
||||||
|
g.editor_w = NULL;
|
||||||
|
g.editor_alloc = 0;
|
||||||
|
g.editor_len = 0;
|
||||||
|
g.editor_point = 0;
|
||||||
|
g.editor_prompt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start the line editor; remember to fill in "change" and "abort" callbacks
|
||||||
|
static void
|
||||||
|
app_editor_start (char prompt)
|
||||||
|
{
|
||||||
|
g.editor_alloc = 16;
|
||||||
|
g.editor_line = xcalloc (sizeof *g.editor_line, g.editor_alloc);
|
||||||
|
g.editor_w = xcalloc (sizeof *g.editor_w, g.editor_alloc);
|
||||||
|
g.editor_len = 0;
|
||||||
|
g.editor_point = 0;
|
||||||
|
g.editor_prompt = prompt;
|
||||||
|
app_invalidate ();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
app_editor_changed (void)
|
||||||
|
{
|
||||||
|
g.editor_line[g.editor_len] = 0;
|
||||||
|
g.editor_w[g.editor_len] = 0;
|
||||||
|
|
||||||
|
if (g.on_editor_changed)
|
||||||
|
g.on_editor_changed ();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
app_editor_move (int to, int from, int len)
|
||||||
|
{
|
||||||
|
memmove (g.editor_line + to, g.editor_line + from,
|
||||||
|
sizeof *g.editor_line * len);
|
||||||
|
memmove (g.editor_w + to, g.editor_w + from,
|
||||||
|
sizeof *g.editor_w * len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
app_editor_process_action (enum action action)
|
||||||
|
{
|
||||||
|
app_invalidate ();
|
||||||
|
switch (action)
|
||||||
|
{
|
||||||
|
case ACTION_QUIT:
|
||||||
|
app_editor_abort (false);
|
||||||
|
return true;
|
||||||
|
case ACTION_EDITOR_CONFIRM:
|
||||||
|
app_editor_abort (true);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case ACTION_EDITOR_B_CHAR:
|
||||||
|
if (g.editor_point < 1)
|
||||||
|
return false;
|
||||||
|
do g.editor_point--;
|
||||||
|
while (g.editor_point > 0
|
||||||
|
&& !g.editor_w[g.editor_point]);
|
||||||
|
return true;
|
||||||
|
case ACTION_EDITOR_F_CHAR:
|
||||||
|
if (g.editor_point + 1 > (int) g.editor_len)
|
||||||
|
return false;
|
||||||
|
do g.editor_point++;
|
||||||
|
while (g.editor_point < (int) g.editor_len
|
||||||
|
&& !g.editor_w[g.editor_point]);
|
||||||
|
return true;
|
||||||
|
case ACTION_EDITOR_B_WORD:
|
||||||
|
{
|
||||||
|
if (g.editor_point < 1)
|
||||||
|
return false;
|
||||||
|
int i = g.editor_point;
|
||||||
|
while (i && g.editor_line[--i] == ' ');
|
||||||
|
while (i-- && g.editor_line[i] != ' ');
|
||||||
|
g.editor_point = ++i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case ACTION_EDITOR_F_WORD:
|
||||||
|
{
|
||||||
|
if (g.editor_point + 1 > (int) g.editor_len)
|
||||||
|
return false;
|
||||||
|
int i = g.editor_point;
|
||||||
|
while (i < (int) g.editor_len && g.editor_line[i] != ' ') i++;
|
||||||
|
while (i < (int) g.editor_len && g.editor_line[i] == ' ') i++;
|
||||||
|
g.editor_point = i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case ACTION_EDITOR_HOME:
|
||||||
|
g.editor_point = 0;
|
||||||
|
return true;
|
||||||
|
case ACTION_EDITOR_END:
|
||||||
|
g.editor_point = g.editor_len;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ACTION_EDITOR_B_DELETE:
|
||||||
|
{
|
||||||
|
if (g.editor_point < 1)
|
||||||
|
return false;
|
||||||
|
int len = 1;
|
||||||
|
while (g.editor_point - len > 0
|
||||||
|
&& !g.editor_w[g.editor_point - len])
|
||||||
|
len++;
|
||||||
|
app_editor_move (g.editor_point - len, g.editor_point,
|
||||||
|
g.editor_len - g.editor_point);
|
||||||
|
g.editor_len -= len;
|
||||||
|
g.editor_point -= len;
|
||||||
|
app_editor_changed ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case ACTION_EDITOR_F_DELETE:
|
||||||
|
{
|
||||||
|
if (g.editor_point + 1 > (int) g.editor_len)
|
||||||
|
return false;
|
||||||
|
int len = 1;
|
||||||
|
while (g.editor_point + len < (int) g.editor_len
|
||||||
|
&& !g.editor_w[g.editor_point + len])
|
||||||
|
len++;
|
||||||
|
g.editor_len -= len;
|
||||||
|
app_editor_move (g.editor_point, g.editor_point + len,
|
||||||
|
g.editor_len - g.editor_point);
|
||||||
|
app_editor_changed ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case ACTION_EDITOR_B_KILL_WORD:
|
||||||
|
{
|
||||||
|
if (g.editor_point < 1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int i = g.editor_point;
|
||||||
|
while (i && g.editor_line[--i] == ' ');
|
||||||
|
while (i-- && g.editor_line[i] != ' ');
|
||||||
|
i++;
|
||||||
|
|
||||||
|
app_editor_move (i, g.editor_point, (g.editor_len - g.editor_point));
|
||||||
|
g.editor_len -= g.editor_point - i;
|
||||||
|
g.editor_point = i;
|
||||||
|
app_editor_changed ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case ACTION_EDITOR_B_KILL_LINE:
|
||||||
|
g.editor_len -= g.editor_point;
|
||||||
|
app_editor_move (0, g.editor_point, g.editor_len);
|
||||||
|
g.editor_point = 0;
|
||||||
|
app_editor_changed ();
|
||||||
|
return true;
|
||||||
|
case ACTION_EDITOR_F_KILL_LINE:
|
||||||
|
g.editor_len = g.editor_point;
|
||||||
|
app_editor_changed ();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
app_editor_insert (ucs4_t codepoint)
|
||||||
|
{
|
||||||
|
while (g.editor_alloc - g.editor_len < 2 /* inserted + sentinel */)
|
||||||
|
{
|
||||||
|
g.editor_alloc <<= 1;
|
||||||
|
g.editor_line = xreallocarray
|
||||||
|
(g.editor_line, sizeof *g.editor_line, g.editor_alloc);
|
||||||
|
g.editor_w = xreallocarray
|
||||||
|
(g.editor_w, sizeof *g.editor_w, g.editor_alloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
app_editor_move (g.editor_point + 1, g.editor_point,
|
||||||
|
g.editor_len - g.editor_point);
|
||||||
|
g.editor_line[g.editor_point] = codepoint;
|
||||||
|
g.editor_w[g.editor_point] = app_is_character_in_locale (codepoint)
|
||||||
|
? uc_width (codepoint, locale_charset ())
|
||||||
|
: 1 /* the replacement question mark */;
|
||||||
|
|
||||||
|
g.editor_point++;
|
||||||
|
g.editor_len++;
|
||||||
|
app_editor_changed ();
|
||||||
|
}
|
||||||
|
|
||||||
// --- User input handling -----------------------------------------------------
|
// --- User input handling -----------------------------------------------------
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -1687,7 +1902,7 @@ app_on_editor_end (bool confirmed)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
char *u8 = (char *) u32_to_u8 (g.editor.line, g.editor.len + 1, NULL, &len);
|
char *u8 = (char *) u32_to_u8 (g.editor_line, g.editor_len + 1, NULL, &len);
|
||||||
mpd_client_send_command_raw (c, u8);
|
mpd_client_send_command_raw (c, u8);
|
||||||
free (u8);
|
free (u8);
|
||||||
|
|
||||||
@ -1703,25 +1918,13 @@ app_process_action (enum action action)
|
|||||||
// First let the tab try to handle this
|
// First let the tab try to handle this
|
||||||
struct tab *tab = g.active_tab;
|
struct tab *tab = g.active_tab;
|
||||||
if (tab->on_action && tab->on_action (action))
|
if (tab->on_action && tab->on_action (action))
|
||||||
{
|
|
||||||
tab->item_mark = -1;
|
|
||||||
app_invalidate ();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case ACTION_NONE:
|
case ACTION_NONE:
|
||||||
return true;
|
return true;
|
||||||
case ACTION_QUIT:
|
case ACTION_QUIT:
|
||||||
// It is a pseudomode, avoid surprising the user
|
|
||||||
if (tab->item_mark > -1)
|
|
||||||
{
|
|
||||||
tab->item_mark = -1;
|
|
||||||
app_invalidate ();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
app_quit ();
|
app_quit ();
|
||||||
return true;
|
return true;
|
||||||
case ACTION_REDRAW:
|
case ACTION_REDRAW:
|
||||||
@ -1729,25 +1932,12 @@ app_process_action (enum action action)
|
|||||||
app_invalidate ();
|
app_invalidate ();
|
||||||
return true;
|
return true;
|
||||||
case ACTION_MPD_COMMAND:
|
case ACTION_MPD_COMMAND:
|
||||||
line_editor_start (&g.editor, ':');
|
app_editor_start (':');
|
||||||
g.editor.on_end = app_on_editor_end;
|
g.on_editor_end = app_on_editor_end;
|
||||||
app_invalidate ();
|
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case ACTION_MULTISELECT:
|
|
||||||
if (!tab->can_multiselect
|
|
||||||
|| !tab->item_count || tab->item_selected < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
app_invalidate ();
|
|
||||||
if (tab->item_mark > -1)
|
|
||||||
tab->item_mark = -1;
|
|
||||||
else
|
|
||||||
tab->item_mark = tab->item_selected;
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case ACTION_LAST_TAB:
|
case ACTION_LAST_TAB:
|
||||||
if (!g.last_tab)
|
if (!g.last_tab)
|
||||||
return false;
|
return false;
|
||||||
@ -1816,49 +2006,6 @@ app_process_action (enum action action)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
|
||||||
app_editor_process_action (enum action action)
|
|
||||||
{
|
|
||||||
app_invalidate ();
|
|
||||||
switch (action)
|
|
||||||
{
|
|
||||||
case ACTION_QUIT:
|
|
||||||
line_editor_abort (&g.editor, false);
|
|
||||||
g.editor.on_end = NULL;
|
|
||||||
return true;
|
|
||||||
case ACTION_EDITOR_CONFIRM:
|
|
||||||
line_editor_abort (&g.editor, true);
|
|
||||||
g.editor.on_end = NULL;
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
|
|
||||||
case ACTION_EDITOR_B_CHAR:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_B_CHAR);
|
|
||||||
case ACTION_EDITOR_F_CHAR:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_F_CHAR);
|
|
||||||
case ACTION_EDITOR_B_WORD:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_B_WORD);
|
|
||||||
case ACTION_EDITOR_F_WORD:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_F_WORD);
|
|
||||||
case ACTION_EDITOR_HOME:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_HOME);
|
|
||||||
case ACTION_EDITOR_END:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_END);
|
|
||||||
|
|
||||||
case ACTION_EDITOR_B_DELETE:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_B_DELETE);
|
|
||||||
case ACTION_EDITOR_F_DELETE:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_F_DELETE);
|
|
||||||
case ACTION_EDITOR_B_KILL_WORD:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_B_KILL_WORD);
|
|
||||||
case ACTION_EDITOR_B_KILL_LINE:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_B_KILL_LINE);
|
|
||||||
case ACTION_EDITOR_F_KILL_LINE:
|
|
||||||
return line_editor_action (&g.editor, LINE_EDITOR_F_KILL_LINE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -1939,8 +2086,8 @@ app_process_mouse (termo_mouse_event_t type, int line, int column, int button,
|
|||||||
if (type != TERMO_MOUSE_PRESS)
|
if (type != TERMO_MOUSE_PRESS)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (g.editor.line)
|
if (g.editor_line)
|
||||||
line_editor_abort (&g.editor, false);
|
app_editor_abort (false);
|
||||||
|
|
||||||
if (button == 1)
|
if (button == 1)
|
||||||
return app_process_left_mouse_click (line, column, double_click);
|
return app_process_left_mouse_click (line, column, double_click);
|
||||||
@ -2000,7 +2147,6 @@ g_normal_defaults[] =
|
|||||||
{ "Enter", ACTION_CHOOSE },
|
{ "Enter", ACTION_CHOOSE },
|
||||||
{ "Delete", ACTION_DELETE },
|
{ "Delete", ACTION_DELETE },
|
||||||
{ "Backspace", ACTION_UP },
|
{ "Backspace", ACTION_UP },
|
||||||
{ "v", ACTION_MULTISELECT },
|
|
||||||
{ "a", ACTION_MPD_ADD },
|
{ "a", ACTION_MPD_ADD },
|
||||||
{ "r", ACTION_MPD_REPLACE },
|
{ "r", ACTION_MPD_REPLACE },
|
||||||
{ ":", ACTION_MPD_COMMAND },
|
{ ":", ACTION_MPD_COMMAND },
|
||||||
@ -2124,7 +2270,7 @@ static bool
|
|||||||
app_process_termo_event (termo_key_t *event)
|
app_process_termo_event (termo_key_t *event)
|
||||||
{
|
{
|
||||||
struct binding dummy = { *event, 0, 0 }, *binding;
|
struct binding dummy = { *event, 0, 0 }, *binding;
|
||||||
if (g.editor.line)
|
if (g.editor_line)
|
||||||
{
|
{
|
||||||
if ((binding = bsearch (&dummy, g_editor_keys, g_editor_keys_len,
|
if ((binding = bsearch (&dummy, g_editor_keys, g_editor_keys_len,
|
||||||
sizeof *binding, app_binding_cmp)))
|
sizeof *binding, app_binding_cmp)))
|
||||||
@ -2132,7 +2278,7 @@ app_process_termo_event (termo_key_t *event)
|
|||||||
if (event->type != TERMO_TYPE_KEY || event->modifiers != 0)
|
if (event->type != TERMO_TYPE_KEY || event->modifiers != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
line_editor_insert (&g.editor, event->code.codepoint);
|
app_editor_insert (event->code.codepoint);
|
||||||
app_invalidate ();
|
app_invalidate ();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -2199,8 +2345,6 @@ current_tab_move_song (const char *id, int diff)
|
|||||||
char *target_str = xstrdup_printf ("%d", target);
|
char *target_str = xstrdup_printf ("%d", target);
|
||||||
mpd_client_send_command (c, "moveid", id, target_str, NULL);
|
mpd_client_send_command (c, "moveid", id, target_str, NULL);
|
||||||
free (target_str);
|
free (target_str);
|
||||||
// TODO: we should create a cancellable action waiting for the move to
|
|
||||||
// finish, so that holding Shift-arrows works as expected.
|
|
||||||
mpd_client_add_task (c, mpd_on_simple_response, NULL);
|
mpd_client_add_task (c, mpd_on_simple_response, NULL);
|
||||||
mpd_client_idle (c, 0);
|
mpd_client_idle (c, 0);
|
||||||
|
|
||||||
@ -2245,7 +2389,6 @@ current_tab_init (void)
|
|||||||
{
|
{
|
||||||
struct tab *super = &g_current_tab;
|
struct tab *super = &g_current_tab;
|
||||||
tab_init (super, "Current");
|
tab_init (super, "Current");
|
||||||
// TODO: implement multiselect, set can_multiselect to true
|
|
||||||
super->on_action = current_tab_on_action;
|
super->on_action = current_tab_on_action;
|
||||||
super->on_item_draw = current_tab_on_item_draw;
|
super->on_item_draw = current_tab_on_item_draw;
|
||||||
return super;
|
return super;
|
||||||
@ -2471,9 +2614,6 @@ library_tab_on_data (const struct mpd_response *response,
|
|||||||
(int (*) (const void *, const void *)) library_tab_compare);
|
(int (*) (const void *, const void *)) library_tab_compare);
|
||||||
g_library_tab.super.item_count = items->len;
|
g_library_tab.super.item_count = items->len;
|
||||||
|
|
||||||
// XXX: this unmarks even if just the database updates
|
|
||||||
g_library_tab.super.item_mark = -1;
|
|
||||||
|
|
||||||
// Don't force the selection visible when there's no need to touch it
|
// Don't force the selection visible when there's no need to touch it
|
||||||
if (g_library_tab.super.item_selected >= (int) items->len)
|
if (g_library_tab.super.item_selected >= (int) items->len)
|
||||||
app_move_selection (0);
|
app_move_selection (0);
|
||||||
@ -2494,37 +2634,22 @@ library_tab_reload (const char *new_path)
|
|||||||
mpd_client_idle (c, 0);
|
mpd_client_idle (c, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
|
||||||
library_tab_is_range_playable (struct tab_range range)
|
|
||||||
{
|
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
|
||||||
{
|
|
||||||
struct library_tab_item x =
|
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
library_tab_on_action (enum action action)
|
library_tab_on_action (enum action action)
|
||||||
{
|
{
|
||||||
|
struct tab *self = g.active_tab;
|
||||||
|
if (self->item_selected < 0 || !self->item_count)
|
||||||
|
return false;
|
||||||
|
|
||||||
struct mpd_client *c = &g.client;
|
struct mpd_client *c = &g.client;
|
||||||
struct tab_range range = tab_selection_range (&g_library_tab.super);
|
if (c->state != MPD_CONNECTED)
|
||||||
if (range.from < 0 || c->state != MPD_CONNECTED)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
struct library_tab_item x =
|
struct library_tab_item x =
|
||||||
library_tab_resolve (g_library_tab.items.vector[range.from]);
|
library_tab_resolve (g_library_tab.items.vector[self->item_selected]);
|
||||||
|
|
||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case ACTION_CHOOSE:
|
case ACTION_CHOOSE:
|
||||||
// I can't think of a reasonable way of handling that
|
|
||||||
if (range.from != range.upto)
|
|
||||||
break;
|
|
||||||
|
|
||||||
switch (x.type)
|
switch (x.type)
|
||||||
{
|
{
|
||||||
case LIBRARY_ROOT:
|
case LIBRARY_ROOT:
|
||||||
@ -2545,41 +2670,26 @@ library_tab_on_action (enum action action)
|
|||||||
return parent != NULL;
|
return parent != NULL;
|
||||||
}
|
}
|
||||||
case ACTION_MPD_REPLACE:
|
case ACTION_MPD_REPLACE:
|
||||||
if (!library_tab_is_range_playable (range))
|
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Clears the playlist (which stops playback), add what user wanted
|
// Clears the playlist (which stops playback), add what user wanted
|
||||||
// to replace it with, and eventually restore playback;
|
// to replace it with, and eventually restore playback;
|
||||||
// I can't think of a reliable alternative that omits the "play"
|
// I can't think of a reliable alternative that omits the "play"
|
||||||
mpd_client_list_begin (c);
|
mpd_client_list_begin (c);
|
||||||
|
|
||||||
mpd_client_send_command (c, "clear", NULL);
|
mpd_client_send_command (c, "clear", NULL);
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
mpd_client_send_command (c, "add", x.path, NULL);
|
||||||
{
|
|
||||||
struct library_tab_item x =
|
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
|
||||||
mpd_client_send_command (c, "add", x.path, NULL);
|
|
||||||
}
|
|
||||||
if (g.state == PLAYER_PLAYING)
|
if (g.state == PLAYER_PLAYING)
|
||||||
mpd_client_send_command (c, "play", NULL);
|
mpd_client_send_command (c, "play", NULL);
|
||||||
|
|
||||||
mpd_client_list_end (c);
|
mpd_client_list_end (c);
|
||||||
mpd_client_add_task (c, mpd_on_simple_response, NULL);
|
mpd_client_add_task (c, mpd_on_simple_response, NULL);
|
||||||
mpd_client_idle (c, 0);
|
mpd_client_idle (c, 0);
|
||||||
return true;
|
return true;
|
||||||
case ACTION_MPD_ADD:
|
case ACTION_MPD_ADD:
|
||||||
if (!library_tab_is_range_playable (range))
|
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
for (int i = range.from; i <= range.upto; i++)
|
return MPD_SIMPLE ("add", x.path);
|
||||||
{
|
|
||||||
struct library_tab_item x =
|
|
||||||
library_tab_resolve (g_library_tab.items.vector[i]);
|
|
||||||
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
|
|
||||||
MPD_SIMPLE ("add", x.path);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2594,7 +2704,6 @@ library_tab_init (void)
|
|||||||
|
|
||||||
struct tab *super = &g_library_tab.super;
|
struct tab *super = &g_library_tab.super;
|
||||||
tab_init (super, "Library");
|
tab_init (super, "Library");
|
||||||
super->can_multiselect = true;
|
|
||||||
super->on_action = library_tab_on_action;
|
super->on_action = library_tab_on_action;
|
||||||
super->on_item_draw = library_tab_on_item_draw;
|
super->on_item_draw = library_tab_on_item_draw;
|
||||||
return super;
|
return super;
|
||||||
@ -3521,7 +3630,7 @@ app_on_key_timer (void *user_data)
|
|||||||
termo_key_t event;
|
termo_key_t event;
|
||||||
if (termo_getkey_force (g.tk, &event) == TERMO_RES_KEY)
|
if (termo_getkey_force (g.tk, &event) == TERMO_RES_KEY)
|
||||||
if (!app_process_termo_event (&event))
|
if (!app_process_termo_event (&event))
|
||||||
beep ();
|
app_quit ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user