summaryrefslogtreecommitdiff
path: root/config.lua
blob: 8af2a1a3abf7d24cd99b1efc3cc94c6a8dacc4d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
stock = {
	SPY = 100,
	VTI = 10
}
crypto = {
	BTC = 1.1,
	ETH = 11,
	LTC = 111
}
tda = {
	account_id = "x",
	client_id = "x",
	token_path = "/path/to/token"
}

columns = {
	{
		"symbol", function(str, asset)
			return colour_symbol(str, asset)
		end
	},
	{
		"usd_price", function(str, asset)
			return colour_zeros(group_thousands(string.format("%.4f", str)))
		end
	},
	{
		"usd_change", function(str, asset)
			return colour_change(string.format("%.2f%%", str))
		end
	},
	{
		"usd_total", function(str, asset)
			return colour_zeros(group_thousands(string.format("%.2f", str)))
		end
	},
	{
		"btc_price", function(str, asset)
			return colour_zeros(string.format("%.8f", str))
		end
	},
	{
		"btc_change", function(str, asset)
			return colour_change(string.format("%.2f%%", str))
		end
	},
	{
		"btc_total", function(str, asset)
			return colour_zeros(string.format("%.4f", str))
		end
	}
}

function group_thousands (str)
	local n
	repeat
		str, n = string.gsub(str, "^(%d+)(%d%d%d)", "%1,%2")
	until n == 0

	return str
end

function colour_zeros (str)
	local x, y = string.match(str, "(.*%.0?.-)(0*)$")
	if y == "" then
		return str
	else
		return x .. colour(y, "90")
	end
end

function colour (str, code)
	return string.format("\27[%sm%s\27[0m", code, str)
end

function colour_change (str)
	if str:sub(1, 1) == "-" then
		return colour(str, "31")
	else
		return colour(str, "32")
	end
end

function colour_symbol (str, asset)
	local t = get_asset_field(asset, "type")

	if t == "STOCK" then
		return colour(str, "1;36")
	elseif t == "OPTION" then
		return colour(str, "1;34")
	elseif t == "CRYPTO" then
		return colour(str, "1;33")
	else
		return colour(str, "1;35")
	end
end