Skip to content

Commit

Permalink
Add 'previewoptions' option
Browse files Browse the repository at this point in the history
At the moment only allows tweaking delay for graphics preview.

Thanks to Joshua Jensch (a.k.a., patroclos).

See vifm#481 (comment)
  • Loading branch information
xaizek committed Dec 6, 2020
1 parent 3d5e7c8 commit e825f52
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 12 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
Added a and A view mode keys that switch to next and previous viewer of
current file correspondingly. Thanks to j-xella.

Added 'previewoptions' option to allow tweaking delay for graphics
preview. Thanks to Joshua Jensch (a.k.a., patroclos).

Made :VifmCs of the plugin fail when 'termguicolors' produces a 24-bit color
value. Thanks to AtomToast.

Expand Down
1 change: 1 addition & 0 deletions THANKS
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ John Shea (coachshea)
Jose Riha (jose1711)
Joseph LP (ootput)
Josh Wainwright (joshaw)
Joshua Jensch (patroclos)
jsmith51 (metal.lunchbox)
kalterfive
kangshugang
Expand Down
17 changes: 16 additions & 1 deletion data/man/vifm.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH VIFM 1 "December 4, 2020" "vifm 0.11"
.TH VIFM 1 "December 6, 2020" "vifm 0.11"
.\" ---------------------------------------------------------------------------
.SH NAME
.\" ---------------------------------------------------------------------------
Expand Down Expand Up @@ -4245,6 +4245,21 @@ scope: local
.br
Minimal number of characters for line number field.
.TP
.BI "'previewoptions'"
type: string list
.br
default: "graphicsdelay:50000"
.br

Tweaks how previewing is done (in quick view, miller view's column and
view mode).

item default meaning
graphicsdelay:num 0 delay before drawing graphics (microseconds)

graphicsdelay is needed if terminal requires some timeout before it can
draw graphics (otherwise it gets lost).
.TP
.BI "'previewprg'"
type: string
.br
Expand Down
18 changes: 17 additions & 1 deletion data/vim/doc/app/vifm-app.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*vifm-app.txt* For Vifm version 0.11 Last change: 2020 Dec 4
*vifm-app.txt* For Vifm version 0.11 Last change: 2020 Dec 6

Email for bugs and suggestions: <[email protected]>

Expand Down Expand Up @@ -3559,6 +3559,22 @@ scope: local

Minimal number of characters for line number field.

*vifm-'previewoptions'*
previewoptions
type: string list
default: "graphicsdelay:50000"

Tweaks how previewing is done (in quick view, miller view's column and
view mode).

item default meaning ~
graphicsdelay:num 0 delay before drawing graphics (microseconds)

graphicsdelay is needed if terminal requires some timeout before it can
draw graphics (otherwise it gets lost).

Default value is used when item is missing from the option.

*vifm-'previewprg'*
previewprg
type: string
Expand Down
17 changes: 9 additions & 8 deletions data/vim/syntax/vifm.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" vifm syntax file
" Maintainer: xaizek <[email protected]>
" Last Change: October 6, 2020
" Last Change: December 6, 2020
" Inspired By: Vim syntax file by Dr. Charles E. Campbell, Jr.

if exists('b:current_syntax')
Expand Down Expand Up @@ -141,13 +141,14 @@ syntax keyword vifmOption contained aproposprg autochpos caseoptions cdpath cd
\ followlinks fusehome gdefault grepprg histcursor history hi hlsearch hls
\ iec ignorecase ic iooptions incsearch is laststatus lines locateprg ls
\ lsoptions lsview mediaprg milleroptions millerview mintimeoutlen number nu
\ numberwidth nuw previewprg quickview relativenumber rnu rulerformat ruf
\ runexec scrollbind scb scrolloff sessionoptions ssop so sort sortgroups
\ sortorder sortnumbers shell sh shellflagcmd shcf shortmess shm showtabline
\ stal sizefmt slowfs smartcase scs statusline stl suggestoptions syncregs
\ syscalls tablabel tabprefix tabscope tabstop tabsuffix timefmt timeoutlen
\ title tm trash trashdir ts tuioptions to undolevels ul vicmd viewcolumns
\ vifminfo vimhelp vixcmd wildmenu wmnu wildstyle wordchars wrap wrapscan ws
\ numberwidth nuw previewoptions previewprg quickview relativenumber rnu
\ rulerformat ruf runexec scrollbind scb scrolloff sessionoptions ssop so
\ sort sortgroups sortorder sortnumbers shell sh shellflagcmd shcf shortmess
\ shm showtabline stal sizefmt slowfs smartcase scs statusline stl
\ suggestoptions syncregs syscalls tablabel tabprefix tabscope tabstop
\ tabsuffix timefmt timeoutlen title tm trash trashdir ts tuioptions to
\ undolevels ul vicmd viewcolumns vifminfo vimhelp vixcmd wildmenu wmnu
\ wildstyle wordchars wrap wrapscan ws

