Class: Uniword::ThemeCLI

Inherits:
Thor
  • Object
show all
Includes:
CLIHelpers
Defined in:
lib/uniword/cli/theme_cli.rb

Overview

Theme subcommands for Uniword CLI.

Manages bundled and imported themes – colors, fonts, and visual styling that control document appearance.

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#apply(input_path, output_path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/uniword/cli/theme_cli.rb', line 110

def apply(input_path, output_path)
  unless options[:name] || options[:file]
    say "Error: Must specify either --name for bundled theme or --file for .thmx file",
        :red
    exit 1
  end

  if options[:name] && options[:file]
    say "Error: Cannot specify both --name and --file", :red
    exit 1
  end

  say "Loading document #{input_path}...", :green if options[:verbose]

  doc = load_document(input_path)

  if options[:verbose]
    say "  Loaded document:", :cyan
    say "    Paragraphs: #{doc.paragraphs.count}"
    say "    Current theme: #{doc.theme&.name || 'None'}"
  end

  apply_theme_to(doc)

  if options[:verbose]
    say "  Applied theme:", :cyan
    say "    Theme name: #{doc.theme.name}"
    say "    Colors: #{doc.theme.color_scheme.colors.keys.join(', ')}"
    say "    Major font: #{doc.theme.major_font}"
    say "    Minor font: #{doc.theme.minor_font}"
    say "    Available variants: #{doc.theme.variants.keys.join(', ')}" if doc.theme.variants.any?
  end

  doc.save(output_path)
  say "Theme applied successfully to #{output_path}", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#auto(input_path, output_path) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/uniword/cli/theme_cli.rb', line 162

def auto(input_path, output_path)
  say "Loading document #{input_path}...", :green if options[:verbose]

  doc = load_document(input_path)

  if options[:verbose]
    say "  Loaded document:", :cyan
    say "    Current theme: #{doc.theme&.name || 'None'}"
  end

  result = doc.auto_transition_theme

  if result
    say "Transitioned MS '#{result.ms_name}' to Uniword '#{result.uniword_slug}'",
        :green
    if options[:verbose]
      friendly = Themes::Theme.load(result.uniword_slug)
      say "  Uniword theme:", :cyan
      say "    Name: #{friendly.name}"
      say "    Major font: #{friendly.font_scheme.major_font}"
      say "    Minor font: #{friendly.font_scheme.minor_font}"
    end
  else
    say "No matching Uniword theme found for this document's theme", :yellow
  end

  doc.save(output_path)
  say "Saved to #{output_path}", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#importObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uniword/cli/theme_cli.rb', line 65

def import
  if options[:verbose]
    say "Importing themes from #{options[:source]}...",
        :green
  end

  importer = Themes::ThemeImporter.new
  count = importer.import_all(options[:source], options[:output])

  say "Successfully imported #{count} themes to #{options[:output]}/",
      :green

  if options[:verbose]
    themes = Dir.glob(File.join(options[:output], "*.yml")).map do |f|
      File.basename(f, ".yml")
    end.sort
    say "\nAvailable themes:", :cyan
    themes.each { |name| say "  - #{name}" }
  end
rescue StandardError => e
  say "Error importing themes: #{e.message}", :red
  exit 1
end

#listObject



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
# File 'lib/uniword/cli/theme_cli.rb', line 24

def list
  themes = Themes::Theme.available_themes

  if themes.empty?
    say "No bundled themes found.", :yellow
    say "Run 'uniword theme import' to import Office themes.", :yellow
    return
  end

  say "Available themes (#{themes.count}):", :green
  themes.each do |theme_name|
    if options[:verbose]
      begin
        friendly = Themes::Theme.load(theme_name)
        say "  #{theme_name}:", :cyan
        say "    Name: #{friendly.name}"
        say "    Colors: #{friendly.color_scheme&.colors&.count || 0}"
        say "    Variants: #{friendly.variants&.count || 0}"
      rescue StandardError => e
        say "  #{theme_name}: Error loading - #{e.message}", :red
      end
    else
      say "  - #{theme_name}"
    end
  end
end