Fix option validation order
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 88e78c9 · Browse files · 1 file·+52 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+52 added−39 removed
- other:+52 added−39 removed
Provenance
- Commit
8937500- Tree
d002b58- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
88e78c9
1 file changed+52 added−39 removed
modified:src/options.go+52 added-39 removed
2614 unmodified lines2615 }2616 return nil2617 }2618 2619+func validateOptions(opts *Options) error {2620+ if opts.Pointer != nil {2621+ if err := validateSign(*opts.Pointer, "pointer"); err != nil {2622+ return err2623+ }2624+ }2625+2626+ if opts.Marker != nil {2627+ if err := validateSign(*opts.Marker, "marker"); err != nil {2628+ return err2629+ }2630+ }2631+2632+ if !tui.IsLightRendererSupported() && opts.Height.size > 0 {2633+ return errors.New("--height option is currently not supported on this platform")2634+ }2635+2636+ if opts.Scrollbar != nil {2637+ runes := []rune(*opts.Scrollbar)2638+ if len(runes) > 2 {2639+ return errors.New("--scrollbar should be given one or two characters")2640+ }2641+ for _, r := range runes {2642+ if uniseg.StringWidth(string(r)) != 1 {2643+ return errors.New("scrollbar display width should be 1")2644+ }2645+ }2646+ }2647+2648+ if opts.Height.auto {2649+ for _, s := range []sizeSpec{opts.Margin[0], opts.Margin[2]} {2650+ if s.percent {2651+ return errors.New("adaptive height is not compatible with top/bottom percent margin")2652+ }2653+ }2654+ for _, s := range []sizeSpec{opts.Padding[0], opts.Padding[2]} {2655+ if s.percent {2656+ return errors.New("adaptive height is not compatible with top/bottom percent padding")2657+ }2658+ }2659+ }2660+2661+ return nil2662+}2663+2664 // This function can have side-effects and alter some global states.2665 // So we run it on fzf.Run and not on ParseOptions.2666 func postProcessOptions(opts *Options) error {2667 if opts.Ambidouble {20 unmodified lines2688 }2689 opts.Marker = &defaultMarker2690 }2691 2647- if err := validateSign(*opts.Pointer, "pointer"); err != nil {2648- return err2649- }2650-2651- if err := validateSign(*opts.Marker, "marker"); err != nil {2652- return err2653- }2654-2655- if !tui.IsLightRendererSupported() && opts.Height.size > 0 {2656- return errors.New("--height option is currently not supported on this platform")2657- }2658-2659- if opts.Scrollbar != nil {2660- runes := []rune(*opts.Scrollbar)2661- if len(runes) > 2 {2662- return errors.New("--scrollbar should be given one or two characters")2663- }2664- for _, r := range runes {2665- if uniseg.StringWidth(string(r)) != 1 {2666- return errors.New("scrollbar display width should be 1")2667- }2668- }2669- }2670-2692 // Default actions for CTRL-N / CTRL-P when --history is set2693 if opts.History != nil {2694 if _, prs := opts.Keymap[tui.CtrlP.AsEvent()]; !prs {2695 opts.Keymap[tui.CtrlP.AsEvent()] = toActions(actPrevHistory)38 unmodified lines2734 if _, prs := opts.Keymap[tui.DoubleClick.AsEvent()]; !prs {2735 opts.Keymap[tui.DoubleClick.AsEvent()] = opts.Keymap[tui.CtrlM.AsEvent()]2736 }2737 2717- if opts.Height.auto {2718- for _, s := range []sizeSpec{opts.Margin[0], opts.Margin[2]} {2719- if s.percent {2720- return errors.New("adaptive height is not compatible with top/bottom percent margin")2721- }2722- }2723- for _, s := range []sizeSpec{opts.Padding[0], opts.Padding[2]} {2724- if s.percent {2725- return errors.New("adaptive height is not compatible with top/bottom percent padding")2726- }2727- }2728- }2729-2738 // If we're not using extended search mode, --nth option becomes irrelevant2739 // if it contains the whole range2740 if !opts.Extended || len(opts.Nth) == 1 {2741 for _, r := range opts.Nth {20 unmodified lines2762 theme.Cursor = boldify(theme.Cursor)2763 theme.Spinner = boldify(theme.Spinner)2764 }2765 2766+ if err := opts.initProfiling(); err != nil {2767+ return errors.New("failed to start pprof profiles: " + err.Error())2768+ }2769+2770 return processScheme(opts)2771 }2772 2773 // ParseOptions parses command-line options35 unmodified lines2809 if err := parseOptions(opts, args); err != nil {2810 return nil, err2811 }2812 2801- if err := opts.initProfiling(); err != nil {2802- return nil, errors.New("failed to start pprof profiles: " + err.Error())2813+ // 4. Final validation of merged options2814+ if err := validateOptions(opts); err != nil {2815+ return nil, err2816 }2817 2818 return opts, nil2819 }