Class: Architext::TUI

Inherits:
Object
  • Object
show all
Defined in:
lib/architext/tui.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: QueryPrompt, Selection, SourceConfigAction

Constant Summary collapse

HELP =
'Up/k Down/j move  space select  a all  / filter  n new search  v source  enter confirm  q back'
KEY_BINDINGS =
{
  ' ' => :space,
  'k' => :up,
  'j' => :down,
  'a' => :all,
  '/' => :filter,
  'n' => :new_query,
  'v' => :new_vault,
  'q' => :quit
}.freeze
LOGO =
[
  '    ___              __    _ __            __ ',
  '   /   |  __________/ /_  (_) /____  _  __/ /_',
  '  / /| | / ___/ ___/ __ \\/ / __/ _ \\| |/_/ __/',
  ' / ___ |/ /  / /__/ / / / / /_/  __/>  </ /_  ',
  '/_/  |_/_/   \\___/_/ /_/_/\\__/\\___/_/|_|\\__/  '
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdin:, stdout:, stderr:, app_name:) ⇒ TUI

Returns a new instance of TUI.



34
35
36
37
38
39
40
41
# File 'lib/architext/tui.rb', line 34

def initialize(stdin:, stdout:, stderr:, app_name:)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @app_name = app_name
  @color = Terminal.enabled?(@stdout)
  @intro_rendered = false
end

Instance Method Details

#prompt_query(default:, context:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/architext/tui.rb', line 43

def prompt_query(default:, context:)
  draw_intro
  draw_startup_source_status(context)
  @stdout.print render("[bold][cyan]Search query[/] [dim](default: #{default})[/] [dim]| type 'v' for source config, 'q' to quit:[/] ")
  input = @stdin.gets&.strip
  return QueryPrompt.new(query: nil, open_vault_config: false, quit: true) if input.nil?

  normalized = input.strip
  return QueryPrompt.new(query: nil, open_vault_config: true, quit: false) if %w[v /v vault /vault].include?(normalized.downcase)
  return QueryPrompt.new(query: nil, open_vault_config: false, quit: true) if %w[q /q quit /quit].include?(normalized.downcase)

  QueryPrompt.new(query: normalized.empty? ? default : normalized, open_vault_config: false, quit: false)
end

#prompt_source_config(context) ⇒ Object

rubocop:disable Metrics/AbcSize



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/architext/tui.rb', line 58

def prompt_source_config(context)
  draw_intro
  @stdout.puts render('[bold][cyan]Source Configuration[/]')
  @stdout.puts render("[dim]active source:[/] [cyan]#{context[:source]}[/]")
  @stdout.puts render("[dim]native root:[/] #{format_root_label(context[:root], context[:root_source])}")
  @stdout.puts render("[dim]obsidian vault:[/] #{format_vault_label(context[:active_vault], context[:active_vault_source])}")
  @stdout.puts render("[dim]saved Obsidian default:[/] #{format_saved_default(context[:default_vault])}")
  @stdout.puts render("[dim]Obsidian default config path:[/] #{context[:default_vault_path]}")
  @stdout.puts
  @stdout.puts render('[dim]Commands:[/]')
  @stdout.puts render('  [cyan]root <path>[/] [dim]use native markdown search in a folder[/]')
  @stdout.puts render('  [cyan]vault <vault>[/] [dim]use Obsidian CLI with a vault name or id[/]')
  @stdout.puts render('  [cyan]save <vault>[/] [dim]save persistent Obsidian default vault[/]')
  @stdout.puts render('  [cyan]clear[/] [dim]clear persistent Obsidian default vault[/]')
  @stdout.puts render('  [cyan]none[/] [dim]use Obsidian CLI default vault[/]')
  @stdout.puts render('  [cyan]back[/] [dim]return to search prompt[/]')
  @stdout.puts
  @stdout.print render('[bold][cyan]source-config[/]> ')
  input = @stdin.gets&.strip
  return source_config_action(back: true) if input.nil?

  command = input.strip
  return source_config_action(back: true) if command.empty?
  return source_config_action(back: true) if command.casecmp('back').zero?
  return source_config_action(session_vault: '') if command.casecmp('none').zero?
  return source_config_action(clear_default: true) if command.casecmp('clear').zero?

  if (match = command.match(/\Asave\s+(.+)\z/i))
    return source_config_action(set_default_vault: match[1].strip)
  end

  if (match = command.match(/\A(?:root|native)\s+(.+)\z/i))
    return source_config_action(session_root: match[1].strip)
  end

  if (match = command.match(/\A(?:vault|obsidian|use)\s+(.+)\z/i))
    return source_config_action(session_vault: match[1].strip)
  end

  source_config_action(session_root: command)
