Allow for more advanced window 'line' formatting

This commit is contained in:
Dave Davenport 2016-07-12 10:44:43 +02:00
parent 9e41b6ee95
commit b47e9c3846
5 changed files with 84 additions and 40 deletions

View File

@ -128,5 +128,5 @@ Settings config = {
.scrollbar_width = 8, .scrollbar_width = 8,
.scroll_method = 0, .scroll_method = 0,
.fake_background = "screenshot", .fake_background = "screenshot",
.window_format = "w c t", .window_format = "{w} {c} {t}",
}; };

View File

@ -587,7 +587,9 @@ See [here](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html) for
Format what is being displayed for windows. Format what is being displayed for windows.
*format*: *format*: {field[:len]}
*field*:
* **w**: Desktop name * **w**: Desktop name
* **t**: Title of window * **t**: Title of window
@ -595,7 +597,10 @@ Format what is being displayed for windows.
* **r**: Role * **r**: Role
* **c**: Class * **c**: Class
default: w c t *len*: maximum field length (0 for auto-size). If length negative and window *width* is negative field length is *width - len*.
default: {w} {c} {t}
### Message dialog ### Message dialog

View File

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3 .\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3
. .
.TH "ROFI\-MANPAGE" "" "2016-07-11" "" "" .TH "ROFI\-MANPAGE" "" "July 2016" "" ""
. .
.SH "NAME" .SH "NAME"
\fBrofi\fR \- A window switcher, run launcher, ssh dialog and dmenu replacement \fBrofi\fR \- A window switcher, run launcher, ssh dialog and dmenu replacement
@ -936,7 +936,10 @@ Tell \fBrofi\fR that DMenu input is pango markup encoded and should be rendered\
Format what is being displayed for windows\. Format what is being displayed for windows\.
. .
.P .P
\fIformat\fR: \fIformat\fR: {field[:len]}
.
.P
\fIfield\fR:
. .
.IP "\(bu" 4 .IP "\(bu" 4
\fBw\fR: Desktop name \fBw\fR: Desktop name
@ -956,7 +959,10 @@ Format what is being displayed for windows\.
.IP "" 0 .IP "" 0
. .
.P .P
default: w c t \fIlen\fR: maximum field length (0 for auto\-size)\. If length negative and window \fIwidth\fR is negative field length is \fIwidth \- len\fR\.
.
.P
default: {w} {c} {t}
. .
.SS "Message dialog" .SS "Message dialog"
\fB\-e\fR \fImessage\fR \fB\-e\fR \fImessage\fR

View File

@ -39,15 +39,15 @@ typedef struct
typedef enum typedef enum
{ {
TB_AUTOHEIGHT = 1 << 0, TB_AUTOHEIGHT = 1 << 0,
TB_AUTOWIDTH = 1 << 1, TB_AUTOWIDTH = 1 << 1,
TB_LEFT = 1 << 16, TB_LEFT = 1 << 16,
TB_RIGHT = 1 << 17, TB_RIGHT = 1 << 17,
TB_CENTER = 1 << 18, TB_CENTER = 1 << 18,
TB_EDITABLE = 1 << 19, TB_EDITABLE = 1 << 19,
TB_MARKUP = 1 << 20, TB_MARKUP = 1 << 20,
TB_WRAP = 1 << 21, TB_WRAP = 1 << 21,
TB_PASSWORD = 1 << 22, TB_PASSWORD = 1 << 22,
} TextboxFlags; } TextboxFlags;
typedef enum typedef enum

View File

