xo / usql

Universal command-line interface for SQL databases
MIT License
8.82k stars 346 forks source link

Syntax Highlighting doesn't work on Windows Terminal #399

Closed snwflake closed 1 year ago

snwflake commented 1 year ago

I don't know how pgsql handles multiline queries, but mariadb inserts a newline without flushing the previous buffer, hiding the parts before it.

usql: image image

mariadb:

MariaDB [test]> select *
    -> from bar
    -> ;

To me this seems like a natural feature to have, so I'm not sure if this is a bug, or actual intended behavior?

kenshaw commented 1 year ago

@snwflake I'm not sure what the question is here? The way that usql works is that it parses up until a ; or a \ character, assuming it's not within a set of (/), is within a string, or is otherwise an "unbalanced" statement. It doesn't have any other concept of SQL. The behavior is more or less similar to how psql works. Are you referring to the mysql command line client's behavior? If so, please note that usql is patterned after psql command line tool, not the mysql command line tool.

It would not make sense to continue appending to the raw query, because if there was two statements on a single line, a \g would only execute the previous statement, not both:

$ psql postgres://postgres:P4ssw0rd@localhost
psql (15.2, server 15.1 (Debian 15.1-1.pgdg110+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

postgres=# select 1; select 2
 ?column? 
----------
        1
(1 row)

postgres-# \p
select 2
postgres-# \g
 ?column? 
----------
        2
(1 row)

postgres=# \g
 ?column? 
----------
        2
(1 row)

postgres=# 

Compare the above psql output to usql's:

$ usql pg://
Connected with driver postgres (PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

pg:postgres@=> select 1; select 2
 ?column? 
----------
        1 
(1 row)

pg:postgres@-> \p
select 2
pg:postgres@-> \g
 ?column? 
----------
        2 
(1 row)

pg:postgres@=> \g
 ?column? 
----------
        2 
(1 row)

pg:postgres@=> 
snwflake commented 1 year ago

@kenshaw

psql:

postgres=# select * <ENTER>
postgres-# from test <ENTER>
postgres-# ; <ENTER>

usql:

my:user@testnode/db=> select * <ENTER>
<BUFFER GETS CLEARED> ^^^ NO LONGER VISIBLE ^^^
my:user@testnode/db->

psql seems to behave exactly like mysql cli in this instance. you enter parts of a query select * in this case, press enter to get a newline, and you continue entering parts of your query. mysqlcli, psql and any other sane sql client I've ever used preserved the parts of the query on the terminal that you've entered before pressing enter for a newline, usql does not. It seems to clear the buffer, if you press enter after inserting select * the statement vanishes, your prompt changes from => to -> and that's it.

I hope that makes more sense now.

Edit: didn't mean to close the issue, just forgot I had vim shortcuts enabled....

kenshaw commented 1 year ago

@snwflake this is not the behavior that I am getting:

$ usql my://
Connected with driver mysql (10.10.2-MariaDB-1:10.10.2+maria~ubu2204)
Type "help" for help.

my:root@=> select *
my:root@-> from test
my:root@-> ;
error: mysql: 1046: No database selected
my:root@=> \p
select *
from test
;
my:root@=> 
kenshaw commented 1 year ago

@snwflake You might want to turn off syntax highlighting and try again. You might be using a funky terminal. Unfortunately, the syntax highlighting doesn't work on every terminal. Is this on Windows?

kenshaw commented 1 year ago

@snwflake to turn off syntax highlighting, do the following:

my:root@=> \set SYNTAX_HL false
snwflake commented 1 year ago

Alright! image

Yes, unfortunately this is windows using https://github.com/microsoft/terminal

kenshaw commented 1 year ago

@snwflake Myself, I've only ever tested usql on the original Command Prompt, and Msys2's included terminal. Microsoft has made it difficult to run Windows 11 under a VM, but I will make a point in trying to get it running this weekend and will try to fix syntax highlighting on the new Windows Terminal.

snwflake commented 1 year ago

thanks @kenshaw, apparently we both changed the title xd I appreciate your effort, cheers!

Edit: Win10 is perfectly capable to run the new terminal btw, it only ships with win11 by default

kenshaw commented 1 year ago

@snwflake I've pushed a change on the master branch of my personal fork -- https://github.com/kenshaw/usql -- that fixes this on Windows Terminal. Before I add it to master here, I need to do more tests and ensure this doesn't break anything for other terminals. I should be able to check/test within the next day or so.

snwflake commented 1 year ago

Thanks, I'll get a goenv going and check it out!