Class: Uniword::HeadersCLI

Inherits:
Thor
  • Object
show all
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

Methods included from CLIHelpers

included

Instance Method Details



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/uniword/cli/headers_cli.rb', line 100

def add_footer(path, text)
  doc = load_document(path)
  manager = HeadersFooters::Manager.new(doc)

  manager.add_footer(text, type: options[:type])
  doc.save(options[:output])
  say "Added #{options[:type]} footer to #{options[: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: options[:type])
  doc.save(options[:output])
  say "Added #{options[:type]} header to #{options[: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 options[:json]
    require "json"
    data = {
      headers: manager.list_headers,
      footers: manager.list_footers,
    }
    puts JSON.pretty_generate(data)
    return
  end

  headers = manager.list_headers
  footers = manager.list_footers

  if headers.empty? && footers.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 footers.empty?
    say "Footers:", :cyan
    footers.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 options[:what]
  when "headers"
    count = manager.remove_headers(type: options[:type])
    say "Removed #{count} header(s)", :green
  when "footers"
    count = manager.remove_footers(type: options[:type])
    say "Removed #{count} footer(s)", :green
  when "all"
    manager.clear_all
    say "Removed all headers and footers", :green
  else
    say "Invalid --what: #{options[:what]}. " \
        "Use headers, footers, or all.", :red
    exit 1
  end

  doc.save(options[:output])
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end