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

Open in editor #182

Closed
runar-indico opened this issue Oct 19, 2020 · 23 comments · Fixed by #361
Closed

Open in editor #182

runar-indico opened this issue Oct 19, 2020 · 23 comments · Fixed by #361
Labels
enhancement New feature or request tui TUI mode

Comments

@runar-indico
Copy link

When viewing a file with less, one can press v to open the file in the editor. Adding this feature to glow would be great.

@muesli muesli added the enhancement New feature or request label Oct 19, 2020
@lepasq
Copy link

lepasq commented Nov 23, 2020

Hi! I'd like to work on this issue.

This shouldn't take too long, so I'll try to make a PR within the next days.

@muesli
Copy link
Member

muesli commented Nov 23, 2020

@lepasq Awesome, looking forward to that!

@lepasq
Copy link

lepasq commented Nov 23, 2020

I've worked on the feature for a bit, and so far I encountered one problem:

Executing any editor will crash glow right after exiting the editor.
You can try this yourself by running exec nvim or exec nano in your terminal emulator, and quitting nvim/nano.

I couldn't find a workaround to this issue yet.
A StackOverflow user explained the problem here.

Shall I post my gitdiff in the meantime, such that others can have a look at my code and try to solve it?

Preview Gif
Error message in terminal: Error: read /dev/stdin: resource temporarily unavailable

@meowgorithm
Copy link
Member

@lepasq Yes, please do! You could also post a link to your fork as well if you prefer.

@lepasq
Copy link

lepasq commented Nov 24, 2020

@meowgorithm I just pushed my changes to the pager-edit-key branch of my fork in this commit.

@lepasq
Copy link

lepasq commented Nov 24, 2020

Note that the code won't work on Windows yet, since I'm running the editor by launching its binary in /bin.

@wantyapps
Copy link

You can check the value of the "$EDITOR" variable, and launch it.

@lepasq
Copy link

lepasq commented Nov 25, 2020

You can check the value of the "$EDITOR" variable, and launch it.

Fair point. This is what the method currently looks like:

func findEditor() (string, error) {
	if os.Getenv("EDITOR") != "" {
		return os.Getenv("EDITOR"), nil
	}
	editors := []string{"nvim", "vim", "vi", "nano", "gedit"}
	for _, editor := range editors {
		if _, err := exec.LookPath("/bin/" + editor); err == nil {
			return "/bin/" + editor, nil
		}
	}
	return "", fmt.Errorf("Couldn't find an editor on this system.")
}

Do we want to remove the second part, in which we check if the user has an editor inside /bin, in case that the user did not set the environment variable?
In that case, we would just rely on the $EDITOR environment variable, and either ignore the v key action or return some error feedback, if $EDITOR is undefined.

@wantyapps
Copy link

Hi! I want to make some changes in the code you sent earlier. Did you push the code to charmbracelet/glow, or is it still in your fork?

@wantyapps
Copy link

Could not fork a fork. Lol! The change I wanted to make was to add the values ["code", "subl"] to the "editors" array. Code is for VSCode, and Subl is the command for Sublime Text.

@shitchell
Copy link
Contributor

shitchell commented Dec 1, 2020

Fair point. This is what the method currently looks like:

func findEditor() (string, error) {
	if os.Getenv("EDITOR") != "" {
		return os.Getenv("EDITOR"), nil
	}
	editors := []string{"nvim", "vim", "vi", "nano", "gedit"}
	for _, editor := range editors {
		if _, err := exec.LookPath("/bin/" + editor); err == nil {
			return "/bin/" + editor, nil
		}
	}
	return "", fmt.Errorf("Couldn't find an editor on this system.")
}

Do we want to remove the second part, in which we check if the user has an editor inside /bin, in case that the user did not set the environment variable?
In that case, we would just rely on the $EDITOR environment variable, and either ignore the v key action or return some error feedback, if $EDITOR is undefined.

Looking only in /bin/ is not a very great way of looking for executables. There are a billion other folders where those binaries might live, namely the directories in the user's $PATH. And since LookPath already checks in the user's $PATH, I'm really not sure why you're adding "/bin/" to the name. I would try something more like this:

// Returns an editor name, editor path, or error
func getEditor() (string, string, error) {
        var editor_name string
        var editor_path string
        var editor_err error

        editors := []string{"micro", "nano", "nvim", "vim", "vi", "gedit"}

        // If $EDITOR is set, prepend it to the list of editors we'll search for
        if os.Getenv("EDITOR") != "" {
                editors = append([]string{os.Getenv("EDITOR")}, editors...)
        }

        // By default, the error should be that no command has been found
        editor_err = fmt.Errorf("No editor found\n")

        // Search for the editors, stopping after the first one is found
        for i := 0; i < len(editors) && editor_path == ""; i++ {
                editor_name = editors[i]

                // Look for the editor in $PATH
                path, err := exec.LookPath(editor_name);

                if err == nil {
                        // If it was found, store the path (exiting the loop)...
                        editor_path = path
                        // ...and set the error to nil
                        editor_err = nil
                } else {
                        // Delete the name so that none is returned if we don't
                        // find an editor
                        editor_name = ""
                }                          
        }

        return editor_name, editor_path, editor_err
}