@ -96,6 +96,7 @@ typedef struct
unsigned int name_len; unsigned int name_len;
unsigned int title_len; unsigned int title_len;
unsigned int role_len; unsigned int role_len;
GRegex *window_regex;
} ModeModePrivateData; } ModeModePrivateData;
winlist *cache_client = NULL; winlist *cache_client = NULL;
@ -495,6 +496,7 @@ static int window_mode_init ( Mode *sw )
{ {
if ( mode_get_private_data ( sw ) == NULL ) { if ( mode_get_private_data ( sw ) == NULL ) {
ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) ); ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
pd->window_regex = g_regex_new ( "{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL );
mode_set_private_data ( sw, (void *) pd ); mode_set_private_data ( sw, (void *) pd );
_window_mode_load_data ( sw, FALSE ); _window_mode_load_data ( sw, FALSE );
} }
@ -504,6 +506,7 @@ static int window_mode_init_cd ( Mode *sw )
{ {
if ( mode_get_private_data ( sw ) == NULL ) { if ( mode_get_private_data ( sw ) == NULL ) {
ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) ); ModeModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
pd->window_regex = g_regex_new ( "{[-\\w]+(:-?[0-9]+)?}", 0, 0, NULL );
mode_set_private_data ( sw, (void *) pd ); mode_set_private_data ( sw, (void *) pd );
_window_mode_load_data ( sw, TRUE ); _window_mode_load_data ( sw, TRUE );
} }
@ -515,16 +518,16 @@ static inline int act_on_window ( xcb_window_t window )
int retv = TRUE; int retv = TRUE;
char **args = NULL; char **args = NULL;
int argc = 0; int argc = 0;
char window_str[100]; /* We are probably safe here */ char window_regex[100]; /* We are probably safe here */
g_snprintf ( window_str, sizeof window_str, "%d", window ); g_snprintf ( window_regex, sizeof window_regex, "%d", window );
helper_parse_setup ( config.window_command, &args, &argc, "{window}", window_str, NULL ); helper_parse_setup ( config.window_command, &args, &argc, "{window}", window_regex, NULL );
GError *error = NULL; GError *error = NULL;
g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error ); g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
if ( error != NULL ) { if ( error != NULL ) {
char *msg = g_strdup_printf ( "Failed to execute action for window: '%s'\nError: '%s'", window_str, error->message ); char *msg = g_strdup_printf ( "Failed to execute action for window: '%s'\nError: '%s'", window_regex, error->message );
rofi_view_error_dialog ( msg, FALSE ); rofi_view_error_dialog ( msg, FALSE );
g_free ( msg ); g_free ( msg );
// print error. // print error.
@ -615,39 +618,69 @@ static void window_mode_destroy ( Mode *sw )
x11_cache_free (); x11_cache_free ();
g_free ( rmpd->cache ); g_free ( rmpd->cache );
g_free ( rmpd ); g_free ( rmpd );
g_regex_unref ( rmpd->window_regex );
mode_set_private_data ( sw, NULL ); mode_set_private_data ( sw, NULL );
} }
} }
static char * _generate_display_string ( const ModeModePrivateData *pd, client *c ) struct arg
{ {
GString * str = g_string_new ( "" ); const ModeModePrivateData *pd;
const char *v = config.window_format; client *c;
ssize_t l = strlen ( v ); };
for ( ssize_t j = 0; j < l; j++ ) { static void helper_eval_add_str ( GString *str, const char *input, int l, int max_len )
if ( v[j] == 'w' ) { {
g_string_append_printf ( str, "%-*s", pd->wmdn_len, c->wmdesktopstr ); int nc = g_utf8_strlen ( input, -1 );
if ( l != 0 && nc > l ) {
nc = l;
int bl = g_utf8_offset_to_pointer ( input, nc ) - input;
g_string_append_len ( str, input, bl );
}
else{
g_string_append_printf ( str, "%-*s", l ? l : max_len, input ? input : "" );
}
}
static gboolean helper_eval_cb ( const GMatchInfo *info, GString *str, gpointer data )
{
struct arg *d = (struct arg *) data;
gchar *match;
// Get the match
match = g_match_info_fetch ( info, 0 );
if ( match != NULL ) {
int l = 0;
if ( match[2] == ':' ) {
l = (int) g_ascii_strtoll ( &match[3], NULL, 10 );
if ( l < 0 && config.menu_width < 0 ) {
l = -config.menu_width + l;
}
if ( l < 0 ) {
l = 0;
}
} }
else if ( v[j] == 'c' ) { if ( match[1] == 'w' ) {
g_string_append_printf ( str, "%-*s", pd->clf_len, c->class ? c->class : "" ); helper_eval_add_str ( str, d->c->wmdesktopstr, l, d->pd->wmdn_len );
} }
else if ( v[j] == 't' ) { else if ( match[1] == 'c' ) {
g_string_append_printf ( str, "%-*s", pd->title_len, c->title ? c->title : "" ); helper_eval_add_str ( str, d->c->class, l, d->pd->clf_len );
} }
else if ( v[j] == 'n' ) { else if ( match[1] == 't' ) {
g_string_append_printf ( str, "%-*s", pd->name_len, c->name ? c->name : "" ); helper_eval_add_str ( str, d->c->title, l, d->pd->title_len );
} }
else if ( v[j] == 'r' ) { else if ( match[1] == 'n' ) {
g_string_append_printf ( str, "%-*s", pd->role_len, c->role ? c->role : "" ); helper_eval_add_str ( str, d->c->name, l, d->pd->name_len );
} }
else { else if ( match[1] == 'r' ) {
g_string_append_c ( str, v[j] ); helper_eval_add_str ( str, d->c->role, l, d->pd->role_len );
} }
} }
return FALSE;
char *r = str->str; }
g_string_free ( str, FALSE ); static char * _generate_display_string ( const ModeModePrivateData *pd, client *c )
return r; {
struct arg d = { pd, c };
char *res = g_regex_replace_eval ( pd->window_regex, config.window_format, -1, 0, 0,
helper_eval_cb, &d, NULL );
return res;
} }
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, int get_entry ) static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, int get_entry )