Class: Tastytrade::PositionsFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/tastytrade/cli/positions_formatter.rb

Overview

Formatter for displaying positions in various formats

Instance Method Summary collapse

Constructor Details

#initialize(pastel: nil) ⇒ PositionsFormatter

Returns a new instance of PositionsFormatter.



9
10
11
# File 'lib/tastytrade/cli/positions_formatter.rb', line 9

def initialize(pastel: nil)
  @pastel = pastel || Pastel.new
end

Instance Method Details

#format_table(positions) ⇒ Object

Format positions as a table



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tastytrade/cli/positions_formatter.rb', line 14

def format_table(positions)
  return if positions.empty?

  headers = ["Symbol", "Quantity", "Type", "Avg Price", "Current Price", "P/L", "P/L %"]
  rows = build_table_rows(positions)

  table = TTY::Table.new(headers, rows)

  begin
    puts table.render(:unicode, padding: [0, 1])
  rescue StandardError
    # Fallback for testing or non-TTY environments
    puts headers.join(" | ")
    puts "-" * 80
    rows.each { |row| puts row.join(" | ") }
  end

  display_summary(positions)
end