Just because the user has $EDITOR set doesn't mean it's installed. We still have to search for it, just like the other predefined editors. So we just prepend it to our editors array to give it priority. Then we loop over that array while i < len(editors) and editor_path == "", that is, until we run out of editors to look for or we find an editor, whichever comes first. Then we spit back the editor path.

The reason we set up our loop that way is because it's typically best to (1) avoid break statements and (2) have only 1 return statement in your code. Forces you to keep your logic much better organized.

shitchell added a commit to shitchell/glow that referenced this issue Dec 3, 2020
Added an option to edit the opened document when the user types 'e'. Searches
$PATH first for the command specified by $EDITOR and then for a set of
pre-defined command line text editors. Addresses:
charmbracelet#182
@shitchell
Copy link
Contributor

shitchell commented Dec 3, 2020

I went ahead and added the option :D Just waiting on them to review / approve the PR
asciicast

@shitchell
Copy link
Contributor

Could not fork a fork. Lol! The change I wanted to make was to add the values ["code", "subl"] to the "editors" array. Code is for VSCode, and Subl is the command for Sublime Text.

What priority would/should code and subl get? I would err towards putting them at the end of the list, but then I'd wager they'd never get used. I didn't read your comment before making the PR, and I'm more than happy to see those editors added to the list, but it's probably preferable to just set $EDITOR to one of those. If you sometimes want to use, say, vim and sometimes want to use subl, create an alias like alias subglow="EDITOR=subl glow"

@wantyapps
Copy link

OK! When they approve the PR, I will update glow, and try it!

BTW: Thank you for responding to my comments. I am programming now for 1 year and love to help.
and, just saying, I am under 14 years old.
Good luck with the PR!

@shitchell
Copy link
Contributor

No problem :) If you're not familiar with how to set the EDITOR variable, you put something like this in your .bashrc file:

export EDITOR="subl"

Then, whenever you use something like glow or any command that uses a text editor, it should default to using Sublime (or whatever you set for your editor).

@shitchell
Copy link
Contributor

shitchell commented Dec 20, 2020

Well if anyone wants to use this, you can clone: https://github.com/shitchell/glow/tree/editor

The guys in charge said (1) they're pretty busy and probably won't be able to review it for a while and (2) an update coming next week will probably conflict with this :P So I'm probably going to keep using this branch for a bit and then re-work it after they roll out this update. They also said they might look at re-working it to help integrate it with their update? So no idea when it'll be available haha. But it's way more useful than I imagined, so I just thought I'd drop this comment here.

@muesli muesli added the tui TUI mode label Feb 1, 2021
@a-marionette
Copy link

Any update on this? Would be way more practical to use Glow if I could create new documents via Glow and edit them as well.

@shitchell
Copy link
Contributor

Any update on this? Would be way more practical to use Glow if I could create new documents via Glow and edit them as well.

I've got a version that was working December of last year. But then they rolled out an update that they said would break it. Honestly, I haven't even checked if their update conflicts with the code I wrote; this is my last semester of college, and I've been focused on that.

Buuut the semester ends in a week, so I'll take a look at this again afterwards! In the mean time, you can feel free to clone/build the branch I created. You'll basically roll back to the version from December 2020, but I find this feature far more useful than anything included in their most recent update :3

@meowgorithm meowgorithm changed the title Feature-request: Press v to edit Open in editor Jan 19, 2022
@meowgorithm
Copy link
Member

Hey all. Just a note that we're planning on implementing this one, however to do it properly we'll most likely make some changes upstream in Bubble Tea (see charmbracelet/bubbletea#171).

The feature itself will most likely be implemented in Glow's beta branch.

@gimbling-away
Copy link

Bump

@managedkaos
Copy link

managedkaos commented Mar 17, 2022

I'm looking forward to this one!

I edit a lot of Markdown..... a loooooooot of Markdown 😩 😂

I'm loving the interaction with Glow on the CLI for viewing nested folders with Markdown files. Honestly, Glow is amazing for that! 🤩

However, when I'm viewing files, I often have the need to drop into the file for quick edit. Right now I can't do that in Glow so I keep notes of the edits I want to make and get to them outside of my Glow session. Would be cool to keep it all in one session and flow.

Also, if Glow had editing, I might just use it as a full blown Markdown IDE! 😎

Thanks to everyone working on this feature. Just wanted you to know that it will really mean a lot to the way that I work so I am thanking you all in advance. 🏆 🙏🏿

@decentral1se
Copy link

Nice! As an addition, it'd be nice to see an editable: false config option to stop folks editing files. In the case of read-only files.

@ryuheechul
Copy link

what an exciting future edition!
I would also vote for key e instead of v or both or ability to set which key to open editor via config or a flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tui TUI mode
Projects
None yet
Development

Successfully merging a pull request may close this issue.