textbringer-auto-revert 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 +7 -0
- data/LICENSE.txt +13 -0
- data/README.md +54 -0
- data/Rakefile +12 -0
- data/lib/textbringer/auto-revert/version.rb +7 -0
- data/lib/textbringer/auto-revert.rb +81 -0
- data/lib/textbringer_plugin.rb +3 -0
- data/test/test_helper.rb +88 -0
- data/test/textbringer_auto_revert_test.rb +117 -0
- metadata +69 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4b9c293b790a5a977dad299b00b2fefe137f781e2afe8b139170f9b370d931dd
|
|
4
|
+
data.tar.gz: d430d791ddc748a74ba7c5d9bca93fbf99a5433c7f34400dbb9ee0ea50e53ced
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: eb4563e64fd3d7a3082968d0263a7f4bb79d2b662f77cde07a25454754bdf71cdbf1d8fb85a551664800a1ce656b249954dd178a6d1dd2ed869f9b96a49938a0
|
|
7
|
+
data.tar.gz: f5d6bb33ebf2309d917913ba4cedce9c55c55db1859f93af1cd871e9e5512438183aa9db5caffbc3320df0c833d4445e294158c1f67b15696d033e94f3cc6dc7
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
2
|
+
Version 2, December 2004
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2026 Shinta Koyanagi <yancya@upec.jp>
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
|
7
|
+
copies of this license document, and changing it is allowed as long
|
|
8
|
+
as the name is changed.
|
|
9
|
+
|
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
12
|
+
|
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Textbringer Auto Revert
|
|
2
|
+
|
|
3
|
+
A Textbringer plugin that automatically reverts buffers when files are modified externally.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
gem install textbringer-auto-revert
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or add it to your Gemfile:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle add textbringer-auto-revert
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Commands
|
|
22
|
+
|
|
23
|
+
| Command | Description |
|
|
24
|
+
|---------|-------------|
|
|
25
|
+
| `auto_revert_mode` | Toggle auto-revert mode for the current buffer |
|
|
26
|
+
| `global_auto_revert_mode` | Toggle global auto-revert mode for all buffers |
|
|
27
|
+
|
|
28
|
+
### Example
|
|
29
|
+
|
|
30
|
+
1. Open a file in Textbringer (`C-x C-f`)
|
|
31
|
+
2. Enable global auto-revert mode: `M-x global_auto_revert_mode`
|
|
32
|
+
3. Edit the file externally (e.g., in another terminal)
|
|
33
|
+
4. Execute any command in Textbringer (e.g., `C-n`)
|
|
34
|
+
5. The buffer will automatically revert to the file's contents
|
|
35
|
+
|
|
36
|
+
### Configuration
|
|
37
|
+
|
|
38
|
+
| Key | Default | Description |
|
|
39
|
+
|-----|---------|-------------|
|
|
40
|
+
| `:auto_revert_verbose` | `true` | Show message when buffer is reverted |
|
|
41
|
+
|
|
42
|
+
## Development
|
|
43
|
+
|
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
|
45
|
+
|
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
47
|
+
|
|
48
|
+
## Contributing
|
|
49
|
+
|
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yancya/textbringer-auto-revert.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
The gem is available as open source under the terms of the [WTFPL](http://www.wtfpl.net/).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "auto-revert/version"
|
|
4
|
+
|
|
5
|
+
module Textbringer
|
|
6
|
+
CONFIG[:auto_revert_verbose] = true
|
|
7
|
+
|
|
8
|
+
module AutoRevert
|
|
9
|
+
@global_mode = false
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_accessor :global_mode
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
POST_COMMAND_HOOK = -> {
|
|
16
|
+
return unless AutoRevert.should_check?
|
|
17
|
+
|
|
18
|
+
buffer = Buffer.current
|
|
19
|
+
return unless AutoRevert.buffer_eligible?(buffer)
|
|
20
|
+
return unless buffer.file_modified?
|
|
21
|
+
|
|
22
|
+
# バッファが未変更なら自動revert
|
|
23
|
+
if !buffer.modified?
|
|
24
|
+
buffer.revert
|
|
25
|
+
Textbringer.message("Reverted buffer from file") if CONFIG[:auto_revert_verbose]
|
|
26
|
+
else
|
|
27
|
+
Textbringer.message("Buffer has unsaved changes; file changed on disk")
|
|
28
|
+
end
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
def self.should_check?
|
|
32
|
+
@global_mode || Buffer.current[:auto_revert_mode]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.buffer_eligible?(buffer)
|
|
36
|
+
buffer.file_name && !buffer.name.start_with?("*")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.enable_global
|
|
40
|
+
@global_mode = true
|
|
41
|
+
Textbringer.add_hook(:post_command_hook, POST_COMMAND_HOOK)
|
|
42
|
+
Textbringer.message("Global auto-revert mode enabled")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.disable_global
|
|
46
|
+
@global_mode = false
|
|
47
|
+
# ローカルモードが有効なバッファがなければフック削除
|
|
48
|
+
unless Buffer.list.any? { |b| b[:auto_revert_mode] }
|
|
49
|
+
Textbringer.remove_hook(:post_command_hook, POST_COMMAND_HOOK)
|
|
50
|
+
end
|
|
51
|
+
Textbringer.message("Global auto-revert mode disabled")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
module Commands
|
|
56
|
+
define_command(:auto_revert_mode,
|
|
57
|
+
doc: "Toggle auto-revert mode for the current buffer.") do
|
|
58
|
+
buffer = Buffer.current
|
|
59
|
+
if buffer[:auto_revert_mode]
|
|
60
|
+
buffer[:auto_revert_mode] = false
|
|
61
|
+
Textbringer.message("Auto-revert mode disabled in this buffer")
|
|
62
|
+
else
|
|
63
|
+
buffer[:auto_revert_mode] = true
|
|
64
|
+
# フックがなければ追加
|
|
65
|
+
unless HOOKS[:post_command_hook].include?(AutoRevert::POST_COMMAND_HOOK)
|
|
66
|
+
Textbringer.add_hook(:post_command_hook, AutoRevert::POST_COMMAND_HOOK)
|
|
67
|
+
end
|
|
68
|
+
Textbringer.message("Auto-revert mode enabled in this buffer")
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
define_command(:global_auto_revert_mode,
|
|
73
|
+
doc: "Toggle global auto-revert mode for all buffers.") do
|
|
74
|
+
if AutoRevert.global_mode
|
|
75
|
+
AutoRevert.disable_global
|
|
76
|
+
else
|
|
77
|
+
AutoRevert.enable_global
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
|
|
5
|
+
# Mock Textbringer for testing without the actual dependency
|
|
6
|
+
module Textbringer
|
|
7
|
+
CONFIG = {} unless defined?(CONFIG)
|
|
8
|
+
HOOKS = Hash.new { |h, k| h[k] = [] } unless defined?(HOOKS)
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
attr_accessor :messages
|
|
12
|
+
end
|
|
13
|
+
self.messages = []
|
|
14
|
+
|
|
15
|
+
class Buffer
|
|
16
|
+
@current = nil
|
|
17
|
+
@list = []
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
attr_accessor :current, :list
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_accessor :file_name, :name
|
|
24
|
+
attr_reader :reverted, :properties
|
|
25
|
+
|
|
26
|
+
def initialize(name: nil, file_name: nil)
|
|
27
|
+
@name = name || "test"
|
|
28
|
+
@file_name = file_name
|
|
29
|
+
@modified = false
|
|
30
|
+
@file_modified = false
|
|
31
|
+
@reverted = false
|
|
32
|
+
@properties = {}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def [](key)
|
|
36
|
+
@properties[key]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def []=(key, value)
|
|
40
|
+
@properties[key] = value
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def modified?
|
|
44
|
+
@modified
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def modified=(val)
|
|
48
|
+
@modified = val
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def file_modified?
|
|
52
|
+
@file_modified
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def file_modified=(val)
|
|
56
|
+
@file_modified = val
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def revert
|
|
60
|
+
@reverted = true
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
module_function
|
|
65
|
+
|
|
66
|
+
def add_hook(name, hook)
|
|
67
|
+
HOOKS[name] << hook unless HOOKS[name].include?(hook)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def remove_hook(name, hook)
|
|
71
|
+
HOOKS[name].delete(hook)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def message(msg)
|
|
75
|
+
Textbringer.messages << msg
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
module Commands
|
|
79
|
+
def self.define_command(name, doc: nil, &block)
|
|
80
|
+
define_method(name, &block)
|
|
81
|
+
module_function(name)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
require "textbringer/auto-revert"
|
|
87
|
+
|
|
88
|
+
require "test/unit"
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class Textbringer::AutoRevertTest < Test::Unit::TestCase
|
|
6
|
+
include Textbringer
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
@buffer = Textbringer::Buffer.new(name: "test.txt", file_name: "/tmp/test.txt")
|
|
10
|
+
Textbringer::Buffer.current = @buffer
|
|
11
|
+
Textbringer::Buffer.list = [@buffer]
|
|
12
|
+
Textbringer.messages = []
|
|
13
|
+
Textbringer::HOOKS.clear
|
|
14
|
+
Textbringer::AutoRevert.global_mode = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test "VERSION is defined" do
|
|
18
|
+
assert do
|
|
19
|
+
::Textbringer::AutoRevert.const_defined?(:VERSION)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "global_auto_revert_mode enables global mode" do
|
|
24
|
+
assert_equal false, Textbringer::AutoRevert.global_mode
|
|
25
|
+
|
|
26
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
27
|
+
|
|
28
|
+
assert_equal true, Textbringer::AutoRevert.global_mode
|
|
29
|
+
assert_includes Textbringer::HOOKS[:post_command_hook],
|
|
30
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test "global_auto_revert_mode toggles off" do
|
|
34
|
+
Textbringer::Commands.global_auto_revert_mode # enable
|
|
35
|
+
Textbringer::Commands.global_auto_revert_mode # disable
|
|
36
|
+
|
|
37
|
+
assert_equal false, Textbringer::AutoRevert.global_mode
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test "auto_revert_mode enables local mode for current buffer" do
|
|
41
|
+
assert_nil @buffer[:auto_revert_mode]
|
|
42
|
+
|
|
43
|
+
Textbringer::Commands.auto_revert_mode
|
|
44
|
+
|
|
45
|
+
assert_equal true, @buffer[:auto_revert_mode]
|
|
46
|
+
assert_includes Textbringer::HOOKS[:post_command_hook],
|
|
47
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test "auto_revert_mode toggles off" do
|
|
51
|
+
Textbringer::Commands.auto_revert_mode # enable
|
|
52
|
+
Textbringer::Commands.auto_revert_mode # disable
|
|
53
|
+
|
|
54
|
+
assert_equal false, @buffer[:auto_revert_mode]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test "reverts buffer when file is modified and buffer is not modified" do
|
|
58
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
59
|
+
@buffer.file_modified = true
|
|
60
|
+
@buffer.modified = false
|
|
61
|
+
|
|
62
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
63
|
+
|
|
64
|
+
assert_equal true, @buffer.reverted
|
|
65
|
+
assert_includes Textbringer.messages, "Reverted buffer from file"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
test "does not revert buffer when buffer has unsaved changes" do
|
|
69
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
70
|
+
@buffer.file_modified = true
|
|
71
|
+
@buffer.modified = true
|
|
72
|
+
|
|
73
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
74
|
+
|
|
75
|
+
assert_equal false, @buffer.reverted
|
|
76
|
+
assert_includes Textbringer.messages, "Buffer has unsaved changes; file changed on disk"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test "does not revert when file is not modified" do
|
|
80
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
81
|
+
@buffer.file_modified = false
|
|
82
|
+
@buffer.modified = false
|
|
83
|
+
|
|
84
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
85
|
+
|
|
86
|
+
assert_equal false, @buffer.reverted
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test "does not check when neither global nor local mode is enabled" do
|
|
90
|
+
@buffer.file_modified = true
|
|
91
|
+
@buffer.modified = false
|
|
92
|
+
|
|
93
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
94
|
+
|
|
95
|
+
assert_equal false, @buffer.reverted
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
test "skips special buffers starting with *" do
|
|
99
|
+
@buffer.name = "*scratch*"
|
|
100
|
+
@buffer.file_modified = true
|
|
101
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
102
|
+
|
|
103
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
104
|
+
|
|
105
|
+
assert_equal false, @buffer.reverted
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test "skips buffers without file_name" do
|
|
109
|
+
@buffer.file_name = nil
|
|
110
|
+
@buffer.file_modified = true
|
|
111
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
112
|
+
|
|
113
|
+
Textbringer::AutoRevert::POST_COMMAND_HOOK.call
|
|
114
|
+
|
|
115
|
+
assert_equal false, @buffer.reverted
|
|
116
|
+
end
|
|
117
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: textbringer-auto-revert
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- yancya
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-01-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: textbringer
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.0'
|
|
27
|
+
description: A Textbringer plugin that automatically reverts buffers when files are
|
|
28
|
+
modified externally.
|
|
29
|
+
email:
|
|
30
|
+
- yancya@upec.jp
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- LICENSE.txt
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- lib/textbringer/auto-revert.rb
|
|
39
|
+
- lib/textbringer/auto-revert/version.rb
|
|
40
|
+
- lib/textbringer_plugin.rb
|
|
41
|
+
- test/test_helper.rb
|
|
42
|
+
- test/textbringer_auto_revert_test.rb
|
|
43
|
+
homepage: https://github.com/yancya/textbringer-auto-revert
|
|
44
|
+
licenses:
|
|
45
|
+
- WTFPL
|
|
46
|
+
metadata:
|
|
47
|
+
allowed_push_host: https://rubygems.org
|
|
48
|
+
homepage_uri: https://github.com/yancya/textbringer-auto-revert
|
|
49
|
+
source_code_uri: https://github.com/yancya/textbringer-auto-revert
|
|
50
|
+
post_install_message:
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: 3.2.0
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
requirements: []
|
|
65
|
+
rubygems_version: 3.4.19
|
|
66
|
+
signing_key:
|
|
67
|
+
specification_version: 4
|
|
68
|
+
summary: AutoRevert mode for Textbringer
|
|
69
|
+
test_files: []
|