Skip to content

Commit a9fbc85

Browse files
author
Chad Juliano
committed
Adding support for parameterized settings.
1 parent d585787 commit a9fbc85

File tree

3 files changed

+86
-27
lines changed

3 files changed

+86
-27
lines changed

src/biw-main.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
set -o nounset
1515

1616
source ${BIW_HOME}/biw-util.sh
17+
source ${BIW_HOME}/biw-term-sgr.sh
18+
source ${BIW_HOME}/biw-term-csi.sh
19+
source ${BIW_HOME}/biw-theme-mgr.sh
20+
source ${BIW_HOME}/biw-term-utf8.sh
21+
source ${BIW_HOME}/biw-panel-hmenu.sh
1722
source ${BIW_HOME}/biw-panel-vmenu.sh
1823
source ${BIW_HOME}/biw-panel-credits.sh
1924

@@ -52,8 +57,7 @@ function fn_biw_main()
5257

5358
fn_hmenu_init _hmenu_values[@]
5459

55-
# init the theme
56-
fn_theme_set_idx_active $theme_saved_idx
60+
fn_theme_init
5761

5862
# show the widgets
5963
fn_biw_show
@@ -66,6 +70,7 @@ function fn_biw_show()
6670
{
6771
local -r _history_cmd="fc -lnr -$BIW_LIST_MAX"
6872

73+
6974
fn_utl_panel_open
7075

7176
while [ 1 ]

src/biw-theme-mgr.sh

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ declare -ri THEME_SET_DEF_ACTIVE=30
4545
declare -ri THEME_SET_SLI_INACTIVE=40
4646
declare -ri THEME_SET_SLI_ACTIVE=50
4747

48-
# file for persisting theme
49-
declare -r BIW_SETTINGS_FILE=$HOME/.biw_settings
5048

5149
# initialize the default theme
5250
declare -i theme_active_idx=-1
@@ -60,31 +58,39 @@ declare -i theme_saved_idx=-1
6058
# reference of theme names
6159
declare -a theme_name_list
6260

61+
# settings param name
62+
declare -r theme_param_name='theme-name'
63+
6364
fn_theme_init()
6465
{
6566
fn_theme_set_desc_list
6667

6768
if [ ! -r $BIW_SETTINGS_FILE ]
6869
then
6970
# nothing to load so set default
70-
fn_theme_set_idx_active -1
71+
fn_theme_set_idx_active 0
7172
return
7273
fi
7374

74-
local _saved_name=$(cat $BIW_SETTINGS_FILE)
75-
75+
local _saved_name
76+
fn_settings_get_param '_saved_name' $theme_param_name
7677
fn_theme_idx_from_name $_saved_name
7778
theme_saved_idx=$?
79+
80+
# init the theme
81+
fn_theme_set_idx_active $theme_saved_idx
82+
}
83+
84+
function fn_theme_save()
85+
{
86+
theme_saved_idx=$theme_active_idx
87+
local _saved_theme=${THEME_LIST[$theme_saved_idx]}
88+
fn_settings_set_param $theme_param_name $_saved_theme
7889
}
7990

8091
function fn_theme_set_idx_active()
8192
{
8293
local -i _selected_idx=$1
83-
if ((_selected_idx == -1))
84-
then
85-
# use the default
86-
_selected_idx=0
87-
fi
8894

8995
if((_selected_idx == theme_active_idx))
9096
then
@@ -178,13 +184,6 @@ function fn_theme_parse_color()
178184
printf -v $_result_ref '%d' $_color_result
179185
}
180186

181-
fn_theme_save()
182-
{
183-
theme_saved_idx=$theme_active_idx
184-
local _saved_theme=${THEME_LIST[$theme_saved_idx]}
185-
echo ${_saved_theme} > $BIW_SETTINGS_FILE
186-
}
187-
188187
fn_theme_idx_from_name()
189188
{
190189
local -r _theme_name=$1
@@ -290,6 +289,3 @@ function fn_theme_set_color()
290289
fn_sgr_color16_set $_mode $_theme_color
291290
fi
292291
}
293-
294-
# always init theme
295-
fn_theme_init

src/biw-util.sh

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
# Description: Controller functions for vmenu panels
1111
##
1212

13-
source ${BIW_HOME}/biw-term-sgr.sh
14-
source ${BIW_HOME}/biw-term-csi.sh
15-
source ${BIW_HOME}/biw-theme-mgr.sh
16-
source ${BIW_HOME}/biw-term-utf8.sh
17-
source ${BIW_HOME}/biw-panel-hmenu.sh
1813

1914
# global widget params
2015
declare -ri BIW_MARGIN=10
@@ -32,6 +27,10 @@ declare UTL_DEBUG_MSG=''
3227

3328
declare -r UTL_OC_ANIMATE_DELAY=0.01
3429

30+
# file for persisting theme
31+
declare -r BIW_SETTINGS_FILE=$HOME/.biw_settings
32+
declare -A biw_settings_params
33+
3534
function fn_utl_set_cursor_pos()
3635
{
3736
local -i _abs_row=$1
@@ -167,6 +166,65 @@ function fn_utl_panel_close()
167166
trap - EXIT
168167
}
169168

169+
function fn_settings_get_param()
170+
{
171+
local _param_ref=$1
172+
local _param_name=$2
173+
174+
fn_settings_load
175+
local _param_val=${biw_settings_params[$_param_name]:-}
176+
printf -v $_param_ref '%s' "$_param_val"
177+
}
178+
179+
function fn_settings_set_param()
180+
{
181+
local _param_name=$1
182+
local _param_val=$2
183+
184+
biw_settings_params[$_param_name]="$_param_val"
185+
fn_settings_save
186+
}
187+
188+
function fn_settings_load()
189+
{
190+
biw_settings_params=()
191+
192+
if [ ! -r $BIW_SETTINGS_FILE ]
193+
then
194+
# nothing to do
195+
return 0
196+
fi
197+
198+
local _line
199+
while read -r _line
200+
do
201+
_key="${_line%%=*}"
202+
_value=${_line##*=}
203+
204+
if [ -z "$_key" ] || [ -z "$_value" ]
205+
then
206+
# skip this because we did not get a complete
207+
# key/val pair
208+
continue
209+
fi
210+
211+
biw_settings_params[$_key]="$_value"
212+
213+
done < $BIW_SETTINGS_FILE
214+
}
215+
216+
function fn_settings_save()
217+
{
218+
local _key
219+
local _value
220+
221+
for _key in "${!biw_settings_params[@]}"
222+
do
223+
_value="${biw_settings_params[$_key]}"
224+
printf '%s=%s\n' $_key "$_value"
225+
done > $BIW_SETTINGS_FILE
226+
}
227+
170228
function fn_utl_die()
171229
{
172230
local _err_msg=$1

0 commit comments

Comments
 (0)