Skip to content

Commit

Permalink
Add option to place hint labels towards the end of the matched text i…
Browse files Browse the repository at this point in the history
…nstead of the beginning.

One can configure this using the `-tailhint` flag or the `@fastcopy-tail-hint` tmux option.
  • Loading branch information
Jy Yuan committed Dec 3, 2023
1 parent 08d88d4 commit 684466b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (app *app) Run(cfg *config) error {
Text: string(bs),
Alphabet: []rune(cfg.Alphabet),
Matcher: matcher,
TailHint: cfg.TailHint,
}
ctrl.Init()

Expand Down Expand Up @@ -159,6 +160,7 @@ type ctrl struct {
Alphabet []rune
Text string
Matcher matcher
TailHint bool

w *fastcopy.Widget
ui *ui.App
Expand All @@ -182,6 +184,7 @@ func (c *ctrl) Init() {
HintLabelInput: base.Foreground(tcell.ColorYellow),
SelectedMatch: base.Foreground(tcell.ColorYellow),
DeselectLabel: base.Foreground(tcell.ColorDarkRed),
TailHint: c.TailHint,
},
}).Build()

Expand Down
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type config struct {
Regexes regexes
Tmux string
LogFile string
TailHint bool
}

// Generates a new default configuration.
Expand All @@ -104,13 +105,15 @@ func (c *config) RegisterFlags(flag *flag.FlagSet) {
flag.BoolVar(&c.Verbose, "verbose", false, "")
flag.StringVar(&c.LogFile, "log", "", "")
flag.StringVar(&c.Tmux, "tmux", "tmux", "")
flag.BoolVar(&c.TailHint, "tailhint", false, "")
}

func (c *config) RegisterOptions(load *tmuxopt.Loader) {
load.StringVar(&c.Action, "@fastcopy-action")
load.StringVar(&c.ShiftAction, "@fastcopy-shift-action")
load.Var(&c.Alphabet, "@fastcopy-alphabet")
load.MapVar(&c.Regexes, "@fastcopy-regex-")
load.BoolVar(&c.TailHint, "@fastcopy-tail-hint")
}

// FillFrom updates this config object, filling empty values with values from
Expand All @@ -136,6 +139,7 @@ func (c *config) FillFrom(o *config) {
}
c.Regexes.FillFrom(o.Regexes)
c.Verbose = c.Verbose || o.Verbose
c.TailHint = c.TailHint || o.TailHint
}

// Flags rebuilds a list of arguments from which this configuration may be
Expand Down Expand Up @@ -164,5 +168,8 @@ func (c *config) Flags() []string {
if len(c.Tmux) > 0 {
args = append(args, "-tmux", c.Tmux)
}
if c.TailHint {
args = append(args, "-tailhint")
}
return args
}
32 changes: 25 additions & 7 deletions internal/fastcopy/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type AnnotationStyle struct {

// Part of a multi-character label that the user has already typed.
LabelTyped tcell.Style

// Whether to place the hint at the end or beginning of the matched text.
TailHint bool
}

func (h *hint) Annotations(input string, style AnnotationStyle) (anns []ui.TextAnnotation) {
Expand All @@ -101,16 +104,32 @@ func (h *hint) Annotations(input string, style AnnotationStyle) (anns []ui.TextA

for _, match := range h.Matches {
pos := match.Range
offset := pos.Start
if style.TailHint {
offset = pos.End - len(h.Label)
if pos.End-len(h.Label) < pos.Start {
offset = pos.Start
}
}
// Show the label only if there's no input, or if the input
// matches all or part of the label.
if matched {
i := 0

// Highlight matched text before annotations (if any).
if offset-pos.Start > 0 {
anns = append(anns, ui.StyleTextAnnotation{
Offset: pos.Start,
Length: offset - pos.Start,
Style: matchStyle,
})
}

// Highlight the portion of the label already typed by
// the user.
if len(input) > 0 {
anns = append(anns, ui.OverlayTextAnnotation{
Offset: pos.Start,
Offset: offset,
Overlay: input,
Style: style.LabelTyped,
})
Expand All @@ -120,20 +139,19 @@ func (h *hint) Annotations(input string, style AnnotationStyle) (anns []ui.TextA
// Highlight the portion of the label yet to be typed.
if i < len(h.Label) {
anns = append(anns, ui.OverlayTextAnnotation{
Offset: pos.Start + len(input),
Offset: offset + len(input),
Overlay: h.Label[i:],
Style: style.Label,
})
}

pos.Start += len(h.Label)
offset += len(h.Label)
}

// Don't show the rest of the matched text if the label is
// longer than the text.
if pos.End > pos.Start {
// Highlight matched text after annotations (if any).
if pos.End > offset {
anns = append(anns, ui.StyleTextAnnotation{
Offset: pos.Start,
Offset: offset,
Length: pos.End - pos.Start,
Style: matchStyle,
})
Expand Down
4 changes: 4 additions & 0 deletions internal/fastcopy/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Style struct {
// Multi-select mode:
SelectedMatch tcell.Style // one of the selected matches
DeselectLabel tcell.Style // label for deselection

// Whether to place the hint at the end or beginning of the matched text.
TailHint bool
}

// Selection is a choice made by the user in the fastcopy UI.
Expand Down Expand Up @@ -299,6 +302,7 @@ func (w *Widget) annotateText() {
Skipped: w.style.SkippedMatch,
Label: w.style.HintLabel,
LabelTyped: w.style.HintLabelInput,
TailHint: w.style.TailHint,
}

if hint.Selected {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ The following flags are available:
path to tmux executable.
-tmux /usr/bin/tmux
Searches $PATH for tmux by default.
-tailhint
places hints/labels towards the end of the matched text.
Defaults to false (hints are at the beginning of the matched text).
-log FILE
file to write logs to.
Uses stderr by default.
Expand Down

0 comments on commit 684466b

Please sign in to comment.