Commit 589ec6b5 by Iwasaki Yudai

Handle hterm preferences with better care

1 parent 86151f1a
Showing with 166 additions and 50 deletions
......@@ -29,15 +29,15 @@
"Rev": "56912fb08d85084aa318edcf2bba735b97cf35c5"
},
{
"ImportPath": "github.com/hashicorp/hcl",
"Rev": "54864211433d45cb780682431585b3e573b49e4a"
},
{
"ImportPath": "github.com/kr/pty",
"Comment": "release.r56-28-g5cf931e",
"Rev": "5cf931ef8f76dccd0910001d74a58a7fca84a83d"
},
{
"ImportPath": "github.com/yudai/hcl",
"Rev": "7227a719113b77a78318a43dba44951556ff9e3d"
},
{
"ImportPath": "github.com/yudai/umutex",
"Rev": "18216d265c6bc72c3bb0ad9c8103d47d530b7003"
}
......
......@@ -8,7 +8,7 @@ import (
"strconv"
"strings"
"github.com/hashicorp/hcl/hcl"
"github.com/yudai/hcl/hcl"
)
// This is the tag to use with structures to have settings for HCL
......@@ -173,6 +173,8 @@ func (d *decoder) decodeInterface(name string, o *hcl.Object, result reflect.Val
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case hcl.ValueTypeString:
set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
case hcl.ValueTypeNil:
return nil
default:
return fmt.Errorf(
"%s: cannot decode into interface: %T",
......@@ -259,14 +261,19 @@ func (d *decoder) decodeMap(name string, o *hcl.Object, result reflect.Value) er
func (d *decoder) decodePtr(name string, o *hcl.Object, result reflect.Value) error {
// Create an element of the concrete (non pointer) type and decode
// into that. Then set the value of the pointer to this type.
resultType := result.Type()
resultElemType := resultType.Elem()
val := reflect.New(resultElemType)
if err := d.decode(name, o, reflect.Indirect(val)); err != nil {
return err
}
switch o.Type {
case hcl.ValueTypeNil:
// NIL
default:
resultType := result.Type()
resultElemType := resultType.Elem()
val := reflect.New(resultElemType)
if err := d.decode(name, o, reflect.Indirect(val)); err != nil {
return err
}
result.Set(val)
result.Set(val)
}
return nil
}
......
......@@ -85,7 +85,7 @@ func (x *hclLex) Lex(yylval *hclSymType) int {
case '-':
return MINUS
case ',':
return x.lexComma()
return COMMA
case '=':
return EQUAL
case '[':
......@@ -166,27 +166,6 @@ func (x *hclLex) consumeComment(c rune) bool {
}
}
// lexComma reads the comma
func (x *hclLex) lexComma() int {
for {
c := x.peek()
// Consume space
if unicode.IsSpace(c) {
x.next()
continue
}
if c == ']' {
return COMMAEND
}
break
}
return COMMA
}
// lexId lexes an identifier
func (x *hclLex) lexId(yylval *hclSymType) int {
var b bytes.Buffer
......@@ -224,6 +203,8 @@ func (x *hclLex) lexId(yylval *hclSymType) int {
case "false":
yylval.b = false
return BOOL
case "null":
return NULL
}
return IDENTIFIER
......
......@@ -36,7 +36,7 @@ func TestLex(t *testing.T) {
"list.hcl",
[]int{
IDENTIFIER, EQUAL, LEFTBRACKET,
NUMBER, COMMA, NUMBER, COMMA, STRING,
NUMBER, COMMA, NUMBER, COMMA, STRING, COMMA, BOOL,
RIGHTBRACKET, lexEOF,
},
},
......@@ -63,6 +63,24 @@ func TestLex(t *testing.T) {
RIGHTBRACE, lexEOF,
},
},
{
"array_comment.hcl",
[]int{
IDENTIFIER, EQUAL, LEFTBRACKET,
STRING, COMMA,
STRING, COMMA,
RIGHTBRACKET, lexEOF,
},
},
{
"null.hcl",
[]int{
IDENTIFIER, EQUAL, NULL,
IDENTIFIER, EQUAL, LEFTBRACKET, NUMBER, COMMA, NULL, COMMA, NUMBER, RIGHTBRACKET,
IDENTIFIER, LEFTBRACE, IDENTIFIER, EQUAL, NULL, RIGHTBRACE,
lexEOF,
},
},
}
for _, tc := range cases {
......
......@@ -29,9 +29,10 @@ import (
%token <b> BOOL
%token <f> FLOAT
%token <num> NUMBER
%token <str> COMMA COMMAEND IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD
%token <str> EPLUS EMINUS
%token <str> NULL
%%
......@@ -96,6 +97,13 @@ objectitem:
Value: $3,
}
}
| objectkey EQUAL NULL
{
$$ = &Object{
Key: $1,
Type: ValueTypeNil,
}
}
| objectkey EQUAL STRING
{
$$ = &Object{
......@@ -152,6 +160,10 @@ list:
{
$$ = $2
}
| LEFTBRACKET listitems COMMA RIGHTBRACKET
{
$$ = $2
}
| LEFTBRACKET RIGHTBRACKET
{
$$ = nil
......@@ -166,10 +178,6 @@ listitems:
{
$$ = append($1, $3)
}
| listitems COMMAEND
{
$$ = $1
}
listitem:
number
......@@ -183,6 +191,20 @@ listitem:
Value: $1,
}
}
| BOOL
{
$$ = &Object{
Type: ValueTypeBool,
Value: $1,
}
}
| NULL
{
$$ = &Object{
Type: ValueTypeNil,
}
}
number:
int
......
......@@ -59,6 +59,14 @@ func TestParse(t *testing.T) {
"types.hcl",
false,
},
{
"array_comment.hcl",
false,
},
{
"null.hcl",
false,
},
}
for _, tc := range cases {
......
foo = null
bar = [1, null, 3]
baz {
foo = null
}
......@@ -3,7 +3,7 @@ package json
import (
"sync"
"github.com/hashicorp/hcl/hcl"
"github.com/yudai/hcl/hcl"
"github.com/hashicorp/go-multierror"
)
......
......@@ -3,8 +3,8 @@ package hcl
import (
"fmt"
"github.com/hashicorp/hcl/hcl"
"github.com/hashicorp/hcl/json"
"github.com/yudai/hcl/hcl"
"github.com/yudai/hcl/json"
)
// Parse parses the given input and returns the root object.
......
......@@ -23,8 +23,8 @@ import (
"github.com/braintree/manners"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket"
"github.com/hashicorp/hcl"
"github.com/kr/pty"
"github.com/yudai/hcl"
"github.com/yudai/umutex"
)
......@@ -64,7 +64,8 @@ type Options struct {
ReconnectTime int `hcl:"reconnect_time"`
Once bool `hcl:"once"`
PermitArguments bool `hcl:"permit_arguments"`
Preferences map[string]interface{} `hcl:"preferences"`
Preferences HtermPrefernces `hcl:"preferences"`
RawPreferences map[string]interface{} `hcl:"preferences"`
}
var Version = "0.0.11"
......@@ -87,7 +88,7 @@ var DefaultOptions = Options{
EnableReconnect: false,
ReconnectTime: 10,
Once: false,
Preferences: make(map[string]interface{}),
Preferences: HtermPrefernces{},
}
func New(command []string, options *Options) (*App, error) {
......
......@@ -13,6 +13,7 @@ import (
"syscall"
"unsafe"
"github.com/fatih/structs"
"github.com/gorilla/websocket"
)
......@@ -128,11 +129,20 @@ func (context *clientContext) sendInitialize() error {
return err
}
prefStruct := structs.New(context.app.options.Preferences)
prefMap := prefStruct.Map()
htermPrefs := make(map[string]interface{})
for key, value := range context.app.options.Preferences {
htermPrefs[strings.Replace(key, "_", "-", -1)] = value
for key, value := range prefMap {
rawKey := prefStruct.Field(key).Tag("hcl")
if _, ok := context.app.options.RawPreferences[rawKey]; ok {
htermPrefs[strings.Replace(rawKey, "_", "-", -1)] = value
}
}
prefs, err := json.Marshal(htermPrefs)
if err != nil {
return err
}
prefs, _ := json.Marshal(htermPrefs)
if err := context.write(append([]byte{SetPreferences}, prefs...)); err != nil {
return err
}
......
package app
type HtermPrefernces struct {
AltGrMode *string `hcl:"alt_gr_mode"`
AltBackspaceIsMetaBackspace bool `hcl:"alt_backspace_is_meta_backspace"`
AltIsMeta bool `hcl:"alt_is_meta"`
AltSendsWhat string `hcl:"alt_sends_what"`
AudibleBellSound string `hcl:"audible_bell_sound"`
DesktopNotificationBell bool `hcl:"desktop_notification_bell"`
BackgroundColor string `hcl:"background_color"`
BackgroundImage string `hcl:"background_image"`
BackgroundSize string `hcl:"background_size"`
BackgroundPosition string `hcl:"background_position"`
BackspaceSendsBackspace bool `hcl:"backspace_sends_backspace"`
CharacterMapOverrides map[string]map[string]string `hcl:"character_map_overrides"`
CloseOnExit bool `hcl:"close_on_exit"`
CursorBlink bool `hcl:"cursor_blink"`
CursorBlinkCycle [2]int `hcl:"cursor_blink_cycle"`
CursorColor string `hcl:"cursor_color"`
ColorPaletteOverrides []*string `hcl:"color_palette_overrides"`
CopyOnSelect bool `hcl:"copy_on_select"`
UseDefaultWindowCopy bool `hcl:"use_default_window_copy"`
ClearSelectionAfterCopy bool `hcl:"clear_selection_after_copy"`
CtrlPlusMinusZeroZoom bool `hcl:"ctrl_plus_minus_zero_zoom"`
CtrlCCopy bool `hcl:"ctrl_c_copy"`
CtrlVPaste bool `hcl:"ctrl_v_paste"`
EastAsianAmbiguousAsTwoColumn bool `hcl:"east_asian_ambiguous_as_two_column"`
Enable8BitControl *bool `hcl:"enable_8_bit_control"`
EnableBold *bool `hcl:"enable_bold"`
EnableBoldAsBright bool `hcl:"enable_bold_as_bright"`
EnableClipboardNotice bool `hcl:"enable_clipboard_notice"`
EnableClipboardWrite bool `hcl:"enable_clipboard_write"`
EnableDec12 bool `hcl:"enable_dec12"`
Environment map[string]string `hcl:"environment"`
FontFamily string `hcl:"font_family"`
FontSize int `hcl:"font_size"`
FontSmoothing string `hcl:"font_smoothing"`
ForegroundColor string `hcl:"foreground_color"`
HomeKeysScroll bool `hcl:"home_keys_scroll"`
Keybindings map[string]string `hcl:"keybindings"`
MaxStringSequence int `hcl:"max_string_sequence"`
MediaKeysAreFkeys bool `hcl:"media_keys_are_fkeys"`
MetaSendsEscape bool `hcl:"meta_sends_escape"`
MousePasteButton *int `hcl:"mouse_paste_button"`
PageKeysScroll bool `hcl:"page_keys_scroll"`
PassAltNumber *bool `hcl:"pass_alt_number"`
PassCtrlNumber *bool `hcl:"pass_ctrl_number"`
PassMetaNumber *bool `hcl:"pass_meta_number"`
PassMetaV bool `hcl:"pass_meta_v"`
ReceiveEncoding string `hcl:"receive_encoding"`
ScrollOnKeystroke bool `hcl:"scroll_on_keystroke"`
ScrollOnOutput bool `hcl:"scroll_on_output"`
ScrollbarVisible bool `hcl:"scrollbar_visible"`
ScrollWheelMoveMultiplier int `hcl:"scroll_wheel_move_multiplier"`
SendEncoding string `hcl:"send_encoding"`
ShiftInsertPaste bool `hcl:"shift_insert_paste"`
UserCss string `hcl:"user_css"`
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!