Skip to content

Commit c855a20

Browse files
committed
Allow input from text editor
Uses environment variable EDITOR if available. Defaults to vim.
1 parent e37e0bf commit c855a20

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/dev_journal.cr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "./dev_journal/*"
22

33
module DevJournal
4-
# TODO Put your code here
54
end
65

76
DevJournal::CLI.run

src/dev_journal/cli.cr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "option_parser"
22
require "colorize"
3+
require "tempfile"
34

45
module DevJournal
56
class CLI
@@ -20,7 +21,7 @@ module DevJournal
2021
case
2122
when @search_text then search
2223
when @clear_last_entry then clear_last_entry
23-
when @argv[0]? && @argv[0] == "-" then stdin_input
24+
when @argv[0]? && @argv[0] == "-" then editor_input
2425
when @argv[0]? then argv_input
2526
else show_recent_entries
2627
end
@@ -52,6 +53,21 @@ module DevJournal
5253
end
5354
end
5455

56+
def editor_input
57+
body = [] of String
58+
editor = ENV.fetch("EDITOR", "vim")
59+
60+
tempfile = Tempfile.new("devjournal")
61+
system(editor, [tempfile.path])
62+
63+
if $?.normal_exit?
64+
body = File.read_lines(tempfile.path)
65+
add_entry(body.join("\n"))
66+
end
67+
68+
tempfile.unlink
69+
end
70+
5571
def stdin_input
5672
lines = [] of String
5773

0 commit comments

Comments
 (0)