refact
This commit is contained in:
192
bbs.rb
192
bbs.rb
@@ -6,20 +6,15 @@ require_relative 'lib/repository/online_users_repository'
|
||||
require_relative 'lib/repository/message_board_repository'
|
||||
require_relative 'lib/repository/wiki_repository'
|
||||
require_relative 'lib/repository/catalog_repository'
|
||||
require_relative 'lib/page/helpers'
|
||||
require_relative 'lib/page/idle_page'
|
||||
require_relative 'lib/page/messages_page'
|
||||
require_relative 'lib/page/new_msg_page'
|
||||
require_relative 'lib/page/wiki_list_page'
|
||||
require_relative 'lib/page/page_view_page'
|
||||
require_relative 'lib/page/games_page'
|
||||
require_relative 'lib/page/online_page'
|
||||
require_relative 'lib/page/sysinfo_page'
|
||||
require_relative 'lib/service/online_service'
|
||||
require_relative 'lib/service/message_service'
|
||||
require_relative 'lib/service/wiki_service'
|
||||
require_relative 'lib/service/games_service'
|
||||
|
||||
ONLINE = OnlineUsersRepository.new
|
||||
MESSAGES = MessageBoardRepository.new(ENV.fetch('MESSAGES_PATH', 'data/messages.dat'))
|
||||
WIKI = WikiRepository.new(token: ENV['WEBAPP_WIKIJS_TOKEN'])
|
||||
CATALOG = CatalogRepository.new
|
||||
ONLINE = OnlineService.new(OnlineUsersRepository.new)
|
||||
MESSAGES = MessageService.new(MessageBoardRepository.new(ENV.fetch('MESSAGES_PATH', 'data/messages.dat')))
|
||||
WIKI = WikiService.new(WikiRepository.new(token: ENV['WEBAPP_WIKIJS_TOKEN']))
|
||||
GAMES = GamesService.new(CatalogRepository.new)
|
||||
|
||||
MENU_ITEMS = [
|
||||
'Messages', 'Post Message', 'Blog Posts',
|
||||
@@ -32,67 +27,134 @@ CONT_X = 23
|
||||
CONT_Y = 2
|
||||
|
||||
THEME = {
|
||||
bg: "\e[0;40m", # #0A0800 ≈ black
|
||||
normal: "\e[0;33m", # #FFB000 amber (body text)
|
||||
bright: "\e[1;33m", # #FFD880 bright amber (headings)
|
||||
border: "\e[0;33m", # amber borders
|
||||
selected: "\e[0;30;43m", # #0A0800 fg on #C88000 bg (inverse)
|
||||
header: "\e[1;33m", # bright amber
|
||||
label: "\e[2;33m", # #6B4800 dim amber (secondary)
|
||||
status: "\e[0;30;43m", # #0A0800 fg on #C88000 bg
|
||||
input: "\e[1;33m", # bright amber for typed text
|
||||
success: "\e[1;32m", # green
|
||||
error: "\e[1;31m", # red
|
||||
bg: "\e[0;40m",
|
||||
normal: "\e[0;33m",
|
||||
bright: "\e[1;33m",
|
||||
border: "\e[0;33m",
|
||||
selected: "\e[0;30;43m",
|
||||
header: "\e[1;33m",
|
||||
label: "\e[2;33m",
|
||||
status: "\e[0;30;43m",
|
||||
input: "\e[1;33m",
|
||||
success: "\e[1;32m",
|
||||
error: "\e[1;31m",
|
||||
reset: "\e[0m",
|
||||
}.freeze
|
||||
|
||||
MAIN_TUI = BBS::TUI.define do
|
||||
helpers { include TUIHelpers }
|
||||
helpers { include BBS::TUI::Helpers }
|
||||
|
||||
init do
|
||||
@menu_sel = 0
|
||||
@page_title = 'TELETYPE BBS'
|
||||
@items = []
|
||||
@item_sel = 0
|
||||
@scroll = 0
|
||||
@detail = []
|
||||
@page_meta = +''
|
||||
@input = +''
|
||||
@status = +''
|
||||
@prev_page = nil
|
||||
init { init_state }
|
||||
chrome { render_chrome }
|
||||
|
||||
page :idle do
|
||||
enter { @page_title = 'TELETYPE BBS' }
|
||||
|
||||
render do
|
||||
text 'Navigate the menu with ↑↓, select with Enter.',
|
||||
x: CONT_X, y: CONT_Y + cont_h / 2, style: THEME[:normal]
|
||||
end
|
||||
|
||||
nav :menu, size: MENU_ITEMS.size
|
||||
key(:enter) do
|
||||
case MENU_ITEMS[@menu_sel]
|
||||
when 'Messages' then go :messages
|
||||
when 'Post Message' then go :new_msg
|
||||
when 'Blog Posts' then go :blog
|
||||
when 'HowTo Guides' then go :howto
|
||||
when 'Game Catalog' then go :games
|
||||
when 'Online Users' then go :online
|
||||
when 'System Info' then go :sysinfo
|
||||
when 'Exit' then :halt
|
||||
end
|
||||
end
|
||||
key('q') { :halt }
|
||||
key(:escape) { :halt }
|
||||
end
|
||||
|
||||
chrome do
|
||||
screen_fill bg: :black
|
||||
|
||||
panel_h = term_rows - 1
|
||||
|
||||
box x: 1, y: 1, w: LEFT_W, h: panel_h,
|
||||
style: THEME[:border], bg: :black
|
||||
box x: LEFT_W + 1, y: 1, w: term_cols - LEFT_W, h: panel_h,
|
||||
title: " #{@page_title} │ #{@username} │ #{ONLINE.count} online ",
|
||||
style: THEME[:border], bg: :black
|
||||
|
||||
list MENU_ITEMS.map { |i| i[0, LEFT_W - 4] },
|
||||
x: 2, y: CONT_Y,
|
||||
selected: @menu_sel,
|
||||
style: THEME[:normal], highlight: THEME[:selected]
|
||||
|
||||
bar y: term_rows,
|
||||
content: " \e[1;37;43m↑↓\e[0;30;43m Navigate \e[1;37;43mEnter\e[0;30;43m Select " \
|
||||
"\e[1;37;43mQ\e[0;30;43m Back \e[1;37;43mR\e[0;30;43m Refresh",
|
||||
style: THEME[:status]
|
||||
page :messages do
|
||||
enter { @page_title = 'MESSAGES'; load_messages(MESSAGES) }
|
||||
render { render_messages }
|
||||
nav :scroll, content: -> { @items }, window: -> { cont_h }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
IdlePage.configure(self)
|
||||
MessagesPage.configure(self)
|
||||
NewMsgPage.configure(self)
|
||||
WikiListPage.configure(self, :blog, category: 'blog', title: 'BLOG')
|
||||
WikiListPage.configure(self, :howto, category: 'howto', title: 'HOWTO GUIDES')
|
||||
PageViewPage.configure(self)
|
||||
GamesPage.configure(self)
|
||||
OnlinePage.configure(self)
|
||||
SysinfoPage.configure(self)
|
||||
page :new_msg do
|
||||
enter { @page_title = 'POST MESSAGE'; @input = +''; @status = +'' }
|
||||
render { render_new_message_form }
|
||||
key(:enter) do
|
||||
case MESSAGES.post(@username, @input)
|
||||
when :empty then @status = 'Message cannot be empty.'
|
||||
when :ok then go :idle
|
||||
end
|
||||
end
|
||||
key(:backspace) { @input.chop! unless @input.empty? }
|
||||
key(:escape) { go :idle }
|
||||
printable { |ch| @input << ch if @input.length < 200 }
|
||||
end
|
||||
|
||||
page :blog do
|
||||
enter { @page_title = 'BLOG'; load_wiki_list(WIKI, 'blog') }
|
||||
render { render_wiki_list }
|
||||
nav :list, window: -> { cont_h - 3 }
|
||||
key(:enter) { open_wiki_page(WIKI, back: :blog) }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
page :howto do
|
||||
enter { @page_title = 'HOWTO GUIDES'; load_wiki_list(WIKI, 'howto') }
|
||||
render { render_wiki_list }
|
||||
nav :list, window: -> { cont_h - 3 }
|
||||
key(:enter) { open_wiki_page(WIKI, back: :howto) }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
page :page_view do
|
||||
render { render_page_view }
|
||||
nav :scroll, content: -> { @detail }, window: -> { cont_h - 2 }
|
||||
key('q') { @scroll = 0; go(@prev_page || :idle) }
|
||||
key(:escape) { @scroll = 0; go(@prev_page || :idle) }
|
||||
end
|
||||
|
||||
page :games do
|
||||
enter { @page_title = 'GAME CATALOG'; @items = GAMES.all; @item_sel = 0 }
|
||||
render { render_game_card }
|
||||
nav :cycle
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
page :online do
|
||||
enter { @page_title = 'ONLINE USERS'; @items = ONLINE.user_list }
|
||||
render { render_online_users }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
page :sysinfo do
|
||||
enter do
|
||||
@page_title = 'SYSTEM INFO'
|
||||
@items = [
|
||||
"Online users #{ONLINE.count}",
|
||||
"Messages #{MESSAGES.count}",
|
||||
"Wiki https://wiki.teletypegames.org",
|
||||
"Games API https://teletypegames.org",
|
||||
"Platform #{RUBY_PLATFORM}",
|
||||
"Ruby #{RUBY_VERSION}"
|
||||
]
|
||||
end
|
||||
render { render_sysinfo_rows }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
|
||||
start :idle
|
||||
end
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module GamesPage
|
||||
def self.configure(tui)
|
||||
tui.page :games do
|
||||
enter { @page_title = 'GAME CATALOG'; @items = CATALOG.fetch; @item_sel = 0 }
|
||||
|
||||
render do
|
||||
if @items.empty?
|
||||
text 'No games found.', x: CONT_X, y: CONT_Y, style: THEME[:normal]
|
||||
else
|
||||
game = @items[@item_sel]
|
||||
links = game.external_links
|
||||
|
||||
text "#{@item_sel + 1}/#{@items.size} #{game.title}"[0, cont_w],
|
||||
x: CONT_X, y: CONT_Y, style: THEME[:bright]
|
||||
text "#{game.platform} · #{game.author}"[0, cont_w],
|
||||
x: CONT_X, y: CONT_Y + 1, style: THEME[:normal]
|
||||
|
||||
story_rows = cont_h - 4 - links.size
|
||||
wordwrap(game.story, cont_w).first(story_rows).each_with_index do |l, i|
|
||||
text l, x: CONT_X, y: CONT_Y + 3 + i, style: THEME[:normal]
|
||||
end
|
||||
|
||||
link_y = CONT_Y + cont_h - links.size
|
||||
links.each_with_index do |lnk, i|
|
||||
text "#{tc(:label, lnk[:label])} #{lnk[:url]}"[0, cont_w],
|
||||
x: CONT_X, y: link_y + i
|
||||
end
|
||||
|
||||
text '↑↓ navigate q back', x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
end
|
||||
|
||||
nav :cycle
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,59 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module TUIHelpers
|
||||
def tc(style, str) = "#{THEME.fetch(style, THEME[:normal])}#{str}#{THEME[:reset]}"
|
||||
def cont_w = term_cols - CONT_X - 1
|
||||
def cont_h = term_rows - 4
|
||||
|
||||
def fmt_date(iso)
|
||||
Time.parse(iso.to_s).strftime('%Y-%m-%d')
|
||||
rescue
|
||||
iso.to_s
|
||||
end
|
||||
|
||||
def wordwrap(text, width)
|
||||
words = text.to_s
|
||||
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
|
||||
.gsub(/[#*_`~>|\\]/, '')
|
||||
.gsub(/\r?\n+/, ' ')
|
||||
.strip
|
||||
.split
|
||||
lines, line = [], +''
|
||||
words.each do |w|
|
||||
if line.empty? then line << w
|
||||
elsif line.length + 1 + w.length <= width then line << ' ' << w
|
||||
else lines << line.dup; line = +w
|
||||
end
|
||||
end
|
||||
lines << line unless line.empty?
|
||||
lines
|
||||
end
|
||||
|
||||
def wiki_list_render
|
||||
list_h = cont_h - 3
|
||||
visible = @items[@scroll, list_h] || []
|
||||
if visible.empty?
|
||||
text 'No items found.', x: CONT_X, y: CONT_Y, style: THEME[:normal]
|
||||
else
|
||||
list visible.map { |p| p.title[0, cont_w - 2] },
|
||||
x: CONT_X, y: CONT_Y,
|
||||
selected: @item_sel - @scroll,
|
||||
style: THEME[:normal], highlight: THEME[:selected]
|
||||
if (page = @items[@item_sel])
|
||||
hint = page.description.to_s.strip[0, cont_w]
|
||||
text hint, x: CONT_X, y: CONT_Y + cont_h - 2, style: THEME[:normal] unless hint.empty?
|
||||
end
|
||||
end
|
||||
text '↑↓ navigate Enter read q back', x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
|
||||
def open_wiki_page(back:)
|
||||
return unless (page = @items[@item_sel])
|
||||
@detail = wordwrap(WIKI.content(page.id), cont_w)
|
||||
@page_title = page.title
|
||||
@page_meta = "#{fmt_date(page.created_at)} #{WIKI.page_url(page.locale, page.path)}"
|
||||
@prev_page = back
|
||||
@scroll = 0
|
||||
go :page_view
|
||||
end
|
||||
end
|
||||
@@ -1,30 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module IdlePage
|
||||
def self.configure(tui)
|
||||
tui.page :idle do
|
||||
enter { @page_title = 'TELETYPE BBS' }
|
||||
|
||||
render do
|
||||
text 'Navigate the menu with ↑↓, select with Enter.',
|
||||
x: CONT_X, y: CONT_Y + cont_h / 2, style: THEME[:normal]
|
||||
end
|
||||
|
||||
nav :menu, size: MENU_ITEMS.size
|
||||
key(:enter) do
|
||||
case MENU_ITEMS[@menu_sel]
|
||||
when 'Messages' then go :messages
|
||||
when 'Post Message' then go :new_msg
|
||||
when 'Blog Posts' then go :blog
|
||||
when 'HowTo Guides' then go :howto
|
||||
when 'Game Catalog' then go :games
|
||||
when 'Online Users' then go :online
|
||||
when 'System Info' then go :sysinfo
|
||||
when 'Exit' then :halt
|
||||
end
|
||||
end
|
||||
key('q') { :halt }
|
||||
key(:escape) { :halt }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,26 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module MessagesPage
|
||||
def self.configure(tui)
|
||||
tui.page :messages do
|
||||
enter do
|
||||
@page_title = 'MESSAGES'
|
||||
@items = MESSAGES.last(30).map { |m| "#{tc(:label, m.timestamp)} #{tc(:bright, m.username)}: #{m.text}" }
|
||||
@scroll = 0
|
||||
end
|
||||
|
||||
render do
|
||||
(@items[@scroll, cont_h] || []).each_with_index do |line, i|
|
||||
text line[0, cont_w], x: CONT_X, y: CONT_Y + i
|
||||
end
|
||||
text "#{@items.size} msg(s) ↑↓ scroll q back",
|
||||
x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
|
||||
nav :scroll, content: -> { @items }, window: -> { cont_h }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,31 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module NewMsgPage
|
||||
def self.configure(tui)
|
||||
tui.page :new_msg do
|
||||
enter { @page_title = 'POST MESSAGE'; @input = +''; @status = +'' }
|
||||
|
||||
render do
|
||||
text 'Type your message and press Enter. Esc cancels.',
|
||||
x: CONT_X, y: CONT_Y, style: THEME[:normal]
|
||||
text "#{tc(:bright, @username)}: #{tc(:input, @input)}_",
|
||||
x: CONT_X, y: CONT_Y + 2
|
||||
unless @status.empty?
|
||||
text @status, x: CONT_X, y: CONT_Y + 4, style: THEME[:error]
|
||||
end
|
||||
end
|
||||
|
||||
key(:enter) do
|
||||
if @input.strip.empty?
|
||||
@status = 'Message cannot be empty.'
|
||||
else
|
||||
MESSAGES.append(@username, @input.strip[0...200])
|
||||
go :idle
|
||||
end
|
||||
end
|
||||
key(:backspace) { @input.chop! unless @input.empty? }
|
||||
key(:escape) { go :idle }
|
||||
printable { |ch| @input << ch if @input.length < 200 }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,22 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module OnlinePage
|
||||
def self.configure(tui)
|
||||
tui.page :online do
|
||||
enter { @page_title = 'ONLINE USERS'; @items = ONLINE.snapshot.sort.map { |_, name| name } }
|
||||
|
||||
render do
|
||||
@items.each_with_index do |uname, i|
|
||||
you = uname == @username ? tc(:normal, ' ← you') : ''
|
||||
text tc(:bright, uname) + you, x: CONT_X, y: CONT_Y + i
|
||||
end
|
||||
text "#{@items.size} user(s) online q back",
|
||||
x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,20 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PageViewPage
|
||||
def self.configure(tui)
|
||||
tui.page :page_view do
|
||||
render do
|
||||
text @page_meta[0, cont_w], x: CONT_X, y: CONT_Y, style: THEME[:normal]
|
||||
(@detail[@scroll, cont_h - 2] || []).each_with_index do |l, i|
|
||||
text l[0, cont_w], x: CONT_X, y: CONT_Y + 2 + i, style: THEME[:normal]
|
||||
end
|
||||
text "#{@scroll + 1}/#{@detail.size} ↑↓ scroll q back",
|
||||
x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
|
||||
nav :scroll, content: -> { @detail }, window: -> { cont_h - 2 }
|
||||
key('q') { @scroll = 0; go(@prev_page || :idle) }
|
||||
key(:escape) { @scroll = 0; go(@prev_page || :idle) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,29 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module SysinfoPage
|
||||
def self.configure(tui)
|
||||
tui.page :sysinfo do
|
||||
enter do
|
||||
@page_title = 'SYSTEM INFO'
|
||||
@items = [
|
||||
"Online users #{ONLINE.count}",
|
||||
"Messages #{MESSAGES.count}",
|
||||
"Wiki https://wiki.teletypegames.org",
|
||||
"Games API https://teletypegames.org",
|
||||
"Platform #{RUBY_PLATFORM}",
|
||||
"Ruby #{RUBY_VERSION}"
|
||||
]
|
||||
end
|
||||
|
||||
render do
|
||||
@items.each_with_index do |row, i|
|
||||
text row[0, cont_w], x: CONT_X, y: CONT_Y + i, style: THEME[:normal]
|
||||
end
|
||||
text 'q back', x: CONT_X, y: CONT_Y + cont_h, style: THEME[:normal]
|
||||
end
|
||||
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,22 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module WikiListPage
|
||||
def self.configure(tui, name, category:, title:)
|
||||
tui.page name do
|
||||
enter do
|
||||
@page_title = title
|
||||
@items = WIKI.list(category)
|
||||
@item_sel = 0
|
||||
@scroll = 0
|
||||
end
|
||||
|
||||
render { wiki_list_render }
|
||||
|
||||
nav :list, window: -> { cont_h - 3 }
|
||||
key(:enter) { open_wiki_page(back: name) }
|
||||
key('r') { reload }
|
||||
key('q') { go :idle }
|
||||
key(:escape) { go :idle }
|
||||
end
|
||||
end
|
||||
end
|
||||
7
lib/service/games_service.rb
Normal file
7
lib/service/games_service.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class GamesService
|
||||
def initialize(repo) = @repo = repo
|
||||
|
||||
def all = @repo.fetch
|
||||
end
|
||||
14
lib/service/message_service.rb
Normal file
14
lib/service/message_service.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MessageService
|
||||
def initialize(repo) = @repo = repo
|
||||
|
||||
def recent(count = 30) = @repo.last(count)
|
||||
def count = @repo.count
|
||||
|
||||
def post(username, text)
|
||||
return :empty if text.strip.empty?
|
||||
@repo.append(username, text.strip[0...200])
|
||||
:ok
|
||||
end
|
||||
end
|
||||
10
lib/service/online_service.rb
Normal file
10
lib/service/online_service.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class OnlineService
|
||||
def initialize(repo) = @repo = repo
|
||||
|
||||
def add(session_id, username) = @repo.add(session_id, username)
|
||||
def remove(session_id) = @repo.remove(session_id)
|
||||
def count = @repo.count
|
||||
def user_list = @repo.snapshot.sort.map { |_, name| name }
|
||||
end
|
||||
9
lib/service/wiki_service.rb
Normal file
9
lib/service/wiki_service.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WikiService
|
||||
def initialize(repo) = @repo = repo
|
||||
|
||||
def list(category) = @repo.list(category)
|
||||
def content(page_id) = @repo.content(page_id)
|
||||
def page_url(locale, path) = @repo.page_url(locale, path)
|
||||
end
|
||||
Reference in New Issue
Block a user