" Disabled boolean options
syntax keyword vifmOption contained noautochpos nocf nochaselinks nodotfiles
Expand Down
2 changes: 2 additions & 0 deletions src/cfg/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ cfg_init(void)

cfg.chase_links = 0;

cfg.graphics_delay = 50000;

cfg.timeout_len = 1000;
cfg.min_timeout_len = 150;

Expand Down
2 changes: 2 additions & 0 deletions src/cfg/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ typedef struct config_t
* link expanded). */
int chase_links;

int graphics_delay; /* Delay before redrawing graphics in microseconds. */

int timeout_len; /* Maximum period on waiting for the input. */
int min_timeout_len; /* Minimum period on waiting for the input. */

Expand Down
2 changes: 2 additions & 0 deletions src/cfg/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,8 @@ store_global_options(JSON_Object *root)
append_dstr(options, format_str("rulerformat=%s",
escape_spaces(cfg.ruler_format)));
append_dstr(options, format_str("%srunexec", cfg.auto_execute ? "" : "no"));
append_dstr(options, format_str("previewoptions=%s",
escape_spaces(vle_opts_get("previewoptions", OPT_GLOBAL))));
append_dstr(options, format_str("%sscrollbind", cfg.scroll_bind ? "" : "no"));
append_dstr(options, format_str("scrolloff=%d", cfg.scroll_off));
append_dstr(options, format_str("shell=%s", escape_spaces(cfg.shell)));
Expand Down
2 changes: 1 addition & 1 deletion src/modes/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ get_view_data(modview_info_t *vi, const char file_to_view[])
{
/* Wait a bit to let terminal emulator do actual refresh (at least some of
* them need this). */
usleep(50000);
usleep(cfg.graphics_delay);
}

const char *error;
Expand Down
74 changes: 74 additions & 0 deletions src/opt_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static void init_lsoptions(optval_t *val);
static void init_lsview(optval_t *val);
static void init_milleroptions(optval_t *val);
static void init_millerview(optval_t *val);
static void init_previewoptions(optval_t *val);
static void init_quickview(optval_t *val);
static void init_shortmess(optval_t *val);
static void init_sizefmt(optval_t *val);
Expand Down Expand Up @@ -147,6 +148,7 @@ static void mediaprg_handler(OPT_OP op, optval_t val);
#endif
static void mintimeoutlen_handler(OPT_OP op, optval_t val);
static void scroll_line_down(view_t *view);
static void previewoptions_handler(OPT_OP op, optval_t val);
static void quickview_handler(OPT_OP op, optval_t val);
static void rulerformat_handler(OPT_OP op, optval_t val);
static void runexec_handler(OPT_OP op, optval_t val);
Expand Down Expand Up @@ -328,6 +330,11 @@ static const char *lsoptions_enum[][2] = {
{ "transposed", "fill grid by column instead of by line" },
};

/* Possible values of 'previewoptions'. */
static const char *previewoptions_vals[][2] = {
{ "graphicsdelay:", "delay before drawing graphics" },
};

/* Possible values of 'suggestoptions'. */
static const char *suggestoptions_vals[][2] = {
[BIT(SF_NORMAL)] = { "normal", "display in normal mode" },
Expand Down Expand Up @@ -701,6 +708,11 @@ options[] = {
OPT_INT, 0, NULL, &mintimeoutlen_handler, NULL,
{ .ref.int_val = &cfg.min_timeout_len },
},
{ "previewoptions", "", "tweaks for how preview is done",
OPT_STRLIST, ARRAY_LEN(previewoptions_vals), previewoptions_vals,
&previewoptions_handler, NULL,
{ .init = &init_previewoptions },
},
{ "quickview", "", "whether quick view is active",
OPT_BOOL, 0, NULL, &quickview_handler, NULL,
{ .init = &init_quickview },
Expand Down Expand Up @@ -1158,6 +1170,20 @@ init_millerview(optval_t *val)
val->bool_val = curr_view->miller_view_g;
}

/* Initializes 'previewoptions' option from global state. */
static void
init_previewoptions(optval_t *val)
{
static char buf[64];
buf[0] = '\0';

if(cfg.graphics_delay != 0)
{
snprintf(buf, sizeof(buf), "graphicsdelay:%d", cfg.graphics_delay);
}
val->str_val = buf;
}

/* Initializes 'quickview' option from global state. */
static void
init_quickview(optval_t *val)
Expand Down Expand Up @@ -2239,6 +2265,54 @@ scroll_line_down(view_t *view)
wresize(view->win, view->window_rows, view->window_cols);
}

