Comments (21)
I've been a bit too busy the past couple of months, but I still intend to take a proper stab at this based on my zsh plugin for peco
at some point in the next few weeks.
Mcfly looks very promising, and I'm very keen to try it out properly within my setup :)
from mcfly.
I have basic Zsh support working now if a few folks could test it out and share their thoughts: #81
from mcfly.
I would really love to see zsh support fro mcfly...
from mcfly.
Oh man, I just installed this and only after did I notice that it doesn't support zsh! I am also looking forward for zsh support!
from mcfly.
I created a similar-ish plugin for ZSH (https://github.com/jimeh/zsh-peco-history) for searching history with peco
. But I'm curious how mcfly would compare.
So I'd be happy to try and help out with ZSH integration.
@cantino since mcfly doesn't quite seem to work like peco or fzf, would you mind writing up some more detailed documentation about how the mcfly
executable itself works, and how you're supposed to interact with it. That would be a great help :)
from mcfly.
Thanks a lot for implementing this @cantino!
I tested it and left a few comments about the zsh implementation in the PR.
from mcfly.
@ericbn Has there been any progress? Is there something I can help?
from mcfly.
Thanks @Linusero! I don't use zsh, but I think that'd be a pretty simple change. I designed it to be as agnostic as possible. I think you'd just need to port mcfly.bash. Interested in sending a PR?
from mcfly.
Sure, I will try once I have some time, not so confident about my programming skills though.
from mcfly.
Thanks! I'm happy to collaborate and answer questions.
from mcfly.
Seems someone is already working on this now: #8.
from mcfly.
peco
looks great! Good to know about.
The mcfly
executable does a few things, listable with mcfly help
. As an integration point, it expects the following:
$HISTFILE
to point to the shell's default history file to be imported (when there's no history in mcfly's sqlite3 DB), cleaned if you use the F2 command in the UI, and appended to if you provide the--append-to-histfile
option when runningmcfly add
.$MCFLY_HISTORY
, the shell history file to read from when adding or searching. This is set to a temporary file bymcfly.bash
.mcfly
uses this file both to save commands to the DB (by taking the last line as the most recent command) and to figure out what you're searching for if you hitctrl-r
after having typed a bit (again by taking the last line after stripping off the leading#mcfly:
.)$PWD
the current directory
Given these environment variables, mcfly.bash
adds lines to the db with: mcfly add --exit \$__last_exit --append-to-histfile
. mcfly
uses the exit status provided on the command line and figures out the command based on the last line of $MCFLY_HISTORY
. It also appends to $HISTFILE
in order to provide backwards compatibility because --append-to-histfile
was provided.
When searching, mcfly.bash
uses the bind
command in mcfly.bash
to comment out the current line being typed and to execute mcfly search
which brings up the search UI, prefilling it with the last line of $MCFLY_HISTORY
, if it contains #mcfly: something
.
Please see mcfly.bash for more comments on this flow.
from mcfly.
I'd like to work on the Rust part of the zsh history parser. I have some questions before actually implement it.
-
Should we rename (and refactor) the bash-related parts, such as
bash_history.rs
andfrom_bash_history
? -
How to detect "zsh mode"? Here're some options:
- Detect by
$SHELL
- Detect by
$HISTFILE
- Heuristics on file content
The "fancy" format is enabled only whenEXTENDED_HISTORY
is set. Also, it's possible to rename.zhistory
. I can't think of a reliable and elegant method.
- Detect by
-
Where to implement the detection code of "zsh mode"? Depending on the mechanism, sometimes it's better to add a flag to "mcfly add" and let the script do the remaining work. Sometimes everything should be done in the Rust part.
from mcfly.
@cantino Thanks for the detailed explanation :)
So mcfly uses it's own separate internal SQLite database, which is added to each time the prompt command runs, and thats the database that's actually searched through, rather than the contents of any history file on disk right?
And it sounds like mcfly uses the $MCFLY_HISTORY
file on disk due to limitations in bash. Please correct me if I'm wrong.
ZSH is a bit more flexible than Bash, and I think you can completely skip having mcfly touching any history files on disk. My peco plugin for example feeds all history into peco via STDIN, which peco then filters, and when you pick an entry inside of peco, all peco itself does is print that to STDOUT, which zsh captures, and puts as the text on your prompt.
Obviously populating the history from scratch on each invocation of ctrl-r would mean the current path is not available. But it should be possible to run a custom command to append to mcfly on each prompt render, without having to worry about mcfly also writing to ZSH's own history file. And any text typed into the prompt when you press ctrl-r is available from ZSH and can be passed to mcfly as a CLI argument, so no need to read it from disk.
Knowing this, I think I'll be able to put together a ZSH plugin for mcfly similar to my zsh-peco-historyf plugin. But a couple of changes might be needed to take full advantage of niceties offered by ZSH:
- Allow passing in the initial search query to the
search
command via the CLI, and maybe add a specific flag for reading it from the$MCLFY_HISTORY
file for use by the bash plugin. - Make
MCFLY_HISTORY
an optional environment variable, as I believe it's not needed for ZSH as we can capture and pass in the initial search query as a CLI argument. - Make
HISTFILE
an optional environment variable, and maybe rename it toBASH_HISTFILE
, so a separateZSH_HISTFILE
env var can be used for importing ZSH history. And only import history if the relevant env var is set.
Also, I have a question:
- What is
MCFLY_SESSION_ID
, and why is it needed?
from mcfly.
@hghwng, I think adding a flag to mcfly add
is the right approach. Let's not be too smart in our detection in the Rust code. The shell script can tell us.
I think we could use a trait to provide different parsers.
from mcfly.
So mcfly uses it's own separate internal SQLite database, which is added to each time the prompt command runs, and thats the database that's actually searched through, rather than the contents of any history file on disk right?
Yup!
And it sounds like mcfly uses the $MCFLY_HISTORY file on disk due to limitations in bash. Please correct me if I'm wrong.
Yes, it's how we figure out the fully escaped text of the most recent command, and also how we figure out what you're searching for if you type some characters before hitting ctrl-r
. This avoids bad things if you type backticks or other active characters in the line you're searching for.
ZSH is a bit more flexible than Bash, and I think you can completely skip having mcfly touching any history files on disk. My peco plugin for example feeds all history into peco via STDIN, which peco then filters, and when you pick an entry inside of peco, all peco itself does is print that to STDOUT, which zsh captures, and puts as the text on your prompt.
Nice. Maybe we can encapsulate the weird Bash handling into a trait.
...
Allow passing in the initial search query to the search command via the CLI, and maybe add a specific flag for reading it from the $MCLFY_HISTORY file for use by the bash plugin.
You can do this now. Both search and add take a --command
option to pass it in. But be careful of escaping shell characters. https://github.com/cantino/mcfly/blob/master/src/settings.rs
What is MCFLY_SESSION_ID, and why is it needed?
This is how we can identify the context of commands in a terminal window so that we're not conflating commands across sessions.
from mcfly.
Hi, I would like to contribute to this idea. I was trying mcfly and just got to know that it does not work with zsh. Can I contribute in any way to this?
from mcfly.
@HarshitKaushik You can fork my branch (or just copy the mcfly.zsh
file from it) and make a new pull request. It seems zsh's history file format is slightly different, I'm not sure what other fixes to the application may be required.
from mcfly.
@cantino, I'm more than happy with contributing with a Zsh script.
I'm confused about the need of the MCFLY_HISTORY
file. Is it required to write all commands to it, even if directly passing them as a parameter to mcfly
? (see function below)
A simple Zsh function that would be executed after each command can be:
_mcfly_precmd() {
local exit_status=${?} # Exit status of last command.
local last_command=${history[$((HISTCMD - 1))]}
mcfly add --exit ${exit_status} ${last_command}
}
autoload -Uz add-zsh-hook && add-zsh-hook precmd _mcfly_precmd
We can read the last command directly from the history
array, so no need to read/write from any file, not even worry about the .zsh_history
file, which Zsh takes care of independently. Another good thing about this approach is that is also nicely handles multi-line commands, which would be more complex to read from a file.
So another question I have is how does mcfly
handle multi-line commands? Like
% print 'Line 1
Line 2'
from mcfly.
@ericbn As long as you can pass the command to mcfly using the --command
option and escape it properly, I don't think mcfly
needs to read from MCFLY_HISTORY
.
from mcfly.
It sounds very interesting and I like to use the ZSH very much. Is there already an update for the project?
Perhaps there are already successes with #8?
from mcfly.
Related Issues (20)
- please update new version to fixed error HOT 1
- [QUESTION] is there anyway to reduce padding from the terminal border on mcfly version >= 0.8.0 ? HOT 1
- Introduce rhis, A Modified Version of McFly HOT 1
- Failed Installation via homebrew HOT 1
- Visibility Issue with colorscheme
- mcfly stopped working (kernel issue) HOT 15
- feature request - CLI option to dump the sqlite3 db HOT 7
- Integrate with Atuin HOT 5
- thread 'main' panicked : database disk image is malformed HOT 2
- thread 'main' panicked at 'McFly error occurs each time I run some command. HOT 2
- Can't search command with '-' arguments in PowerShell
- have sane default colors HOT 1
- mcfly prints arguments when moving files around HOT 2
- Draft releases messed up HOT 4
- zsh-vi-mode conflict
- Allow <TAB> to cycle through options instead of select HOT 3
- Allow ENTER to select the command and put it on commandline
- Handle multiple commands separated by "\n" in Fish shell HOT 2
- F3 key does not work with 'vim' key scheme HOT 1
- Powershell support documentation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mcfly.