Class: Uniword::HeadersCLI
- Inherits:
-
Thor
- Object
- Thor
- Uniword::HeadersCLI
- Includes:
- CLIHelpers
- Defined in:
- lib/uniword/cli/headers_cli.rb
Overview
Headers/Footers subcommand for Uniword CLI.
Manages document headers and footers with support for default, first-page, and even-page types.
Instance Method Summary collapse
- #add_footer(path, text) ⇒ Object
- #add_header(path, text) ⇒ Object
- #list(path) ⇒ Object
- #remove(path) ⇒ Object
Methods included from CLIHelpers
Instance Method Details
#add_footer(path, text) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/uniword/cli/headers_cli.rb', line 100 def (path, text) doc = load_document(path) manager = HeadersFooters::Manager.new(doc) manager.(text, type: [:type]) doc.save([:output]) say "Added #{[:type]} footer to #{[:output]}", :green rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e) end |
#add_header(path, text) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/uniword/cli/headers_cli.rb', line 77 def add_header(path, text) doc = load_document(path) manager = HeadersFooters::Manager.new(doc) manager.add_header(text, type: [:type]) doc.save([:output]) say "Added #{[:type]} header to #{[:output]}", :green rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e) end |
#list(path) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/uniword/cli/headers_cli.rb', line 23 def list(path) doc = load_document(path) manager = HeadersFooters::Manager.new(doc) if [:json] require "json" data = { headers: manager.list_headers, footers: manager., } puts JSON.pretty_generate(data) return end headers = manager.list_headers = manager. if headers.empty? && .empty? say "No headers or footers found.", :yellow return end unless headers.empty? say "Headers:", :cyan headers.each do |h| text = h[:empty] ? "(empty)" : truncate(h[:text]) say " [#{h[:type]}] #{text}" end end unless .empty? say "Footers:", :cyan .each do |f| text = f[:empty] ? "(empty)" : truncate(f[:text]) say " [#{f[:type]}] #{text}" end end rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e) end |
#remove(path) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/uniword/cli/headers_cli.rb', line 126 def remove(path) doc = load_document(path) manager = HeadersFooters::Manager.new(doc) case [:what] when "headers" count = manager.remove_headers(type: [:type]) say "Removed #{count} header(s)", :green when "footers" count = manager.(type: [:type]) say "Removed #{count} footer(s)", :green when "all" manager.clear_all say "Removed all headers and footers", :green else say "Invalid --what: #{[:what]}. " \ "Use headers, footers, or all.", :red exit 1 end doc.save([:output]) rescue Uniword::Error => e handle_error(e) rescue StandardError => e handle_error(e) end |