/* Handles updates of the 'previewoptions' option. */
static void
previewoptions_handler(OPT_OP op, optval_t val)
{
char *new_val = strdup(val.str_val);
char *part = new_val, *state = NULL;

int graphics_delay = 0;

while((part = split_and_get(part, ',', &state)) != NULL)
{
if(starts_with_lit(part, "graphicsdelay:"))
{
const char *const num = after_first(part, ':');
if(!read_int(num, &graphics_delay))
{
vle_tb_append_linef(vle_err,
"Failed to parse \"graphicsdelay\" value: %s", num);
break;
}
if(graphics_delay < 0)
{
vle_tb_append_linef(vle_err,
"\"graphicsdelay\" can't be negative, got: %s", num);
break;
}
}
else
{
break_at(part, ':');
vle_tb_append_linef(vle_err,
"Unknown key for 'previewoptions' option: %s", part);
break;
}
}
free(new_val);

if(part == NULL)
{
cfg.graphics_delay = graphics_delay;
}

/* In case of error, restore previous value, otherwise reload it anyway to
* remove any duplicates. */
init_previewoptions(&val);
vle_opts_assign("previewoptions", val, OPT_GLOBAL);
}

/* Handles switch that controls visibility of quick view. */
static void
quickview_handler(OPT_OP op, optval_t val)
Expand Down
1 change: 1 addition & 0 deletions src/tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const char *tags[] = {
"vifm-'number'",
"vifm-'numberwidth'",
"vifm-'nuw'",
"vifm-'previewoptions'",
"vifm-'previewprg'",
"vifm-'quickview'",
"vifm-'relativenumber'",
Expand Down
2 changes: 1 addition & 1 deletion src/ui/quickview.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ view_file(const char path[], const preview_area_t *parea,
if(kind != VK_TEXTUAL)
{
cleanup_area(parea, curr_stats.preview.cleanup_cmd);
usleep(50000);
usleep(cfg.graphics_delay);
}
else
{
Expand Down
28 changes: 28 additions & 0 deletions tests/misc/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,5 +726,33 @@ TEST(sessionoptions)
assert_int_equal(0, cfg.session_options);
}

TEST(previewoptions)
{
assert_success(exec_commands("set previewoptions=graphicsdelay:12345", &lwin,
CIT_COMMAND));
assert_int_equal(12345, cfg.graphics_delay);

assert_failure(exec_commands("set previewoptions=graphicsdelay:inf", &lwin,
CIT_COMMAND));
assert_int_equal(12345, cfg.graphics_delay);
assert_string_equal("Failed to parse \"graphicsdelay\" value: inf",
vle_tb_get_data(vle_err));

assert_failure(exec_commands("set previewoptions=graphicsdelay:-12345", &lwin,
CIT_COMMAND));
assert_int_equal(12345, cfg.graphics_delay);
assert_string_equal("\"graphicsdelay\" can't be negative, got: -12345",
vle_tb_get_data(vle_err));

assert_failure(exec_commands("set previewoptions=graphicsdelay:145,wtf",
&lwin, CIT_COMMAND));
assert_int_equal(12345, cfg.graphics_delay);
assert_string_equal("Unknown key for 'previewoptions' option: wtf",
vle_tb_get_data(vle_err));

assert_success(exec_commands("set previewoptions=", &lwin, CIT_COMMAND));
assert_int_equal(0, cfg.graphics_delay);
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
2 changes: 2 additions & 0 deletions tests/test-data/syntax-highlight/syntax.vifm
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ hi OddLine
" "sessionoptions" and "ssop" should be highlighted as 'option'
" "tabprefix" should be highlighted as 'option'
" "tabsuffix" should be highlighted as 'option'
" "previewoptions" should be highlighted as 'option'
set dotfiles nodotfiles invdotfiles dotfiles!
set caseoptions
set sizefmt
Expand All @@ -539,6 +540,7 @@ set tablabel
set sessionoptions ssop
set tabprefix
set tabsuffix
set previewoptions

" "term" should be highlighted as a function()
echo term('cmd')
Expand Down

0 comments on commit e825f52

Please sign in to comment.