forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopbar.lua
More file actions
114 lines (96 loc) · 3.19 KB
/
topbar.lua
File metadata and controls
114 lines (96 loc) · 3.19 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
local config = require('opencode.config')
local util = require('opencode.util')
local winbar = require('opencode.ui.winbar')
local M = {}
local state = require('opencode.state')
local config_file = require('opencode.config_file')
local LABELS = {
NEW_SESSION_TITLE = 'New session',
}
local function format_token_info()
local parts = {}
if state.current_model then
if config.ui.display_context_size then
local provider, model = state.current_model:match('^(.-)/(.+)$')
local ok, model_info = pcall(config_file.get_model_info, provider, model)
if not ok then
model_info = nil
end
local limit = state.tokens_count and model_info and model_info.limit and model_info.limit.context or 0
local formatted_count = util.format_number(state.tokens_count)
if formatted_count then
table.insert(parts, formatted_count)
end
if limit > 0 then
local formatted_pct = util.format_percentage(state.tokens_count / limit)
if formatted_pct then
table.insert(parts, formatted_pct)
end
end
end
if config.ui.display_cost and state.cost and state.cost > 0 then
local formatted_cost = util.format_cost(state.cost)
if formatted_cost then
table.insert(parts, formatted_cost)
end
end
end
local result = table.concat(parts, ' | ')
result = result:gsub('%%', '%%%%')
return result
end
local function create_winbar_text(description, token_info, _)
return description .. '%=' .. token_info
end
local function get_session_desc()
if state.is_opening then
return 'Loading...'
end
local session_title = LABELS.NEW_SESSION_TITLE
if state.active_session and state.active_session.title ~= '' then
session_title = state.active_session.title
end
if not session_title or type(session_title) ~= 'string' then
session_title = ''
end
return session_title
end
function M.render()
vim.schedule(function()
if not state.windows then
return
end
local win = state.windows.output_win
if not win or not vim.api.nvim_win_is_valid(win) then
return
end
vim.wo[win].winbar = ' '
local desc = get_session_desc():gsub('%%', '%%%%')
local token_info = format_token_info()
local winbar_str = create_winbar_text(desc, token_info, vim.api.nvim_win_get_width(win))
vim.wo[win].winbar = winbar_str
winbar.update_highlights(win, 'OpencodeSessionDescription')
end)
end
local function on_change(_, _, _)
M.render()
end
function M.setup()
state.store.subscribe('current_mode', on_change)
state.store.subscribe('current_model', on_change)
state.store.subscribe('active_session', on_change)
state.store.subscribe('is_opencode_focused', on_change)
state.store.subscribe('tokens_count', on_change)
state.store.subscribe('cost', on_change)
state.store.subscribe('is_opening', on_change)
M.render()
end
function M.close()
state.store.unsubscribe('current_mode', on_change)
state.store.unsubscribe('current_model', on_change)
state.store.unsubscribe('active_session', on_change)
state.store.unsubscribe('is_opencode_focused', on_change)
state.store.unsubscribe('tokens_count', on_change)
state.store.unsubscribe('cost', on_change)
end
return M