Add module Helpers
This commit is contained in:
parent
460427e6f6
commit
60281e78eb
3 changed files with 21 additions and 6 deletions
17
lib/helpers.rb
Normal file
17
lib/helpers.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Helpers
|
||||
refine String do
|
||||
def etc(max_length, etc: '...')
|
||||
return self if length <= max_length
|
||||
return '' unless max_length.positive?
|
||||
orig_length = max_length - etc.length
|
||||
return etc[0...max_length] if orig_length <= 0
|
||||
"#{self[0...orig_length]}#{etc}"
|
||||
end
|
||||
|
||||
def ljustetc(required_length, padstr: ' ', etc: '...')
|
||||
ljust(required_length, padstr).etc(required_length, etc: etc)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,6 +6,7 @@ require 'thread'
|
|||
|
||||
require 'faker'
|
||||
|
||||
require 'helpers'
|
||||
require 'screen'
|
||||
|
||||
class Main
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Widgets
|
||||
using Helpers
|
||||
|
||||
class Chat < VPanel
|
||||
class Info < Base
|
||||
PUBLIC_KEY_LABEL = 'Public key: '
|
||||
|
@ -43,12 +45,7 @@ module Widgets
|
|||
def draw_public_key
|
||||
setpos 0, 1
|
||||
addstr PUBLIC_KEY_LABEL
|
||||
if PUBLIC_KEY_LABEL.length + props[:public_key].length > props[:width]
|
||||
width = props[:width] - PUBLIC_KEY_LABEL.length
|
||||
addstr "#{props[:public_key][0...(width - 3)]}..."
|
||||
else
|
||||
addstr props[:public_key]
|
||||
end
|
||||
addstr props[:public_key].ljustetc props[:width] - PUBLIC_KEY_LABEL.length
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue