daily-txt 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33ded10f2b06f900d21fca1e8d500d80d7e79894
4
+ data.tar.gz: 550fa1835ab38eefc6904ab67fcd4b41fc9c5948
5
+ SHA512:
6
+ metadata.gz: 599e369bd7be6565978cedce39e04b0782f69ce7e16f0d32800dffd95ffa3434419bff5e98d3c58f1b514d034ceb94c471f67205bc54766862f71ec90f77102e
7
+ data.tar.gz: 08b8f68578f24f1be5f4a844701c657851c869b972590cde59a96e2ad6e8f5fb6235cb0df22e86fe03acc52f1f5117ad560f5c6c3b6ef3f9c6001e33208a0ac1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.swp
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at bonar@me.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in daily-md.gemspec
4
+ gemspec
5
+
6
+ gem "file-visitor", ">= 0.2.0"
7
+ gem "term-ansicolor"
8
+ gem "json", ">= 1.8.0"
9
+ gem "systemu"
10
+ gem 'which', '~> 0.0.2'
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 bonar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Daily::Txt
2
+
3
+ daily-txt is command line script for creating and listing text files that has a filename with current date.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'daily-txt'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install daily-txt
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ $ daily-txt
25
+ ```
26
+
27
+ A command above creates text file at /${ROOT}/text/{$YYYY}/{$MM}/${YYYY}_${MM}_${DD}.txt
28
+ and open it with default editor. If it already exists, just open it for appending.
29
+
30
+ ### Initialize
31
+
32
+ daily-txt command asks ROOT directory and default editor at initial startup, and stores
33
+ setting to ~/.daily-txt.rc.
34
+
35
+ ```
36
+ $ daily-txt
37
+
38
+ Document Root? (/Document/daily-txt/):
39
+ Default Editor? (/usr/bin/vim):
40
+
41
+ Setting stored to ~/.daily-txt.rc
42
+ ```
43
+
44
+ ### Listing
45
+
46
+ List text files
47
+
48
+ ```
49
+ $ daily-txt -l
50
+
51
+ /your/root/text/2016/04/2016_04_23.txt
52
+ /your/root/text/2016/04/2016_04_24.txt
53
+ /your/root/text/2016/04/2016_05_04.txt
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/daily-txt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "daily/txt"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/daily-txt ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'daily/txt'
4
+ Daily::Txt::CLI.bootstrap(ARGV)
5
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/daily-txt.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'daily/txt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "daily-txt"
8
+ spec.version = Daily::Txt::VERSION
9
+ spec.authors = ["bonar"]
10
+ spec.email = ["bonar@me.com"]
11
+
12
+ spec.summary = %q{command line tool for daily text file memo}
13
+ spec.description = %q{daily-txt is command line script for creating and listing text files wth a current date filename.}
14
+ spec.homepage = "http://bonar.jp/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = ["daily-txt"]
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,117 @@
1
+ require "daily/txt/config"
2
+ require "daily/txt/path_builder"
3
+ require 'date'
4
+ require 'optparse'
5
+ require 'file/visitor'
6
+
7
+ class Daily::Txt::CLI
8
+
9
+ class << self
10
+
11
+ def parse_option(argv)
12
+ option = {}
13
+ parser = OptionParser.new
14
+
15
+ parser.on('-f PATH', 'config file path') do |path|
16
+ option[:config_path] = path
17
+ end
18
+ parser.on('-l', '--list', 'list text files') do |path|
19
+ option[:mode] = :list
20
+ end
21
+ parser.on('--ascending', 'make list order ascending') do
22
+ option[:asc] = true
23
+ end
24
+
25
+ begin
26
+ parser.parse!(argv)
27
+ rescue OptionParser::MissingArgument => e
28
+ abort(e.to_s)
29
+ end
30
+ option
31
+ end
32
+
33
+ def bootstrap(argv)
34
+ begin
35
+ option = parse_option(argv)
36
+ rescue OptionParser::InvalidOption => e
37
+ abort(e.to_s)
38
+ end
39
+ begin
40
+ config_path = option[:config_path]
41
+ config = Daily::Txt::Config.load(config_path)
42
+ rescue Daily::Txt::Config::NotFound => e
43
+ if config_path
44
+ abort(e.to_s)
45
+ end
46
+ config = Daily::Txt::Config.create_default
47
+ end
48
+
49
+ Daily::Txt::PathBuilder.prepare_basedir(config["home"])
50
+
51
+ dispatch_action(option, config)
52
+ end
53
+
54
+ def dispatch_action(option, config)
55
+ case option[:mode]
56
+ when :list
57
+ list(config, option)
58
+ else
59
+ open_today(config)
60
+ end
61
+ end
62
+
63
+ def list(config, option)
64
+ visitor = File::Visitor.new
65
+ visitor.add_filter(:ext, Daily::Txt::PathBuilder::DEFAULT_EXT)
66
+
67
+ if option[:asc]
68
+ visitor.set_direction(:asc)
69
+ else
70
+ visitor.set_direction(:desc)
71
+ end
72
+
73
+ visitor.visit(config["home"]) do |path|
74
+ puts path
75
+ end
76
+ end
77
+
78
+ def open_today(config)
79
+ path = Daily::Txt::PathBuilder.by_date(
80
+ config["home"], Date.today)
81
+ Daily::Txt::PathBuilder.prepare_basedir(path)
82
+ Daily::Txt::System.exec(config["editor"], path)
83
+ end
84
+
85
+ end
86
+
87
+ <<MEMO
88
+
89
+ begin
90
+ config = Daily::Txt::Confing.load
91
+ rescue Daily::Txt::NotInitializedError
92
+ self.initialize
93
+ exit
94
+ end
95
+
96
+ begin
97
+ manager = Daily::Txt::DirectoryManager.new(config)
98
+ rescue Daily::Txt::ScanError
99
+ end
100
+
101
+ manager.get_current_file
102
+ Daily::Txt::System.open(
103
+
104
+ manager.get_files(count: 10).each do |result|
105
+ result.year
106
+ result.month
107
+ result.path
108
+ end
109
+
110
+ results = manager.search(regexp)
111
+
112
+ printer = Daily::Txt::Confing::Printer(config)
113
+ printer.print_files(manager.get_files)
114
+
115
+ MEMO
116
+
117
+ end
@@ -0,0 +1,83 @@
1
+ require 'daily/txt/system'
2
+ require 'json'
3
+
4
+ class Daily::Txt::Config
5
+
6
+ class NotFound < StandardError; end
7
+ class ParseError < StandardError; end
8
+ class Invalid < StandardError; end
9
+
10
+ DEFAULT_CONFIG_PATH = "#{Dir.home}/.daily_txt_config.json"
11
+ DEFAULT_ROOT_DIR = "daily_txt"
12
+ REQUIRED_KEYS = [
13
+ "home",
14
+ "editor",
15
+ "color"
16
+ ]
17
+
18
+ class << self
19
+
20
+ def select_from_candidates(label, candidates)
21
+ index = 1
22
+ candidates.each do |candidate|
23
+ puts "[#{index}] #{candidate}"
24
+ index = index + 1
25
+ end
26
+ input = Daily::Txt::System.wait_input(label)
27
+ selected_index = input.to_i - 1
28
+ if selected_index >= 0 && candidates[selected_index]
29
+ return candidates[selected_index]
30
+ end
31
+ abort("invalid selection: #{input}")
32
+ end
33
+
34
+ def create_default
35
+ editors = Daily::Txt::System.find_editor
36
+ if editors.empty?
37
+ abort('No editor found')
38
+ end
39
+
40
+ selected_editor = editors.size == 1 ? editors.first : nil
41
+ unless selected_editor
42
+ selected_editor = select_from_candidates(
43
+ "> Which editor do you use? : ", editors)
44
+ end
45
+
46
+ config = {
47
+ "home" => File.join(Dir.home, DEFAULT_ROOT_DIR),
48
+ "editor" => selected_editor,
49
+ "color" => true
50
+ }
51
+ File.write(DEFAULT_CONFIG_PATH,
52
+ JSON.pretty_generate(config))
53
+ self.load(DEFAULT_CONFIG_PATH)
54
+ end
55
+
56
+ def load(path)
57
+ path ||= DEFAULT_CONFIG_PATH
58
+
59
+ unless File.readable?(path)
60
+ raise NotFound, "file not found: #{path}"
61
+ end
62
+
63
+ begin
64
+ config = JSON.parse(IO.read(path))
65
+ rescue JSON::ParserError => e
66
+ raise ParseError, "cannot parse: #{path}"
67
+ end
68
+ validate(config)
69
+ config
70
+ end
71
+
72
+ def validate(config)
73
+ REQUIRED_KEYS.each do |key|
74
+ unless config.has_key?(key)
75
+ raise Invalid, "config has not required key: #{key}"
76
+ end
77
+ end
78
+ true;
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,32 @@
1
+ require 'date'
2
+ require 'fileutils'
3
+
4
+ class Daily::Txt::PathBuilder
5
+
6
+ DIR_TEXT = "text"
7
+ DEFAULT_EXT = "txt"
8
+
9
+ class << self
10
+
11
+ def filename(date)
12
+ "%04d_%02d_%02d.%s" % [date.year, date.month, date.day, DEFAULT_EXT]
13
+ end
14
+
15
+ def prepare_basedir(path)
16
+ dir = File.dirname(path)
17
+ unless Dir.exist?(dir)
18
+ FileUtils.mkdir_p(dir)
19
+ end
20
+ end
21
+
22
+ def by_date(root, date)
23
+ File.join(root, DIR_TEXT,
24
+ "%04d" % date.year,
25
+ "%02d" % date.month,
26
+ filename(date))
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,36 @@
1
+ require 'systemu'
2
+ require 'which'
3
+
4
+ class Daily::Txt::System
5
+
6
+ EDITOR_CANDIDATES = [
7
+ "vim",
8
+ "vi",
9
+ "emacs"
10
+ ]
11
+
12
+ class << self
13
+
14
+ def wait_input(question)
15
+ print question
16
+ return gets.chomp
17
+ end
18
+
19
+ def find_editor
20
+ editors = []
21
+ EDITOR_CANDIDATES.each do |candidate|
22
+ found = Which.which(candidate)
23
+ if found && !found.empty?
24
+ editors = editors + found
25
+ end
26
+ end
27
+ editors
28
+ end
29
+
30
+ def exec(command_path, arg)
31
+ system(command_path, arg)
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,5 @@
1
+ module Daily
2
+ module Txt
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/daily/txt.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Daily
2
+ module Txt
3
+ end
4
+ end
5
+
6
+ require "daily/txt/version"
7
+ require "daily/txt/cli"
8
+
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daily-txt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bonar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: daily-txt is command line script for creating and listing text files
56
+ wth a current date filename.
57
+ email:
58
+ - bonar@me.com
59
+ executables:
60
+ - daily-txt
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/daily-txt
74
+ - bin/setup
75
+ - daily-txt.gemspec
76
+ - lib/daily/txt.rb
77
+ - lib/daily/txt/cli.rb
78
+ - lib/daily/txt/config.rb
79
+ - lib/daily/txt/path_builder.rb
80
+ - lib/daily/txt/system.rb
81
+ - lib/daily/txt/version.rb
82
+ homepage: http://bonar.jp/
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.6.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: command line tool for daily text file memo
106
+ test_files: []