Skip to content

Instantly share code, notes, and snippets.

@Willamin
Created September 11, 2020 14:44
Show Gist options
  • Save Willamin/b1769a239bd8a912989abb16099e5eae to your computer and use it in GitHub Desktop.
Save Willamin/b1769a239bd8a912989abb16099e5eae to your computer and use it in GitHub Desktop.
#!/usr/bin/env cashrun
# usage:
# $ monotonic foo
# > 0
# $ monotonic foo
# > 1
# $ monotonic foo
# > 2
# $ monotonic bar
# > 0
# $ monotonic foo
# > 3
# $ monotonic bar
# > 1
the_path : Path? = nil
Path["~/.config/monotonic-counter/"]
.expand(home: true)
.tap { |path| the_path = path }
.try { |path| Dir.mkdir_p(path) }
begin
the_path = the_path.not_nil!
key = ARGV[0]? || ""
this_counter_file = the_path.join(key)
value = 0
if File.exists?(this_counter_file) && File.readable?(this_counter_file)
value = File.read(this_counter_file).to_i32? || 0
end
new_value = value += 1
File.write(this_counter_file, new_value)
STDOUT.puts(value)
rescue
STDERR.puts("a problem occured")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment