Module: EasyCreds::Views::InitTree

Defined in:
lib/easy_creds/views/init_tree.rb

Constant Summary collapse

MAX_WIDTH =
120
MIN_LEFT_W =
32
MAX_LEFT_W =
54
HEADER_ROWS =
2
2
SOURCE_TAG =
{
  unset:        ['', 'unset',  :dim],
  template:     ['', 'tmpl',   :dim],
  generate:     ['', 'gen',    :ok],
  generate_alt: ['', 'alt',    :ok],
  from_op:      ['', 'op',     :info],
  manual:       ['', 'manual', :warn],
  ar_batch:     ['', 'batch',  :ok]
}.freeze

Class Method Summary collapse

Class Method Details

.build_tree_rows(entries) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/easy_creds/views/init_tree.rb', line 69

def self.build_tree_rows(entries)
  seen_groups = {}
  rows = []
  entries.each do |entry|
    parts = entry.key.split('.')
    group = parts.size > 1 ? parts.first : nil
    leaf  = parts.size > 1 ? parts[1..].join('.') : parts.first
    if group && !seen_groups[group]
      seen_groups[group] = true
      rows << { kind: :header, label: group }
    end
    indent = group ? 4 : 2
    rows << { kind: :entry, label: leaf, entry: entry, indent: indent }
  end
  rows
end

.redraw(state, ctx, show_help:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/easy_creds/views/init_tree.rb', line 86

def self.redraw(state, ctx, show_help:)
  width        = [TTY::Screen.width, MAX_WIDTH].min
  height       = TTY::Screen.height
  content_rows = [height - HEADER_ROWS - FOOTER_ROWS, 4].max

  # Right pane is wider (~56%) per design: left ratio 1.1, right 1.4
  left_w  = (width * 0.44).to_i.clamp(MIN_LEFT_W, MAX_LEFT_W)
  right_w = width - left_w - 1

  tree_rows     = build_tree_rows(state.entries)
  cursor_tree_i = tree_rows.index { |r| r[:kind] == :entry && r[:entry] == state.current } || 0
  state.adjust_scroll(cursor_tree_i, content_rows)

  visible_rows = tree_rows[state.scroll_offset, content_rows] || []
  right_rows   = show_help ? help_right_rows(right_w) : detail_right_rows(state, right_w)

  buf = +''
  buf << "\e[H\e[J"

  # Traffic-light title bar
  set_c  = state.set_count == state.total ? state.set_count.to_s : state.set_count.to_s
  status = "#{state.set_count}/#{state.total} set"
  buf << "#{Theme.title_bar("init · #{ctx.env}", width, status: status)}\n"
  buf << "#{Theme.box_h(width)}\n"

  content_rows.times do |i|
    left_row  = build_left_row(visible_rows[i], state, left_w)
    right_row = right_rows[i] || ''
    buf << "#{left_row}#{Theme.box_side}#{rpad(right_row, right_w)}\n"
  end

  buf << "#{Theme.box_h(width)}\n"
  buf << footer_line(state, width, show_help)

  print buf
end

.run(state, ctx) ⇒ Object



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
65
66
67
# File 'lib/easy_creds/views/init_tree.rb', line 28

def self.run(state, ctx)
  reader    = TTY::Reader.new(interrupt: :signal)
  show_help = false

  print TTY::Cursor.hide
  redraw(state, ctx, show_help: show_help)

  loop do
    key    = reader.read_keypress(nonblock: false)
    signal = InitDispatch.handle(key, state, root: ctx.root)

    case signal
    when :manual_entry
      print TTY::Cursor.show
      val = ctx.prompt.ask('  Value (input hidden): ') { |q| q.echo(false) }
      print TTY::Cursor.hide
      state.set_entry(state.current, value: val, source: :manual) if val.present?
    when :toggle_help
      show_help = !show_help
    when :save
      return :saved
    when :quit
      return :aborted unless state.dirty

      print TTY::Cursor.show
      system('clear')
      return :aborted unless ctx.prompt.yes?('  Quit without saving?', default: false)

      print TTY::Cursor.hide
    end

    redraw(state, ctx, show_help: show_help)
  end
rescue Interrupt
  :aborted
ensure
  print TTY::Cursor.show
  print TTY::Cursor.move_to(0, 0)
  print TTY::Cursor.clear_screen_down
end