end

#select(paths, query:, diagnostics:) ⇒ Object

rubocop:disable Metrics/BlockLength



102
103
104
105
106
107
108
109
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
150
151
152
153
154
155
156
# File 'lib/architext/tui.rb', line 102

def select(paths, query:, diagnostics:)
  state = {
    query: query,
    diagnostics: diagnostics,
    filter: '',
    cursor: 0,
    offset: 0,
    selected: {}
  }

  with_screen do
    loop do
      visible = filtered_paths(paths, state[:filter])
      clamp_cursor!(state, visible.length)
      draw_selector(paths:, visible:, state:)

      case read_key
      when :up
        state[:cursor] -= 1
      when :down
        state[:cursor] += 1
      when :space
        toggle_current(visible, state)
      when :all
        toggle_all(visible, state)
      when :filter
        state[:filter] = prompt_inline('Filter visible results', state[:filter])
      when :new_query
        return Selection.new(
          paths: [],
          new_query: prompt_inline('New markdown search', state[:query]),
          source_config: false,
          reprompt_query: false
        )
      when :new_vault
        return Selection.new(
          paths: [],
          new_query: nil,
          source_config: true,
          reprompt_query: false
        )
      when :enter
        selected = selected_paths(paths, state)
        return Selection.new(paths: selected, new_query: nil, source_config: false, reprompt_query: false)
      when :quit
        return Selection.new(paths: [], new_query: nil, source_config: false, reprompt_query: true)
      when :ctrl_c
        return Selection.new(paths: [], new_query: nil, source_config: false, reprompt_query: false)
      end

      clamp_cursor!(state, visible.length)
      keep_cursor_visible!(state, visible.length)
    end
  end
end

#show_copied(bytes) ⇒ Object



172
173
174
# File 'lib/architext/tui.rb', line 172

def show_copied(bytes)
  @stdout.puts render("[green]Copied[/] [bold]#{bytes} bytes[/] [dim]to clipboard.[/]")
end

#show_dry_run(selected_paths, bytes) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/architext/tui.rb', line 184

def show_dry_run(selected_paths, bytes)
  draw_intro
  @stdout.puts render("[bold][green]Dry run[/] [dim]#{selected_paths.length} selected file(s)[/]")
  @stdout.puts
  selected_paths.each { |path| @stdout.puts render("  [cyan]•[/] #{path}") }
  @stdout.puts
  @stdout.puts render("[dim]Estimated context size:[/] [bold]#{bytes} bytes[/]")
end

#show_error(message) ⇒ Object



176
177
178
# File 'lib/architext/tui.rb', line 176

def show_error(message)
  @stderr.puts render("[red]#{message}[/]")
end

#show_info(message) ⇒ Object



180
181
182
# File 'lib/architext/tui.rb', line 180

def show_info(message)
  @stdout.puts render("[green]#{message}[/]")
end

#show_no_results(query, diagnostics:, default_vault_path:, obsidian_executable:) ⇒ Object

rubocop:enable Metrics/BlockLength



159
160
161
162
163
164
165
166
# File 'lib/architext/tui.rb', line 159

def show_no_results(query, diagnostics:, default_vault_path:, obsidian_executable:)
  @stderr.puts render("[red]No markdown notes matched[/] [amber]#{query.inspect}[/]  #{format_source_label(diagnostics)}")
  if diagnostics[:source] == 'obsidian'
    @stderr.puts render("[dim]default vault config:[/] #{default_vault_path}")
    @stderr.puts render("[dim]obsidian cli:[/] #{obsidian_executable}")
  end
  @stderr.puts render('[amber]Tip:[/] at search prompt type [bold]v[/] for source config, or pass [bold]--root[/].')
end

#show_no_selectionObject



168
169
170
# File 'lib/architext/tui.rb', line 168

def show_no_selection
  @stderr.puts render('[amber]No files selected.[/]')
end