Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query server command to expose internal state #1384

Merged
merged 21 commits into from
Sep 8, 2023
Merged

Add query server command to expose internal state #1384

merged 21 commits into from
Sep 8, 2023

Conversation

joelim-work
Copy link
Collaborator

@joelim-work joelim-work commented Aug 13, 2023

Description

Based on discussion starting from #1310 (comment).

Support for querying the command history is added. This is an improvement over reading the history file as it includes commands from the current session as well as the persisted history from previous sessions.

In addition, I have decided to remove the existing maps/cmaps/cmds/jumps commands, so that they are no longer provided by default. Instead these can be configured manually according to the user's preference. maps, cmaps and cmds will remain provided by default for convenience and instructive purposes, but jumps will be removed. This is a breaking change.

Examples

Create maps command to display mappings:

cmd maps $lf -remote "query $id maps" | "$PAGER"

Display both maps and cmaps in a single command:

cmd maps ${{
    {
        lf -remote "query $id maps"
        printf '\n'
        lf -remote "query $id cmaps"
    } | "$PAGER"
}}

Use awk to add color:

cmd maps color-pager maps
cmd cmaps color-pager cmaps
cmd cmds color-pager cmds
cmd jumps color-pager jumps
cmd history color-pager history

cmd color-pager ${{
AWK_SCRIPT=$(cat << "EOF"
BEGIN {
    FS = "\t"
    OFS = "\t"
}
NR == 1 {
    print "\033[35m" $0
}
NR > 1 {
    sub(/^> /, "\033[33m> \033[34m", $1)
    $1 = "\033[34m" $1
    $NF = "\033[32m" $NF
    print $0
}
EOF
)

lf -remote "query $id $1" | awk "$AWK_SCRIPT" | "$PAGER"
}}

Select and execute a previous command:

map <a-h> ${{
    clear
    cmd=$(
        lf -remote "query $id history" |
        awk -F'\t' 'NR > 1 { print $NF}' |
        sort -u |
        fzf --reverse --prompt='Execute command: '
    )
    lf -remote "send $id $cmd"
}}

Change location based on the jump list:

map <a-f> ${{
    clear
    dir=$(
        lf -remote "query $id jumps" |
        awk -F'\t' 'NR > 1 { print $NF}' |
        sort -u |
        fzf --reverse --prompt='Jump to location: '
    )
    if [ -d "$dir" ]; then
        lf -remote "send $id cd \"$dir\""
    fi
}}

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 13, 2023

Thank you, I haven't had time to look at lf lately, but this seems very nice!

I only looked at the PR superficially1, but I'll once again make the point that, I think, :maps and :cmds are nice to have and friendly to beginners. I think re-implementing them as cmd maps $lf -remote "recv $id maps" | "$PAGER" would be a definite improvement over the status-quo. I'd build id cmds and maps defined like that, similarly to how doc is built in. We should absolutely also put the fancier (e.g. colored) versions of them in the wiki or even the sample lfrc for people who'd like that.

Another reason I like pre-defining such commands is to pique users' curiosity: when they use :cmds and see how :cmds itself is implemented, they'll learn something. They might become motivated to come up with fancier versions or to look at the wiki, or not, but this discovery is a gift we can give them.

I don't feel as strongly about cmaps and jumps; I think the users who want those are fewer and can define them for themselves.

That said, you have by now thought about this far deeper than I have, so I leave the final decision up to you. Thank you again for working through this mess of a problem!

Footnotes

  1. Time-permitting, I might look at it more carefully, it seems quite interesting. However, there's no need to wait for that to maybe happen before merging this.

@joelim-work
Copy link
Collaborator Author

@ilyagr That's OK, I'm happy to wait a week or however long you want before merging this. I don't think this feature is urgent or anything.

I guess I'm fine with providing maps and cmds by default, and it should be sufficient for users to work out how to map the others if they want. Thanks for the suggestion.

@MahouShoujoMivutilde
Copy link
Contributor

MahouShoujoMivutilde commented Aug 23, 2023

Is this a base for how #1084 will be implemented, too?

@joelim-work
Copy link
Collaborator Author

Is this a base for how #1084 will be implemented, too?

Should be possible, something like lf -remote "recv $id files", not 100% sure on the name files though.

@MahouShoujoMivutilde
Copy link
Contributor

not 100% sure on the name files though

Seems intuitive enough to me.

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 28, 2023

Perhaps we should rename recv? I think something like lf -remote "show $id maps" or lf -remote "info $id maps" might be clearer than lf -remote "recv $id maps".

Copy link
Collaborator

@ilyagr ilyagr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long (I guess I did warn you), and thank you for waiting. I had some minor questions, I don't think any of them are blocking.

The only thing I feel stronger about is that it'd be nice to rename recv to something clearer (see my other comment), or at least discuss alternative names. recv is OK, but once we merge it, we'll be stuck with it for a long time.

Thank you again for working on this!

client.go Show resolved Hide resolved
client.go Show resolved Hide resolved
doc.go Outdated Show resolved Hide resolved
server.go Show resolved Hide resolved
client.go Show resolved Hide resolved
client.go Show resolved Hide resolved
os_windows.go Outdated Show resolved Hide resolved
client.go Outdated Show resolved Hide resolved
@joelim-work joelim-work changed the title Add recv server command to expose internal state Add query server command to expose internal state Aug 29, 2023
server.go Show resolved Hide resolved
@joelim-work
Copy link
Collaborator Author

After some thinking I have decided to additionally provide a default command for cmaps in addition to maps and cmds for the following reasons:

  • cmaps is in a way analogous to maps, and it somehow feels wrong to provide a default command for maps but not for cmaps.
  • cmaps are rarely customized compared to maps, which means users are more likely to forget them and adding a default command will serve as a useful reference.
  • Prior to this change, cmaps was already provided by default, so this is one less breaking change to worry about.

Regarding the jump list and the history, I do consider their usage to be more niche compared to displaying configurations like mappings/commands, so I will leave them out.

@joelim-work joelim-work mentioned this pull request Sep 3, 2023
@joelim-work joelim-work merged commit 9068801 into gokcehan:master Sep 8, 2023
@joelim-work joelim-work deleted the remote-recv branch September 8, 2023 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants