Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLI: implement something like git commit (open a text editor and get value)

I am making a command line app. I want to allow the user, when they are adding values for things in the terminal, to be able to add the value with their text editor. How does git achieve this?

I want to open their default editor, allow them to enter text, and when they save get the value and use that value for my app.

Thanks.

like image 687
Jonovono Avatar asked Oct 18 '13 17:10

Jonovono


1 Answers

Typically, you do four things:

  1. Create a temporary file
  2. Fork an external process which execs the program specified in the environment variable $EDITOR, giving the name of the temporary file as an argument.
  3. Wait for the process to return.
  4. Open and read from the temporary file to see what the user wrote.
like image 58
chepner Avatar answered Oct 26 '22 06:10

chepner