From 5eecdcb550188ff16039ae49682188af552f4940 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Wed, 11 May 2016 16:52:17 -0400 Subject: [PATCH 01/15] README fixes --- README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ace99c6..3a08ccb 100644 --- a/README.md +++ b/README.md @@ -21,22 +21,28 @@ Upon first initialization, a logfile will be created at `~/log.sqlite3`. ### Display Log Entries - $ log + $ devjournal ### Add a single line entry - $ log This is an example line entry + $ devjournal This is an example line entry ### Add a multiple line entry - $ log - + $ devjournal - + +### Shorter CLI + +Alias `devjournal` to `dj` for less keystrokes. + + $ alias dj='devjournal' ## Development - $ git clone https://github.com/adam12/log - $ cd log + $ git clone https://github.com/adam12/devjournal + $ cd devjournal $ crystal deps - $ crystal run src/log.cr + $ crystal run src/dev_journal.cr ## Possible new features From e572df335041bf1c88e88d1260c1411021f27cbc Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Wed, 25 May 2016 02:33:11 -0400 Subject: [PATCH 02/15] Page output if TTY --- src/dev_journal/cli.cr | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 83b7394..207dbd6 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -38,18 +38,31 @@ module DevJournal end def display_entries(entries) + if STDIN.tty? + pager = ENV.fetch("PAGER", "less") + + Process.run(pager, output: true) do |proc| + display_entries(entries, proc.input) + proc.input.close + end + else + display_entries(entries, STDOUT) + end + end + + def display_entries(entries, output) entries.each do |entry| body = entry[0] type = entry[1] project = entry[2] created_at = entry[3] - puts created_at - puts "Type: #{type}".colorize(:blue) if type - puts "Project: #{project}".colorize(:yellow) if project - puts - puts body - puts + output.puts created_at + output.puts "Type: #{type}".colorize(:blue) if type + output.puts "Project: #{project}".colorize(:yellow) if project + output.puts + output.puts body + output.puts end end From c69ac2efccb0975263865416108a708f32bc1426 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Fri, 27 May 2016 10:43:27 -0400 Subject: [PATCH 03/15] Don't log empty entries --- src/dev_journal/cli.cr | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 207dbd6..8c3cf85 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -75,7 +75,12 @@ module DevJournal if $?.normal_exit? body = File.read(tempfile.path) - add_entry(body) + + if !body.empty? + add_entry(body) + else + STDERR.puts "Skipping empty entry" + end end tempfile.unlink From 433f892060ad98db0df6d4cf85463157568b854d Mon Sep 17 00:00:00 2001 From: Nicholas Bering Date: Sun, 5 Jun 2016 21:02:37 -0400 Subject: [PATCH 04/15] Report invalid commandline options without stack trace. --- src/dev_journal/cli.cr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 8c3cf85..765ac24 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -123,6 +123,8 @@ module DevJournal parser.on("-h", "--help", "Show this help") { abort(parser) } parser.on("-v", "--version", "Show the version") { abort("Log v#{DevJournal::VERSION}") } end + rescue ex + puts ex.message end def self.run From 55ca215846e86310ce677c47d8ac80cb92b3b381 Mon Sep 17 00:00:00 2001 From: Nicholas Bering Date: Sun, 5 Jun 2016 21:22:51 -0400 Subject: [PATCH 05/15] Oops. Don't add the invalid option to the journal. Signed-off-by: Nicholas Bering --- src/dev_journal/cli.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 765ac24..b97908f 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -124,7 +124,7 @@ module DevJournal parser.on("-v", "--version", "Show the version") { abort("Log v#{DevJournal::VERSION}") } end rescue ex - puts ex.message + puts abort(ex.message) end def self.run From e6efc0efc60b0bb4e367e488252a3ff3373c2569 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:42:07 -0400 Subject: [PATCH 06/15] Update dependencies --- shard.lock | 10 +++++++--- shard.yml | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/shard.lock b/shard.lock index eec1330..e55b520 100644 --- a/shard.lock +++ b/shard.lock @@ -1,6 +1,10 @@ -version: 1.0 +version: 2.0 shards: + db: + git: https://github.com/crystal-lang/crystal-db.git + version: 0.13.1 + sqlite3: - github: manastech/crystal-sqlite3 - version: 0.3.0 + git: https://github.com/crystal-lang/crystal-sqlite3.git + version: 0.21.0 diff --git a/shard.yml b/shard.yml index c804dfc..1251808 100644 --- a/shard.yml +++ b/shard.yml @@ -9,4 +9,4 @@ license: MIT dependencies: sqlite3: - github: manastech/crystal-sqlite3 + github: crystal-lang/crystal-sqlite3 From 4deebffb0d15825f4984a2533e60ba625bca9167 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:42:18 -0400 Subject: [PATCH 07/15] Add default target --- shard.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shard.yml b/shard.yml index 1251808..9b54ca4 100644 --- a/shard.yml +++ b/shard.yml @@ -10,3 +10,7 @@ license: MIT dependencies: sqlite3: github: crystal-lang/crystal-sqlite3 + +targets: + app: + main: src/dev_journal.cr From 2d9110ff5ca385f8d367b7fe50f71e1ce1d914c5 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:42:42 -0400 Subject: [PATCH 08/15] Tempfile changes --- src/dev_journal/cli.cr | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index b97908f..ab0aafc 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -1,6 +1,5 @@ require "option_parser" require "colorize" -require "tempfile" module DevJournal class CLI @@ -70,7 +69,7 @@ module DevJournal body = [] of String editor = ENV.fetch("EDITOR", "vim") - tempfile = Tempfile.new("devjournal") + tempfile = File.tempfile("devjournal") system(editor, [tempfile.path]) if $?.normal_exit? @@ -83,7 +82,7 @@ module DevJournal end end - tempfile.unlink + tempfile.delete end def stdin_input From 480493db1f7621215f274e7fdcf9c4867eef2e75 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:43:45 -0400 Subject: [PATCH 09/15] New db abstraction --- src/dev_journal/cli.cr | 10 +++++----- src/dev_journal/logfile.cr | 13 +++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index ab0aafc..025f2ea 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -50,11 +50,11 @@ module DevJournal end def display_entries(entries, output) - entries.each do |entry| - body = entry[0] - type = entry[1] - project = entry[2] - created_at = entry[3] + entries.each do + body = entries.read(String) + type = entries.read(String | Nil) + project = entries.read(String | Nil) + created_at = entries.read(String) output.puts created_at output.puts "Type: #{type}".colorize(:blue) if type diff --git a/src/dev_journal/logfile.cr b/src/dev_journal/logfile.cr index f66d771..9c1fd55 100644 --- a/src/dev_journal/logfile.cr +++ b/src/dev_journal/logfile.cr @@ -2,32 +2,33 @@ require "sqlite3" module DevJournal class Logfile + @db : DB::Database property :db def initialize(@logfile : String) - @db = SQLite3::Database.new(@logfile) + @db = DB.open("sqlite3://" + @logfile) create_entries_table end def add_entry(body, type, project) sql = "INSERT INTO entries(body, type, project) VALUES(?, ?, ?)" - db.execute(sql, body, type, project) + db.exec(sql, body, type, project) end def entries sql = "SELECT body, type, project, created_at FROM ENTRIES ORDER BY created_at DESC" - db.execute(sql) + db.query(sql) end def search_entries(q) sql = "SELECT body, type, project, created_at FROM ENTRIES WHERE body LIKE ? ORDER BY created_at DESC" - db.execute(sql, "%#{q}%") + db.query(sql, "%#{q}%") end def clear_last_entry sql = "DELETE FROM entries WHERE id=(SELECT id FROM entries ORDER BY created_at DESC LIMIT 1) LIMIT 1" - db.execute(sql) + db.exec(sql) end def create_entries_table @@ -41,7 +42,7 @@ module DevJournal ) SQL - db.execute(sql) + db.exec(sql) end end end From 6d90c5a1f798db19b81faa4fd2666ec9788c4c45 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:44:04 -0400 Subject: [PATCH 10/15] Fix path to log file --- src/dev_journal/cli.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 025f2ea..0bd69fb 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -11,7 +11,7 @@ module DevJournal def initialize(@argv : Array(String)) parse_options - filename = File.expand_path("~/log.sqlite3") + filename = File.expand_path("#{ENV["HOME"]}/log.sqlite3") @logfile = Logfile.new(filename) end From a69b2263e0a8814d67318405d4c21b3b92ab3af1 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:44:16 -0400 Subject: [PATCH 11/15] Dont' rescue exceptions on OptionParser --- src/dev_journal/cli.cr | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index 0bd69fb..e49a572 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -122,8 +122,6 @@ module DevJournal parser.on("-h", "--help", "Show this help") { abort(parser) } parser.on("-v", "--version", "Show the version") { abort("Log v#{DevJournal::VERSION}") } end - rescue ex - puts abort(ex.message) end def self.run From f8954eb497ab615f50848ed6fc2c6da61830b682 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 12:44:34 -0400 Subject: [PATCH 12/15] Parse pager output to STDOUT with colors (-R) --- src/dev_journal/cli.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index e49a572..f066fa5 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -40,7 +40,7 @@ module DevJournal if STDIN.tty? pager = ENV.fetch("PAGER", "less") - Process.run(pager, output: true) do |proc| + Process.run(pager, output: STDOUT, env: {"LESS" => "-R"}) do |proc| display_entries(entries, proc.input) proc.input.close end From 83dc4bb47f881f4b48981986288866ed7ae097ab Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 13:00:30 -0400 Subject: [PATCH 13/15] Fix parser syntax --- src/dev_journal/cli.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index f066fa5..d690e06 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -110,9 +110,8 @@ module DevJournal end def parse_options - - OptionParser.parse! do |parser| parser.banner = "Usage: #{$0} [arguments]" + OptionParser.parse do |parser| parser.on("-t TYPE", "--type=TYPE", "The type classification that the log event belongs to. example: work, school etc.") { |t| @type = t } parser.on("-p PROJECT", "--project=PROJECT", "The project that the log event belongs to. This helps group log events together which might belong to the same type or which my not belong to a type at all.") { |p| @project = p } From 199752c314cfe16eef59025e976ceb803b5106f9 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 13:00:36 -0400 Subject: [PATCH 14/15] Fix deprecated $0 variable --- src/dev_journal/cli.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev_journal/cli.cr b/src/dev_journal/cli.cr index d690e06..cc4213d 100644 --- a/src/dev_journal/cli.cr +++ b/src/dev_journal/cli.cr @@ -110,8 +110,8 @@ module DevJournal end def parse_options - parser.banner = "Usage: #{$0} [arguments]" OptionParser.parse do |parser| + parser.banner = "Usage: #{PROGRAM_NAME} [arguments]" parser.on("-t TYPE", "--type=TYPE", "The type classification that the log event belongs to. example: work, school etc.") { |t| @type = t } parser.on("-p PROJECT", "--project=PROJECT", "The project that the log event belongs to. This helps group log events together which might belong to the same type or which my not belong to a type at all.") { |p| @project = p } From d3dc82deabc73b567f0cbdf45045c53ccde92712 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Tue, 21 May 2024 13:01:18 -0400 Subject: [PATCH 15/15] Fix annotation of variable --- src/dev_journal/logfile.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dev_journal/logfile.cr b/src/dev_journal/logfile.cr index 9c1fd55..7ee3517 100644 --- a/src/dev_journal/logfile.cr +++ b/src/dev_journal/logfile.cr @@ -2,8 +2,7 @@ require "sqlite3" module DevJournal class Logfile - @db : DB::Database - property :db + property db : DB::Database def initialize(@logfile : String) @db = DB.open("sqlite3://" + @logfile)